c语言链表
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
struct student
{
int ornum;
double score;
struct student *next;
};
#define LEN sizeof(struct student)
struct student *create();
void print(struct student *head);
struct student *del(struct student *p,int n);
int n=0;
void main()
{
int num;
struct student *stu , *p;
stu = create();
p = stu ;
print(p);
printf("please input the order number you want to delete:");
scanf("%d",&num);
print(del(stu , num));
printf("\n\n");
system("pause");
}
struct student *create()
{
struct student *p1,*p2,*head;
p1 = p2 = (struct student *)malloc(LEN);
printf("请输入第%d个学生的学号:",n+1);
scanf("%d", &p1->ornum);
printf("请输入第%d个学生的成绩:",n+1);
scanf("%lf",&p1->score);
while(p1->ornum)
{
if(!n)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
n++;
p1 = p2 = (struct student *)malloc(LEN);
printf("请输入第%d个学生的学号:",n+1);
scanf("%d", &p1->ornum);
printf("请输入第%d个学生的成绩:",n+1);
scanf("%lf",&p1->score);
}
p2->next = NULL;
return head;
}
struct student *del(struct student *head,int n)
{
struct student *p1,*p2;
if(!head)
{
printf("该链表为空表\n");
goto end;
}
p1 = head ;
while(p1->ornum != n&&p1->next !=NULL)
{
p2 = p1;
p1 = p1->next ;
}
if(p1->ornum ==n)
{
if(p1 == head)
{
head = p1->next ;
}
else
{
p2->next = p1->next ;
}
printf("\ndelete No.%d succeed!\n");
n--;
}
else
{
printf("找不到!");
}
end:
return head;
}
void print(struct student *head)
{
struct student *p;
printf("there are %d student in total!\n",n);
p = head;
if(head)
{
do
{
printf("学生的学号为%d,成绩为%lf\n",p->ornum ,p->score );
p=p->next ;
}while(p);
}
}求助大家,这段代码怎么也不对,print函数只能打印出第一个学生的信息 本帖最后由 jackz007 于 2020-2-19 23:11 编辑
p2 = p1;
n++;
p1 = p2 = (struct student *)malloc(LEN);// 问题语句,前面的 p1、p2 在这里通通都丢掉了,也就是说,本节点会丧失与上一个节点的联系,所有节点都会成为孤立节点
下面是我修改的代码版本,谨供楼主参考
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student
{
int ornum ;
double score ;
struct student * next ;
} NODE , * PNODE ;
int n = 0 ;
PNODE create()
{
PNODE p1 , p2 , head ;
int ornum ;
double score ;
char s ;
for(head = NULL ;; n ++ , p1 = p2) {
printf("请输入第%d个学生的学号:" , n + 1) ;
fflush(stdin) ;
gets(s) ;
if(strlen(s) > 0 && sscanf(s , "%d" , & ornum) == 1 && ornum > 0) {
printf("请输入第%d个学生的成绩:" , n + 1) ;
scanf("%lf" , & score) ;
if((p2 = (PNODE) malloc(sizeof(NODE))) != NULL) {
p2 -> ornum = ornum ;
p2 -> score = score ;
p2 -> next= NULL ;
if(! n) head = p2 ;
else p1 -> next = p2 ;
} else {
while(head) {
p1 = head ;
head = p1 -> next ;
free(p1) ;
}
fprintf(stderr , "Error : malloc() falure !\n") ;
break ;
}
} else {
break ;
}
}
return head ;
}
void print(PNODE p)
{
if(n > 0) {
printf("链表中一共有 %d 个节点.\n" , n) ;
for(; p ; p = p -> next) printf("学生的学号为%d , 成绩为%lf\n" , p -> ornum, p -> score) ;
} else {
printf("链表为空。\n") ;
}
}
PNODE del(PNODE p , int ornum)
{
PNODE pd , head = p ;
bool f = false ;
if(n > 0) {
if(head -> ornum == ornum) {
pd = head ;
head = head -> next ;
f = true ;
} else {
for (; ! f && p -> next ; p = p -> next) {
if(p -> next -> ornum == ornum) {
pd = p -> next ;
p -> next = p -> next -> next ;
f = true ;
}
}
}
if(f) {
free(pd) ;
n -- ;
printf("节点已经删除。\n") ;
} else {
printf("节点未找到。\n") ;
}
} else {
printf("链表为空。\n") ;
}
return head ;
}
main(void)
{
int num ;
PNODE stu , p ;
stu = create() ;
p = stu ;
print(p) ;
printf("请输入需要删除学生的学号:") ;
scanf("%d" , & num) ;
print(del(stu , num)) ;
printf("\n\n") ;
system("pause") ;
}
页:
[1]