上图为效果图
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct Node
{ int data;
struct Node *next;
} SLIST;
void inlist(SLIST *l,int a)
{
SLIST *p,*q;
int i;
p=(SLIST *)malloc(sizeof(SLIST));
l->next=p;
for(i=a;i>0;--i)
{
q=(SLIST *)malloc(sizeof(SLIST));
printf("请输入数字\n");
scanf("%d",&p->data);
p->next=q; /*这儿看看*/
p=q;
}
q->next=NULL;
}
void Inset(SLIST *l,int i,int e)
{
SLIST *p,*s;
int j;
p=l;
j=0;
while(p&&j<i-1)
{
p=p->next;
s=(SLIST *)malloc(sizeof(SLIST));
s->data=e;
s->next=p->next; /*这儿看看 链表的插入*/
p->next=s;
}
}
void OutputList(SLIST *h)
{ SLIST *p;
p=h->next;
if(p==NULL) printf("The list is NULL!\n");
else
{ printf("Head");
while((p->next)!=NULL)
{ printf("->%d",p->data);
p=p->next;
}
printf("->End\n");
}
}
void Delete(SLIST *l,int i)
{
SLIST *p;
int j;
p=l;
j=0;
while(p->next&&j<i-1)
{
p=p->next;
++j;
p->next=p->next->next;
}
}
int main()
{
SLIST *h;
int i,j,k,m,n;
h= (SLIST *)malloc(sizeof(SLIST));
printf("输入要求的长度\n");
scanf("%d",&i);
inlist(h, i);
OutputList(h);
printf("输入数字,1代表删除,2代表插入\n");
scanf("%d",&n);
while(n!=1||n!=2)
{
if(n==1)
{
printf("输入删除的位置\n");
scanf("%d",&j);
Delete(h,j);
OutputList(h);
}
else if(n==2)
{
printf("输入插入的位置与值\n");
scanf("%d%d",&k,&m);
Inset(h,k,m);
OutputList(h);
}
printf("再次输入数字,1代表删除,2代表插入\n");
scanf("%d",&n);
}
OutputList(h);
return 0;
}