gpt4 book ai didi

c - 学生管理系统使用链表删除部分出错

转载 作者:行者123 更新时间:2023-11-30 16:29:50 24 4
gpt4 key购买 nike

我使用链表为学生管理系统编写了这段代码,但它的排序和删除节点部分不起作用。

谁能帮帮我吗?

它插入节点、修改节点并完美搜索节点。但是,当涉及到删除和排序时,一旦我们选择执行此操作的选项,程序就会挂起。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<dos.h>

struct stud
{
int rn,id,ph;
char add[30],na[20],d[15],in[10];
struct stud *next;
}*h=NULL,*p,*q,*t;

void add()
{
clrscr();
p =(stud *)malloc(sizeof(stud));
printf("\nEnter first name of Student : ");
scanf("%s",&p->in);
printf("\nEnter the Last Name of Student : ");
scanf("%s",&p->na);
printf("\nEnter the ID of Student not more
than 5 digits: ");
scanf("%d",&p->id);
printf("\nEnter the Roll No. of Student : ");
scanf("%d",&p->rn);
printf("\nEnter the Address of Student : ");
scanf("%s",&p->add);
printf("\nEnter the D.O.B. of
Student(dd/mm/yyyy) : ");
scanf("%s",&p->d);

p->next=NULL;

if(h==NULL)
{
h=p;
}
else
{
q=h;
while(q->next!=NULL)
q=q->next;
q->next=p;
}
t++;
}

void addAt(int r)
{
q=h;
while(q->rn!=r || q==NULL)
q=q->next;
if(q->rn==r)
{
clrscr();
p =(stud *)malloc(sizeof(stud));
printf("\nEnter first name of Student : ");
scanf("%s",&p->in);
printf("\nEnter the Last Name of Student : ");
scanf("%s",&p->na);
printf("\nEnter the ID of Student : ");
scanf("%d",&p->id);
printf("\nEnter the Roll No. of Student : ");
scanf("%d",&p->rn);
printf("\nEnter the Address of Student : ");
scanf("%s",&p->add);
printf("\nEnter the D.O.B. of
Student(dd/mm/yyyy) : ");
scanf("%s",&p->d);
p->next=q->next;
q->next=p;
t++;
}
else
{
printf("\n\nRecord Not Found.");
}
}


void delAt(int r)
{
q=h;
while((q->next)->rn!=r || q==NULL)
q=q->next;
if((q->next)->rn==r)
{
q->next=(q->next)->next;
printf("\n\nRecord Deleted.");
t--;
}
else
printf("\n\nRecord Not Found.");
}

void modAt(int id)
{
q=h;
int ch;
while(q->id!=id && q!=NULL)
q=q->next;
if(q->id==id)
{
clrscr();
printf("*** MODIFY ***\n1.First name\n2.Last
Name");
printf("\n3.Roll No.\n4.Add\n5.D.O.B.:\nEnter
choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n\nEnter first name of
Student : ");
scanf("%s",&q->in);break;
case 2 : printf("\nEnter the Last Name of
Student : ");
scanf("%s",&q->na);break;
case 3 : printf("\nEnter the Roll No. of
Student : ");
scanf("%d",&q->rn);break;
case 4 : printf("\nEnter the Address of
Student : ");
scanf("%s",&q->add);break;
case 5 : printf("\nEnter the D.O.B. of
Student(dd/mm/yyyy) : ");
scanf("%s",&q->d);break;
}
}
else
printf("\nRecord not Found.");
}

void search(char ni[5])
{
int flag=0;
q=h;
clrscr();
while(q!=NULL)
{
if(stricmp(q->in,ni)==0)
{
flag=1;
printf("\n\nInitals of Student : %s ",q-
>in);
printf("\n\nLast Name of Student : %s ",q-
>na);
printf("\n\nID of Student : %d ",q-
>id);
printf("\n\nRoll No. of Student : %d",q-
>rn);
printf("\n\nAddress of Student : %s",q-
>add);
printf("\n\nD.O.B. of Student : %s",q->d);
printf("\n\n\n");
}
q=q->next;
}
if(flag==0)
printf("\n\nNo Match Found.");
}

void sort()
{
p=h;
while(p!=NULL)
{
q=h;
while(q!=NULL)
{
if(stricmp(q->in,(q->next)->in)>0)
{
strcpy(t->in,q->in);
strcpy(t->na,q->na);
t->id=q->id;
t->rn=q->rn;
t->ph=q->ph;
strcpy(t->add,q->add);
strcpy(t->d,q->d);


strcpy(q->in,(q->next)->in);
strcpy(q->na,(q->next)->na);
q->id= (q->next)->id;
q->rn= (q->next)->rn;
q->ph= (q->next)->ph;
strcpy(q->add,(q->next)->add);
strcpy(q->d,(q->next)->d);

strcpy((q->next)->in,t->in);
strcpy((q->next)->na,t->na);
(q->next)->id = t->id;
(q->next)->rn = t->rn;
(q->next)->ph = t->ph;
strcpy((q->next)->add,t->add);
strcpy((q->next)->d,t->d);

}
q=q->next;
}
p=p->next;
}

}

void disp()
{
p=h;
clrscr();
while(p!=NULL)
{
printf("\nInitals of Student : %s ",p->in);
printf("\nName of Student : %s ",p->na);
printf("\nID of Student : %d ",p->id);
printf("\nRoll No. of Student : %d",p->rn);
printf("\nAddress of Student : %s",p-
>add);
printf("\nD.O.B. of Student : %s",p->d);
printf("\n\n");
p=p->next;
}
}

void main()
{
int ch=0,r;
char ni[5];
clrscr();
while(ch!=8)
{
clrscr();
printf("1.Add the Record.\n\n2.Delete
Record.");
printf("\n\n3.Modify Record.\n\n4.Search
Record.\n\n5.Sort Records.");
printf("\n\n6.Display\n\n8.Press 8 to Exit");
printf("\n\nEnter the Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
break;

case 2:
printf("\nEnter the Roll No. : ");
scanf("%d",&r);
delAt(r);
break;

case 3:
printf("\nEnter the ID : ");
scanf("%d",&r);
modAt(r);
break;

case 4:
printf("\nEnter the Initials : ");
scanf("%s",&ni);
search(ni);
break;

case 5:
sort();
printf("\n\nSorted");
break;
case 6:
disp();
break;
}
getch();
}
getch();
}

最佳答案

首先检查head是否是需要删除的item,具体操作如下:

if(q->rn == r && q == h){
h = h->next;
delete q;
q = h;
continue;
}

然后检查除 head 之外的其他节点是否匹配,然后使用以下方法删除该项目:

if((q->next->rn == r)
{
temp = q->next;
q->next=q->next->next;
printf("\n\nRecord Deleted.");
delete temp;
}

完整的删除函数如下:

void delAt(int r)
{
stud* temp;
q=h;

while(q!=NULL) {
if(q->rn == r && q == h){
h = h->next;
delete q;
q = h;
continue;
}

if((q->next->rn == r)
{
temp = q->next;
q->next=q->next->next;
printf("\n\nRecord Deleted.");
delete temp;
}
else
printf("\n\nRecord Not Found.");

q=q->next;
}
}

也不需要读取字符串&。例如:

scanf("%s",&p->in);

不正确。它将是:

scanf("%s", p->in);// without &

对于排序函数,在比较期间添加一个 NULL 检查:

if(q->next != NULL && stricmp(q->in,(q->next)->in)>0)

排序功能的完整代码为:

void sort()
{

p=h;
while(p!=NULL)
{
q=h;
while(q!=NULL)
{
if(q->next != NULL && stricmp(q->in,(q->next)->in)>0)
{
strcpy(t->in,q->in);
strcpy(t->na,q->na);
t->id=q->id;
t->rn=q->rn;
t->ph=q->ph;
strcpy(t->add,q->add);
strcpy(t->d,q->d);


strcpy(q->in,(q->next)->in);
strcpy(q->na,(q->next)->na);
q->id= (q->next)->id;
q->rn= (q->next)->rn;
q->ph= (q->next)->ph;
strcpy(q->add,(q->next)->add);
strcpy(q->d,(q->next)->d);

strcpy((q->next)->in,t->in);
strcpy((q->next)->na,t->na);
(q->next)->id = t->id;
(q->next)->rn = t->rn;
(q->next)->ph = t->ph;
strcpy((q->next)->add,t->add);
strcpy((q->next)->d,t->d);

}
q=q->next;
}
p=p->next;
}

}

关于c - 学生管理系统使用链表删除部分出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51583512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com