선형 테이블 삽입 및 삭제

다음 절차에서는 선형 테이블을 삽입하고 삭제합니다.
//         
//     
#include<iostream>
using namespace std;
typedef struct LNode{
    int data;
    struct LNode *next;
};
int main(){
    //   
    LNode *head=new LNode;
    head->data=NULL;
    head->next=NULL;
    int n;
    LNode *p=head;
    while(cin>>n&&n){
        LNode *q=new LNode;
        q->data=n;
        q->next=NULL;
        p->next=q;
        p=q;
    }
    /*
       
    while(cin>>n&&n){
        LNode *q=new LNode;
        q->data=n;
        q->next=NULL;
        q->next=p->next;
        p->next=q;
    }
    */
       
    p=head->next;
    while(p!=NULL){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    cin.ignore();
    cout<<"        :"<<endl;
    cin>>n;
    p=head->next;
    LNode *q;
    q=head;
    LNode *r;
    while(p->next!=NULL){
        if(p->data==n){
            r=p;
            q->next=p->next;
            delete r;
            break;
        }
        else{
            q=p;
            p=p->next;
        }
    }
    if(p->next==NULL){
        r=p;
        delete r;
    }
    q->next=NULL;
    p=head->next;
    while(p!=NULL){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    delete p;
    delete q;
    return 0;
}
//      
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct LNode{
    int data;
    struct LNode *next;
    struct LNode *pre;
};
int main(){
    //   
    LNode *head=new LNode;
    head->data=NULL;
    head->next=NULL;
    head->pre=NULL;
    int n;
    LNode *p=head;
    while(cin>>n&&n){
        LNode *q=new LNode;
        q->data=n;
        q->next=NULL;
        q->pre=NULL;
        p->next=q;
        q->pre=p;
        p=q;
    }
    /*
       
    while(cin>>n&&n){
        LNode *q=new LNode;
        q->data=n;
        q->next=NULL;
        q->pre=NULL;
           
        if(p->next!=NULL){   //       
            q->next=p->next;
            q->pre=p;
            p->next->pre=q;
            p->next=q;
            p=q;
        }
        else{
            p->next=q;
            q->pre=p;
            p=q;
        }
        p=head;
    }
    */
    p=head->next;
    while(p!=NULL){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    cin.ignore();
    cout<<"         :"<<endl;
    cin>>n;
    LNode *r;
    p=head->next;
    while(p->next!=NULL){
        if(p->data==n){
            r=p;
            p->pre->next=p->next;
            p->next->pre=p->pre;
            delete r;
            break;
        }
        else
            p=p->next;
    }
    if(p->next==NULL){
        r=p;
        p->pre->next=NULL;
        delete r;
    }
    p=head->next;
    while(p!=NULL){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    delete p;
    system("pause");
    return 0;
}
//       
#include<iostream>
using namespace std;
typedef struct LNode{
    int data;
    struct LNode *next;
};
int main(){
    //   
    LNode *head=new LNode;
    head->data=NULL;
    head->next=head;
    LNode *p=head;
    int n;
    while(cin>>n){
        if(n==0){
            LNode *q=new LNode;
            q->data=n;
            q->next=NULL;
            p->next=q;
            q->next=head;
            break;
        }
        else{
            LNode *q=new LNode;
            q->data=n;
            q->next=NULL;
            p->next=q;
            p=q;
        }
    }
    /*
       
    while(cin>>n&&n){
        LNode *q=new LNode;
        q->data=n;
        q->next=NULL;
        if(p->next==head){
            p->next=q;
            q->next=head;
        }
        else{
            q->next=p->next;
            p->next=q;
        }
    }
    */
       
    p=head->next;
    while(p!=head){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    cout<<"         :"<<endl;
    cin>>n;
    p=head->next;
    LNode *q=head;
    LNode *r;
    while(p->next!=NULL){
        if(p->data==n){
            r=p;
            q->next=p->next;
            delete r;
            break;
        }
        else{
            q=p;
            p=p->next;
        }
    }
    if(p->next==NULL){
        r=p;
        q->next=head;
    }
    p=head->next;
    while(p!=head){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    delete p;
    delete q;
    return 0;
}
//      
#include<iostream>
using namespace std;
typedef struct LNode{
    int data;
    struct LNode *next;
    struct LNode *pre;
};
int main(){
    //   
    LNode *head=new LNode;
    head->data=NULL;
    head->next=head;
    head->pre=head;
    LNode *p=head;
    int n;
    while(cin>>n){
        LNode *q=new LNode;
        q->data=n;
        q->next=NULL;
        q->pre=NULL;
        if(n==0){
            p->next=q;
            q->pre=p;
            q->next=head;
            head->pre=q;
            break;
        }
        else{
            p->next=q;
            q->pre=p;
            p=q;
        }
    }
    /*
       
    while(cin>>n&&n){
        LNode *q=new LNode;
        q->data=n;
        q->next=NULL;
        q->pre=NULL;
        if(p->next==head&&p->pre==head){
            p->next=q;
            q->pre=p;
            q->next=head;
            head->pre=q;
        }
        else{
            q->next=p->next;
            q->pre=p;
            p->next->pre=q;
            p->next=q;
        }
        p=head;
    }
    */
    p=head->next;
    while(p!=head){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    cout<<"         :"<<endl;
    cin>>n;
    p=head->next;
    LNode *r;
    while(p->next!=head){
        if(p->data==n){
            r=p;
            p->pre->next=p->next;
            p->next->pre=p->pre;
            delete r;
            break;
        }
        else
            p=p->next;
    }
    if(p->next==head){
        r=p;
        p->pre->next=head;
        head->pre=p->pre;
        delete r;
    }
    p=head->next;
    while(p!=head){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    delete p;
    return 0;
}

좋은 웹페이지 즐겨찾기