|
发表于 2020-4-18 14:28:45
|
显示全部楼层
- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- struct LNode
- {
- int data;
- struct LNode *next;
- };
- struct LNode* CreateList(int n);
- void print(struct LNode* s);
- int main()
- {
- struct LNode* Head = NULL;
- int n;
- printf("please input the num of data:");
- scanf("%d", &n);
- Head = CreateList(n);
- print(Head); printf("\n");
- return 0;
- }
- struct LNode * CreateList(int n)
- {
- struct LNode* pHead, *p, *q;//你想怎么定义链表头?
- int i,a;
- pHead =p=q= NULL;
- for (i = 1; i <= n; i++)
- {
- //主要是这里面的逻辑,你才理一理才行
- p = (struct LNode*) malloc(sizeof(struct LNode));//申请内存注意强制转换
- if (!p) return NULL;
- printf("please input the %d nun:", i);
- scanf("%d", &(p->data));
- if (pHead == NULL) {
- pHead = p;
- q = p;
- }
- else {
- q->next = p;
- q = p;
- }
- }
- q->next = NULL;
- return pHead;
- }
- void print(struct LNode* s)
- {
- struct LNode* p = s;
- while (p != NULL)
- {
- printf("%d", p->data);
- p = p->next;
- }
- }
复制代码
稍做修改了 |
|