双向循环链表

#include<iostream>

using namespace std;

typedef int ElemType;

typedef struct Node

{

         ElemType data;

         Node *next,*prior;

}LinkNode,*LinkList;

 

void creat_List(LinkList&L,int length)

{

         LinkNode *p,*first=L;

         while(length-->0)

         {

                   p=new LinkNode;

                   cin>>p->data;

                   L->next=p;

                   p->prior=L;

                   L=p;

         }

         L->next=first;

         first->prior=L;

}

void print(LinkList&real)

{

         LinkList last=real;

         LinkList head=real->next->next;

         while(head!=real->next)

         {

                   cout<<head->data<<'\t';

                   head=head->next;

         }

         cout<<endl;

         while(last->prior!=real)

         {

                   cout<<last->data<<'\t';

                   last=last->prior;

         }

         cout<<endl;

}

void main()

{

         LinkNode *first=new LinkNode;

         cout<<"请输入数据的个数"<<endl;

         int number;

         cin>>number;

         if(number<1)

         {

                   cout<<"输入数据有误:"<<endl;

                   exit(0);

         }

         creat_List(first,number);

         print(first);

}

 

posted @ 2012-09-26 23:12  ♂咱說 ろ算  阅读(153)  评论(0编辑  收藏  举报