雙向連結串列的建立及基本操作

bigbigship發表於2014-10-06
/****************************************************************************
                          author---bigship
                         date----2014.10.6
*****************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef struct nod
{
    int x;
    struct nod *next,*pre;
}Dulnode,*DulLinklist;

bool init()//雙連結串列的初始化
{
    DulLinklist L;
    L=(DulLinklist) malloc(sizeof(Dulnode)); //申請空間
    if(L==NULL) return false; //判斷是否申請成功
    L->next=NULL;  //初始化連結串列的長度為0;
    L->pre=NULL;
    return true;
}

DulLinklist Head_insert_Creat(int n)
{
    DulLinklist H;
    H=(DulLinklist )malloc(sizeof(Dulnode));
    H->pre=NULL;
    H->next=NULL;
    H->x=n;//記錄下連結串列的長度
    int x;
    while(n--){
        scanf("%d",&x);
        DulLinklist p;
        p=(DulLinklist ) malloc(sizeof(Dulnode)); //申請空間
        p->x=x;//頭插法建立雙連結串列元素的順序與輸入的順序是相反的
        p->next=H->next;   //將結點插入到表頭H-->|2|-->|1|-->NULL
        p->pre=H;
        H->next=p;
    }
    return H;
}

DulLinklist Tail_Insert_Creat(int n)//尾插法建立雙連結串列
{
    DulLinklist H,l,r;
    H=(DulLinklist )malloc(sizeof(Dulnode));
    H->next=NULL; //初始化雙連結串列
    H->pre=NULL;
    H->x=n;//記錄下連結串列的長度
    l=H;
    int x;
    while(n--){
        scanf("%d",&x);
        r=(DulLinklist )malloc(sizeof(Dulnode));
        r->x=x;//尾插法建立雙連結串列元素的順序與輸入的順序是相同的
        l->next=r;//將結點插入到表尾H-->|1|-->|2|-->NULL
        r->pre=l;
        l=r;
    }
    l->next=NULL;
    return H;
}

DulLinklist Pos_insert(DulLinklist H,int i,int x)//雙連結串列在i位置插入值x
{
    DulLinklist pp;
    pp = H;
    for(int j=1;j<i;j++) //查到第i-1個位置
        pp=pp->next;
    DulLinklist p;
    p=(DulLinklist )malloc(sizeof(Dulnode));
    p->x=x;
    p->next=pp->next;
    p->pre=pp;
    pp->next=p;      //L--->L[1]--->L[2](--->)L[3]----->NULL
    return H;                      //  |       個
}                                  //  |__p____|

DulLinklist Val_delete(DulLinklist H,int x)//刪除第一個值為x的節點,x必須存在
{
    DulLinklist p,pp;
    p=H->next;
    while(p->x!=x&&p!=NULL){//查詢值為x的元素
        pp=p;
        p=p->next;                     // ________________
    }                                  //|                V
    pp->next=p->next;  //L--->L[1]--->L[2]--->(L[3])--->L[4]----->NULL
    p->next->pre=p->pre;
    free(p);
    return H;
}

DulLinklist del_x(DulLinklist H,int x)//刪除所有的值為x的節點
{
    int flag=0;
    DulLinklist p;
    p=(DulLinklist)malloc(sizeof(Dulnode));
    p=H->next;
    while(p)
    {
        if(p->next)
        if(p->x == x){
            if(p->next !=NULL){
                p->next->pre =p->pre ;
                p->pre->next =p->next ;
                p=p->next ;
                flag++;
            }
            else//針對最後一個元素為刪除結點
            {
                p->pre->next =NULL;
                flag++;
                p=p->next ;
            }

        }
        else p=p->next;
    }
    if(flag!=0)
        printf("刪除了總共%d個節點\n",flag);
    else
        printf("沒有找到你要刪除的元素\n");
    return H;
}

int search_pos(DulLinklist H,int x)
{
    int cnt=1;
    DulLinklist p=H->next;
    while(p!=NULL){
        if(p->x==x) break;
        p=p->next;
        cnt++;
    }
    return cnt;
}

void output(DulLinklist H)//輸出雙連結串列
{
    DulLinklist p;
    p=H->next;
    while(p!=NULL){
        printf("%d ",p->x);
        p=p->next;
    }
    puts("");
}

int main()
{
    DulLinklist L;
    int n;
    scanf("%d",&n);//輸入準備建立的雙連結串列的長度
    L=Head_insert_Creat(n);//建立雙連結串列
    output(L);
    int pos,x;//輸入要插入的位置 和要插入的值
    scanf("%d%d",&pos,&x);
    Pos_insert(L,pos,x);
    output(L);
    scanf("%d",&x);//輸入要刪除的值
    Val_delete(L,x);
    output(L);
    scanf("%d",&x);//輸入要查詢的值
    printf("%d\n",search_pos(L,x));
    return 0;
}

相關文章