这个程序只为实现其功能,并未对其完善,意在了解动态链表的节点的申请,,实现输入输出。若要了解某个节点当前处于什么状态,可在其前面加上printf("%d\n",p->date);此时会输出当前p节点的值,帮助理解
上图:
代码:
#include<stdio.h>
#include<stdlib.h>
struct node{ //定义结构体,名字为node
char date;
struct node *next;
}
main()
{
int x;
struct node *head,*s,*r,*q; //声明结构体中要用到的指针
head=(struct node *)malloc(sizeof(struct node)); //用head指针去指向新生成的头节点
r=head; //将r指向头指针
printf("please input number_\n");
scanf("%d",&x);
while(x!=0) //当输入0时结束录入
{
s=(struct node *)malloc(sizeof(struct node));//用S去指向新生成的节点
s->date=x; //把第一个字符放在新生成的节点域
r->next=s; //让头节点指向新生成的节点
r=s; // r指向新生成的节点
scanf("%d",&x); //继续录入
}
r->next=NULL; //把终端节点的指针域置为空
q=head; //让q指向头指针
while(q!=r) //判断q是否指向了终端节点
{
q=q->next; //q指向下一个节点
printf("%d ",q->date); //输出数据
}
printf("\n");
struct node *p; //声明指针*p
int i=1,n; //用变量i记录指针移动的次数
printf("please input you want inset number_and inset where\n");
scanf("%d %d",&x,&n); //从键盘接受需要插入的数据
//x=getchar();
p=head->next; //让p指向头节点的下一个节点
while(i<n) //当<5时,及让p移动5次
{
p=p->next;
i++;
}
s=(struct node *)malloc(sizeof(struct node));//用s指向新生成的节点
s->date=x; //把接收到的字符放在新生成的节点域
s->next=p->next; //用新生成的节点去指向P的后一个节点
p->next=s; //让前面循环停下来的位点的值去指向新生成的节点
r->next=NULL;
q=head;
while(q!=r)
{
q=q->next;
printf("%d ",q->date);
}
system("pause"); //等待从键盘输入
}