- /*---------------------------------------------------------------
- —————c语言设计一个单链表实现数据存储,节点总数计算,节点遍历!----------
- --------------------------------------------------------------*/
- #include<stdio.h>
- #include"stdlib.h"
- typedef struct node
- {
- int data;
- struct node *next;
- }n_node,*pnode;
- pnode linearlist()
- {
- n_node *phead,*s,*r;
- int x,tempindex = 0;
- phead = (n_node*)malloc(sizeof(n_node));
- r = phead;
-
-
- while(tempindex<20)
- {
-
- printf("请输入链表的第[%d]个节点\n",tempindex);
- scanf("%d",&x);
-
-
- s = (n_node*)malloc(sizeof(n_node));
- s->data = x;
- s->next = NULL;
- r->next = s;
- r = s;
- tempindex++;
-
- }
- phead = phead->next;
- return phead;
- }
- int main()
- {
-
- int len = 0;
- n_node *p;
- p = linearlist();
- printf("----------------------\n");
- //while((p->next)!=NULL)
- while(1)
- {
- printf("打印链表的第%d个节点为:%d\n",len,p->data);
- p = p->next;
- len++;
- if((p->next)==NULL)
- {
- printf("打印链表的第%d个节点为:%d\n",len,p->data);
- break;
- }
- }
-
- printf("----------------------\n");
- printf("*****%d\n",p->data);
- printf("该链表的节点总数为 %d\n",len+1);
- return 0;
- }
复制代码
|