我用链表为学生管理系统写了这段代码,但它的排序和删除节点部分不起作用。
有人能帮我吗?
它插入节点,修改它,并完美地搜索它。但是,当涉及到删除和排序时,一旦我们选择了这样做的选项,程序就会挂起。
#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();
}发布于 2018-07-30 08:52:14
首先检查头部是否是需要删除的项,具体方法是:
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;
}
}https://stackoverflow.com/questions/51583512
复制相似问题