#include<stdio.h>
#include<malloc.h>
#define NULL 0
typedef int ElemType;
typedef struct node{
ElemType data;
node *next;
}*Node;
void Creat_list(Node &head)//头插法插入新节点
{
int x;
head->next=NULL;
Node p=new node;
printf("请输入数据,以0结束\n");
scanf("%d",&x);
while(x)
{
Node u=new node;
u->data=x;
u->next=head->next;
head->next=u;
scanf("%d",&x);
}
}
void Display_list(Node head)
{
Node p=new node;
p=head->next;
printf("现在的LIST为:\n");
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
}
void Insert_list(Node &head,ElemType x,int n)
{
Node p=new node;
p=head->next;
int i=1;
for(i=1;i<=n;i++)
p=p->next;
Node u=new node;
u->data=x;
u->next=p->next;
p->next=u;
}
void Delete_list(Node &head,int n)
{
Node p=new node;
Node u=new node;
p=head->next;
int i=1;
for(i=1;i<=n;i++)
p=p->next;
u=p->next;
p->next=u->next;
}
void main()
{
printf("本程序是用头插法插入节点,因此反序乃正常现象\n");
node head;
Node p=new node;
Creat_list(p);
Display_list(p);
Insert_list(p,677,2);
Display_list(p);
Delete_list(p,2);
Display_list(p);
}
线性链表C
最新推荐文章于 2022-10-11 20:28:59 发布