一个链表建立删除的问题,请大家帮我看看哪错了
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20];
float num;
char sex;
int age;
struct student *next;
};
struct student *creat(int n)
{
int i;
struct student *head, *p1, *p2;
head = (struct student*)malloc(sizeof(struct student));
head->next = NULL;
p2 = head;
printf("input the name,num,sex,age.\n");
for(i = 1; i <= n; i++)
{
p1 = (struct student*)malloc(sizeof(struct student));
scanf("name:%s,num:%f,sex:%c,age:%d",p1->name,&p1->num,&p1->sex,&p1->age);
p2->next = p1;
p1->next = NULL;
}
return (head);
}
void Del_Elem(struct student *head, int age)
{
struct student *p1,*p2;
p1 = head;
while((p1->age != age)&&(p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if(age == p1->age)
{
if(p1 == head)
{
head->next = p1->next;
}
else
p2->next = p1->next;
printf("the student of age %i is delete.\n",age);
free(p1);
}
else
printf("he is not exist\n");
}
void display(struct student *head)
{
struct student *p;
p = head;
do
{
printf("name:%s,num:%f,sex:%c,age:%d", p->name, p->num, p->sex, p->age);
p = p->next;
}while(p->next != NULL);
}
int main()
{
struct student *creat(int n);
void Del_Elem(struct student *head, int age);
void display(struct student *head);
struct student *head;
int n = 5;
head = creat(n);
display(head);
Del_Elem(head, 20);
display(head);
return 0;
}
出现两个错误
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/linklist.exe : fatal error LNK1120: 1 unresolved externals
是新手~不太明白~~帮帮忙看一下了~~
[解决办法]
逻辑修改了一下,修改的地方就不一一解释了,你自己比较看看吧:
#include <stdio.h >
#include <stdlib.h >
struct student
{
char name[20];
float num;
char sex;
int age;
struct student *next;
};
struct student *creat(int n)
{
int i;
struct student *head, *p1, *p2;
head = (struct student*)malloc(sizeof(struct student));
head->next = NULL;
p2 = head;
printf("input the name,num,sex,age.\n");
scanf("%s %f %c %d",head->name,&head->num,&head->sex,&head->age);
for(i = 1; i <= n-1; i++)
{
p1 = (struct student*)malloc(sizeof(struct student));
p1->next = NULL;
scanf("%s %f %c %d",p1->name,&p1->num,&p1->sex,&p1->age);
p2->next = p1;
p2 = p2->next;
}
return (head);
}
struct student *Del_Elem(struct student *head, int age)
{
struct student *p1,*p2;
p1 = head;
while( p1 != NULL && p1->age != age )
{
p2 = p1;
p1 = p1->next;
}
if( p1 != NULL && age == p1->age)
{
if(p1 == head)
head = p1->next;
else
p2->next = p1->next;
printf("the student of age %i is delete.\n",age);
free(p1);
}
else
{
printf("he is not exist\n");
}
return head;
}
void display(struct student *head)
{
struct student *p;
p = head;
while( p != NULL )
{
printf("name:%s,num:%f,sex:%c,age:%d\n", p->name, p->num, p->sex, p->age);
p = p->next;
}
}
int main()
{
struct student *creat(int n);
struct student *Del_Elem(struct student *head, int age);
void display(struct student *head);
struct student *head;
int n = 5;
head = creat(n);
display(head);
head = Del_Elem(head, 20);
display(head);
return 0;
}
[解决办法]
#include <stdio.h>#include <stdlib.h>struct student{ char name[20]; int num; short sex; int age; struct student *next;};struct student *creat(int n){ int i; struct student *head, *p1, *p2; printf("input the name,num,sex,age.\n"); head = (struct student*)malloc(sizeof(struct student)); scanf("%s%d%d%d",head->name,&head->num,&head->sex,&head->age); head->next = NULL; p2 = head; for(i = 1; i < n; i++) { p1 = (struct student*)malloc(sizeof(struct student)); scanf("%s%d%d%d",p1->name,&p1->num,&p1->sex,&p1->age); p2->next = p1; p1->next = NULL; p2=p1; // let p2 point to p1 } return (head);}void Del_Elem(struct student **head, int age)//we may change the head so we use **{ struct student *p1,*p2; p1 = *head; while((p1->age != age)&&(p1->next != NULL)) { p2 = p1; p1 = p1->next; } if(age == p1->age) { if(p1 == *head) { *head = p1->next;//let head point to the next } else p2->next = p1->next; printf("the student of age %i is delete.\n",age); free(p1); } else printf("he is not exist\n");}void display(struct student *head){ struct student *p; p = head; do { printf("name:%s,num:%d,sex:%c,age:%d\n", p->name, p->num, (p->sex>0)?'f':'m' , p->age); p = p->next; }while(p != NULL);//p already point to the next now}struct student *creat(int n);void Del_Elem(struct student **head, int age);void display(struct student *head);int main(){ struct student *head; int n = 5; head = creat(n); display(head); Del_Elem(&head, 22); display(head); return 0;}