如何创建一个单链表.docxVIP

  • 6
  • 0
  • 约6.56千字
  • 约 8页
  • 2020-09-17 发布于上海
  • 举报
①.如何创建一个单链表? 链表节点的定义:?typedef struct node?{?int data; //节点内容?node *next; //下一个节点?}node; 单链表的创建: 1 //创建单链表?2 node *create() 3 {?4 int i = 0; //链表中数据的个数?5 node *head, *p, *q;?6 int x = 0; 7 head = (node *)malloc(sizeof(node)); //创建头节点?8 9 while(1) 10 {?11 printf(Please input the data: "); 12 scanf("%d, x); 13 if (x == 0) //data为0时创建结束 14 break;?15 p = (node *)malloc(sizeof(node)); 16 p-data = x; 17 if (++i == 1)?18 { //链表只有一个元素 19 head-next = p; //连接到head的后面?20 } 21 else?22 {?23 q->next = p; //连接到链表尾端 24 }?25 q = p; //q指向末节点?26 } 27 q-next = NULL; //链表的最后一个指针为NULL?28 return head; 29 }?上面的代码中,使用w

文档评论(0)

1亿VIP精品文档

相关文档