单链表的建立 C语言实现

来源:互联网 发布:域名需要购买吗 编辑:程序博客网 时间:2024/06/14 18:30

#include <stdlib.h>#include <stdio.h>#include <malloc.h>struct LNode{int data;struct LNode *next;};//创建链表,count为创建的链表的节点数目struct LNode *create(int count){int i;struct LNode *pNode = NULL;struct LNode *pNewNode = NULL;struct LNode *head = NULL;printf("Input the integers :\n");for(i = count;i > 0;i--){pNewNode = (struct LNode*)malloc(sizeof(struct LNode));//分配节点空间scanf_s("%d",&pNewNode->data);if(head == NULL)//指定头结点{head = pNewNode;pNode = pNewNode;}else{pNode->next = pNewNode;pNode = pNewNode;}}pNode->next = NULL;return head;}//入口函数int main(){int count;//链表节点数struct LNode *node;printf("Input the count of nodes you want to create:");scanf_s("%d",&count);node = create(count);printf("The result is :\n");while(node){printf("%d",node->data);node = node->next;}system("pause");}

建立单链表的代码如下:


0 0
原创粉丝点击