具体看红色的部分:
#include<stdio.h>
#include<malloc.h>
// #define NULL 0
typedef struct link{
int data;
struct link *next;
} * LinkList;
#define LEN sizeof(LinkList)
int n;
LinkList creatlink(int h)
{
LinkList head;
LinkList p1,p2;
int j=0;
n=0;
p1=p2=(LinkList)malloc(LEN);
head=NULL;
while(j<h)
{
if(n==0)
{head=p1;
printf("input p1->data:\n");
scanf("%d",&p1->data);
}
else
{ p2=p1;
p1=(LinkList)malloc(LEN);
p2->next=p1;
printf("input the data:");
scanf("%d",&p1->data);
}
j++;
n++;
}
p1->next=NULL;
return(head);
}
void print(LinkList head )
{
LinkList p;
printf("Now ,These %d recods are:\n",n);
p=head;
do
{
printf("%d\n",p->data);
p=p->next;
}
while (p!=NULL);
}
LinkList insert(LinkList head,int i)
{
int j;
LinkList p0,p1,p2;
p1=head ;
p0=(LinkList)malloc(LEN);
printf("input the input data:");
scanf("%f",&p0->data);
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else if(i==1)
{
head=p0;
p0->next=p1;
}
else
if(i)
{
while(p1->next!=NULL&&j<i)
{
p2=p1;
p1=p1->next;
j++;
}
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;p0->next=NULL;
}
n=n+1;
return(head);
}
LinkList dellink(LinkList head,int i)
{
int j=1;
LinkList p1,p2;
if(head==NULL)
{
printf("\n list null!\n");
return(head);
}
p1=head;
while(j<i&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
j++;
}
if(i==1)
head=p1->next;
else if(i)
{
p2->next=p1->next;
n--;
}
else printf("%ld not be found!\n",i);
return(head);
}
void main()
{
LinkList creatlink(int h);
void print(LinkList head);
LinkList insert(LinkList head,int i);
LinkList dellink(LinkList head,int i);
LinkList head;
int i,p;
char a;
printf("Input how much nodes do you want to enter in ?\n");
scanf("%d",&p);
head = creatlink(p);
print(head);
printf(" 2 . which one do you want to delete or input :\n");
scanf("%d",&i);
printf(" 1 . The link have been created,you can instrt and delete link,input 'i' or 'd'\n");
scanf("%c",&a);
// 多个scanf语句先后出现时,避免使用%c格式输
// 此处,a 实际得到的值 是上一个scanf语句输入时的回车
// 建议 将 a 定义成 int 类型字符输入回车时候,缓冲流中是不留回车的,而其他输入完毕后缓冲流中还回有回车的.字符输入完毕不 //是回车时以后输入回车时候,如果遇到其他类型的时候.前边的回车符号就全部清空了
if(a=='i'||a=='I')
head=insert(head,i);
else if(a=='d'||a=='D')
head=dellink(head,i);
print(head);
}
运行图片如下: