循环链表的创建及遍历

来源:互联网 发布:nginx 限制ip访问目录 编辑:程序博客网 时间:2024/05/16 14:18
#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");}

0 0
原创粉丝点击