#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node *next;
}Node,*linklist;
//创建循环链表
Node *createList(Node *head,int n)
{
Node *p;
for(int i=1;i<=n;i++)
{
p=(Node*)malloc(sizeof(Node));
ElemType a;
if(!p)
{
cout<<"内存分配失败"<<endl;
exit(0);
}
cin>>a;
p->elem=a;
p->next=head->next;
head->next=p;
}
return head;
}
//遍历循环链表
void printList(Node *head)
{
Node *p;
p=head->next;
while(p!=head)
{
cout<<p->elem<<endl;
p=p->next;
}
}
void main()
{
Node *head,*p,*q;
head=(Node*)malloc(sizeof(Node));
if(!head)
{
cout<<"内存分配失败"<<endl;
exit(0);
}
head->next=head;
createList(head,4);
printList(head);
system("pause");
}