#include<stdio.h>
#include<stdlib.h>
int main()
{
struct list{
int num;
struct list* prior;
};
char key;
struct list *head = NULL;
struct list *last = NULL;
struct list *p = NULL;
printf("do u want to write ur data?\n");
scanf("%c", &key);
while(key == 'y')
{
p = (struct list*)malloc(sizeof(struct list));
if(!p)
printf("no enough memory \n");
printf("in out ur data\n");
scanf("%d", &p->num);
p->prior = NULL;
if(head == NULL)
{
head = p;
last = p;
}
else
{
p->prior = head; //新建节点的上一个节点指向头结点
head = p; //始终令新建节点为头结点
}
printf("\ndo u want to continue?\n");
scanf(" %c", &key);
}
while(head!= NULL)
{
printf("%d", head->num);
head = head->prior;
}
return 0;
}
【C语言】单向链表 头插法
最新推荐文章于 2022-10-04 21:55:05 发布