谁能给我个C语言最简单的单链表例子。只要有创建和输出就足够了 要动态链表

我会输出和创建链表了,就是不会写主函数把他们两个子函数串联起来。。谢谢了 发来让我学习下 不要删除,增加节点 ,我还没学到那。

#include <stdio.h>
#include <malloc.h>

typedef struct node
{
int nDate;
struct node *pstnext;
}Node;

//链表输出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}

//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;

p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}

int main()
{
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;

//链表建立
Head = creat();

printf("输出建立的单链表\r\n");

//链表输出
output(Head);

return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答