the data struct on DulLinkList

来源:互联网 发布:淘宝小西装 编辑:程序博客网 时间:2024/05/19 06:51

// copyright vinco zhang

 

#include<stdio.h>
typedef int ElemType;

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

/******************************************/
void Insert_DuLink_i(DulLinkList p, int x);
void Insert_DuLink_x(DulLinkList p, int x);
void Delete_DuLink(DulLinkList p);
DulLinkList Create_DuLink_H(int n);
DulLinkList Create_DuLink_T(int n);
void print_DuLink(DulLinkList L);
/******************************************/

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

    return 0;
}

/******************************************/
void Insert_DuLink_i(DulLinkList p, int i)
{
   
}
void Insert_DuLink_x(DulLinkList p, int x)
{
    DulLinkList s;
    s=(DulLinkList)malloc(sizeof(DulNode));
    s->data=x;

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

void Delete_DuLink(DulLinkList 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;
        L->next = s;

        if(s->next)
        {
            s->next->prior=s;
            s->prior=L;
        }
        else
        {
            s->prior=L;
        }
    }
    print_DuLink(L);

    return L;
}

DulLinkList Create_DuLink_T(int n)
{
    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 %100d node!/n",i);
            return L;
        }
  
        printf("please input the data of the %10d node:/n",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++;
        printf("index=%d    L->data=%d/n",i,L->data );
        p=p->next ;
    }   
}

参考资料

《数据结构(C语言)》