http://blog.csdn.net/v_yang_guang_v/article/details/44906729
- #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");
- }