编程实现一个单链表的建立

  • Post author:
  • Post category:其他


typedef struct node 
{
	int data;
	node *next;
} node;
//单链表的创建
node *create()
{
	int i = 0;                             //链表中数据的个数  
	node *head, *p,*P                                           
	int x = 0;
	head = (node *)malloc(sizeof(node));   //创建头结点
	while(1)
	{
		printf("Please input the data:");
		scanf("%d",&x);
		if(x == 0)                        //data 为0时创建结束
			break;
		p = (node *)malloc(sizeof(node));
		p->data = x;
		if(++i ==1)                      //链表只有一个元素
		{
			head->next = p;              //连接到head的后面 
		}		
		else 
		{
			q->next = p;                 //连接到链表的尾端
		}
		q = p;                           //q指向末节点
	}
	q->next = NULL;                      //链表的最后一个指针为NULL
	return head;
}



版权声明:本文为Pollow_原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。