|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- # include <stdio.h>
- # include <malloc.h>
- # include <stdlib.h>
- typedef struct Node
- {
- int date;
- struct Node * pNext;
- }*PNODE, NODE;
- void Show_list(PNODE pHead)
- {
- PNODE p = pHead->pNext;
- while (NULL != p)
- {
- printf("%d", p->date);
- p = p->pNext;
- }
- }
- PNODE Create_list(void)
- {
- int len;
- int val;
- PNODE pHead = (PNODE)malloc(sizeof(NODE));
- if (NULL == pHead)
- {
- printf("分配失败!");
- exit(-1);
- }
- PNODE pTail = pHead;
-
- printf("Please input the length to the LIST: length = ");
- scanf ("%d", &len);
-
- for (int i=0; i<len; i++)
- {
- printf("请输入第%d个节点的数值", i+1);
- scanf ("%d", &val);
-
- PNODE pNew = (PNODE)malloc(sizeof(NODE));
- pTail->pNext = pNew;
- pNew->date = val;
- pNew->pNext = NULL;
- pTail = pNew;
- }
-
- return pHead;
- }
- int main (void)
- {
- PNODE pHead = (PNODE)malloc(sizeof(NODE));
-
- pHead = Create_list();
- Show_list(pHead);
-
-
- return 0;
- }
复制代码 这个是小弟在看数据结构时候写的链表,运行是输入链表长度为3时候Please input the length to the LIST: length = 3请输入第1个节点的数值1
请输入第2个节点的数值2
请输入第3个节点的数值3
123Press any key to continue
但是当length = 5时候却是一闪而过,求解??
|
|