循环链表

问题:我用的是尾插入法,主要是在输出的时候循环条件不要用错了^^,昨天查资料时遇见了一批“外星人”,今天看循环链表时又遇见“外星人”了**!

贴代码:

#include <iostream>

#include <cstdlib>



using namespace std;



typedef struct CircleList

{

	int data;

	struct CircleList *next;

}*CList;



void createCList(CList &clist)

{

	 CList head,node;

	 int c;

	 clist=(CList)malloc(sizeof(struct CircleList));

	 if(!clist)

	 {

		 cout<<"allocate fail"<<endl;

	 }

	 else

	 {

		 clist->next=NULL;

	 }

	 head=clist;

	 scanf("%d",&c);

	 while(c!=-1)

	 {

		 node=(CList)malloc(sizeof(struct CircleList));

		 if(!node)

		 {

			 cout<<"allocate fail"<<endl;

			 exit(-1);

		 }

		 else

		 {

		 node->data=c;

		 node->next=clist;

		 head->next=node;

		 head=node;

		 }

	     scanf("%d",&c);

	 }

}



void displayCList(CList clist)

{

	CList head;

	head=clist;

	while(head->next!=clist)

	{

		head=head->next;

		cout<<head->data<<"->";

	}

	cout<<endl;

}



int main()

{

	CList clist;

	cout<<"创建循环链表:"<<endl;

	createCList(clist);

	cout<<"输出循环单链表"<<endl;

    displayCList(clist);

	return 0;

}

 

运行结果:

循环链表

 

你可能感兴趣的:(链表)