求c语言大神指教 这是一个简易链表
#include<stdio.h>
#include<stdlib.h>
struct num
{ int num;
struct num *link;
};
void main()
{ struct num *head=NULL,*p,*q;
int n;
printf("pledse input number and end with number '0'\n");
scanf("%d",&n);
while(n!=0)
{ p=(struct num*)malloc(sizeof(struct num));
p->num=n;
p->link=NULL;
if(head==NULL)
head=p;
else
q->link=p; //使用了可能未初始化的本地指针变量“q” 求解
q=p;
scanf("%d",&n);
}
printf("the string is\n");
p=head;
while(p)
printf("%d",p->num);
printf("delete the fifth element\n");
p=head;
n=1;
while(p!=NULL&&n!=5)
{ q=p;
p=p->link;
n++;}
q->link=p->link;
p=head;
while(p)
printf("%d",p->num);
p=head;
while(p)
{ q=p->link;
free(p);
p=q;}
head=NULL;
} 展开
求 指出错误即可
给你修正ok了,已经在vc6下成功运行。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct num
{ int inum;
struct num *link;
};
void main()
{ struct num *head=NULL,*p,*q;
int n;
printf("please input number and end with number '0'\n");
scanf("%d",&n);
while(n!=0)
{ p=(struct num*)malloc(sizeof(struct num));
p->inum=n;
p->link=NULL;
if(head==NULL)
head=p;
else
q->link=p; //使用了可能未初始化的本地指针变量“q” 求解
q=p;
scanf("%d",&n);
}
printf("the string is\n");
p=head;
while(p)
{
printf("%d ",p->inum);
p=p->link;
}
printf("\n");
printf("delete the fifth element\n");
p=head;
n=1;
while(p!=NULL&&n<5)
{ q=p;
p=p->link;
n++;
}
if (p!=NULL)
{
printf("the deleted fifth element is: %d\n",p->inum);
q->link=p->link;
}
printf("剩余的元素\n");
p=head;
while(p)
{
printf("%d ",p->inum);
p=p->link;
}
printf("\n");
p=head;
while(p)
{ q=p->link;
free(p);
p=q;}
head=NULL;
}