2.the data struct on DulLinkList

来源:互联网 发布:sim800 tcp 接收数据 编辑:程序博客网 时间:2024/06/08 03:12

// there are some bugs in the prior one , and it's ok when I run it on my Ubuntu os, just believe in me !
// have a good time!

// copyright vinco zhang

#include<stdio.h>
typedef int ElemType;

typedef struct DulNode
{
    ElemType data;
    struct DulNode *prior,*next;
}DulNode,* DulLinkList;

/***************function declaration***************************/
void InsertBefore_DuLink_i(DulLinkList L, int i,int x);
void InsertAfter_DuLink_i(DulLinkList L, int i,int x);
void InsertBefore_DuLink(DulLinkList p, int x);
void InsertAfter_DuLink(DulLinkList p, int x);
void DeleteNode_DuLink(DulLinkList p);
DulLinkList Create_DuLink_H(int n);
DulLinkList Create_DuLink_T(int n);
void print_DuLink(DulLinkList L);
void print_DuLink_Reverse(DulLinkList L);
DulLinkList GetElem_DuLink_i(DulLinkList L,int i);
DulLinkList GetElem_DuLink_x(DulLinkList L,int x);

/****************main**************************/

int main()
{
    int n;
    DulLinkList L,p;
    printf("/ninput the length of the DulLinkList n=");
    scanf("%d",&n);
    printf("/n");

    L = Create_DuLink_T(n);
    printf("Create_DuLink_T/n");
    print_DuLink(L);
    //print_DuLink_Reverse(L);

    InsertBefore_DuLink_i(L, 3, 100);
    printf("InsertBefore_DuLink_i/n");
    print_DuLink(L);
    //print_DuLink_Reverse(L);

    InsertAfter_DuLink_i(L, 3, 200);
    printf("InsertAfter_DuLink_i/n");
    print_DuLink(L);
    //print_DuLink_Reverse(L);

    printf("GetElem_DuLink_i/n/n");
    p=GetElem_DuLink_i(L,5);

    InsertBefore_DuLink(p, 300);
    printf("InsertBefore_DuLink/n");
    print_DuLink(L);
    //print_DuLink_Reverse(L);

    InsertAfter_DuLink(p, 400);
    printf("InsertAfter_DuLink/n");
    print_DuLink(L);
    //print_DuLink_Reverse(L);

    DeleteNode_DuLink(p);
    printf("DeleteNode_DuLink/n");
    print_DuLink(L);
    //print_DuLink_Reverse(L);


    return 0;
}

/*****************function defination*************************/

void InsertBefore_DuLink_i(DulLinkList L, int i,int x)//insert before i th of the L
{
    int j=0;
    DulLinkList p=L,s;
    while(p->next && j<i)// forward to the i th
    {
        j++;
    p=p->next ;
    }
    s=(DulLinkList)malloc(sizeof(DulNode));
    s->data=x;
   
    s->prior=p->prior;
    p->prior->next=s;

    s->next=p;
    p->prior=s;
}

void InsertAfter_DuLink_i(DulLinkList L, int i,int x)// insert after i th of the L
{
    int j=0;
    DulLinkList p=L,s;
    while(p->next && j<i)// forward to the i th
    {
        j++;
    p=p->next ;
    }
    s=(DulLinkList)malloc(sizeof(DulNode));
    s->data=x;
   
    s->next=p->next;
    s->next->prior=s;
   
    p->next=s;
    s->prior=p;
}

void InsertBefore_DuLink(DulLinkList p, int x)// insert before the point of p
{
    DulLinkList s;
    s=(DulLinkList)malloc(sizeof(DulNode));
    s->data=x;

    s->prior = p->prior ;
    p->prior->next = s;
    s->next=p;
    p->prior = s;
   
    p=s;// for do not change p inside the function ???
}

void InsertAfter_DuLink(DulLinkList p, int x)// insert behind the point of p
{
    DulLinkList s;
    s=(DulLinkList)malloc(sizeof(DulNode));
    s->data=x;

    s->next=p->next;
    s->next->prior=s;
    p->next=s;
    s->prior=p;
}

void DeleteNode_DuLink(DulLinkList p)// delete the node which position is p
{
    p->prior->next = p->next;
    p->next->prior = p->prior;

    free(p);
}

DulLinkList Create_DuLink_H(int n)// insert into the behind of head node everytime
{
    int i;
    DulLinkList L = (DulLinkList)malloc(sizeof(DulNode));
    if(L==NULL)
    {
        printf("error when creating head node!/n");
        return NULL;
    }
    L->prior = NULL;
    L->next = NULL;
 
    for(i=1;i<=n;i++)
    {
        DulLinkList s = (DulLinkList)malloc(sizeof(DulNode));
        if(s==NULL)
        {
            printf("error when creating %d node!/n",i);
            return L;
        }
 
        printf("please input the data of the node:index=%d  data=",i);
        scanf("%d",&(s->data));
 
        s->next = L->next;
    s->prior=L;
        L->next = s;

        if(s->next)// if n!=1,n=1 means :there is only one node except the head one
        {
            s->next->prior=s;
        }
    }

    return L;
}

DulLinkList Create_DuLink_T(int n)//insert into the Tail of last node everytime
{
    int i;
    DulLinkList L = (DulLinkList)malloc(sizeof(DulNode));
    DulLinkList p = L;
    if(L==NULL)
    {
        printf("error when creating head node!/n");
        return NULL;
    }
    L->prior = NULL;
    L->next = NULL;
 
    for(i=1;i<=n;i++)
    {
        DulLinkList s = (DulLinkList)malloc(sizeof(DulNode));
        if(s==NULL)
        {
            printf("error when creating %d node!/n",i);
            return L;
        }
 
        printf("please input the data of the %d node:",i);
        scanf("%d",&(s->data));

        s->next = NULL;
        s->prior =p;
        p->next = s;
        p = s;
        //p->next =NULL;
    }
    return L;
}

void print_DuLink(DulLinkList L)
{
    int i=0;
    DulLinkList p=L;
    while(p->next!=NULL)
    {
        i++;
    p=p->next ;
        printf("index=%d    L->data=%d/n",i,p->data );
    }
    printf("/n");  
}

void print_DuLink_Reverse(DulLinkList L)// print the last one firstly
{
    int i=0;
    DulLinkList p,q=L;
    while(q->next)
    {
        i++;
        q=q->next;
    }
   
    p=q;// p point to the last one
    while(p->prior)// backforward to first one, but not the head one
    {   
            printf("index=%d    L->data=%d/n",i,p->data );
        p=p->prior ;
        i--;
    }
    printf("/n");
}

DulLinkList GetElem_DuLink_i(DulLinkList L,int i)
{
    int j=0;
    DulLinkList p=L;
    while(p->next && j<i)
    {
        j++;
    p=p->next ;
    } 
    return p;
}

DulLinkList GetElem_DuLink_x(DulLinkList L,int x)
{
    DulLinkList p=L->next;
    while(p && p->data!=x)
    {
    p=p->next ;
    } 
    return p;   
}