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



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

有人能帮我吗?

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

#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("nnRecord 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("nnRecord Deleted.");
t--;
}
else
printf("nnRecord 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 namen2.Last 
Name");
printf("n3.Roll No.n4.Addn5.D.O.B.:nEnter 
choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 :  printf("nnEnter 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("nnInitals of Student :   %s ",q- 
>in);
printf("nnLast Name of Student : %s ",q- 
>na);
printf("nnID of Student :        %d ",q- 
>id);
printf("nnRoll No. of Student :  %d",q- 
>rn);
printf("nnAddress of Student :   %s",q- 
>add);
printf("nnD.O.B. of Student :    %s",q->d);
printf("nnn");
}
q=q->next;
}
if(flag==0)
printf("nnNo 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("nn");
p=p->next;
}  
} 
void main()
{
int ch=0,r;
char ni[5];
clrscr();
while(ch!=8)
{
clrscr();
printf("1.Add the Record.nn2.Delete 
Record.");
printf("nn3.Modify Record.nn4.Search 
Record.nn5.Sort Records.");
printf("nn6.Displaynn8.Press 8 to Exit");
printf("nnEnter 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("nnSorted");
break;
case 6:
disp();
break;
}
getch();
} 
getch();  
}

首先检查标题是否是需要删除的项目,这是由完成的

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("nnRecord 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("nnRecord Deleted.");
delete temp;
}
else
printf("nnRecord 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;
}
}

相关内容

  • 没有找到相关文章

最新更新