Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用链表删除学生管理系统的部分时出错_C_Linked List - Fatal编程技术网

C 使用链表删除学生管理系统的部分时出错

C 使用链表删除学生管理系统的部分时出错,c,linked-list,C,Linked List,我已经为使用链表的学生管理系统编写了此代码,但其排序和删除节点部分不起作用 有人能帮我吗 它插入节点,修改它,并完美地搜索它。但是,当涉及到删除和排序时,只要我们选择该选项,程序就会挂起 #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #include<dos.h> struct stud { int rn,id,ph; char a

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

有人能帮我吗

它插入节点,修改它,并完美地搜索它。但是,当涉及到删除和排序时,只要我们选择该选项,程序就会挂起

#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();  
  }
#包括
#包括
#包括
#包括
#包括
结构螺柱
{
int-rn,id,ph;
在[10]中添加[30],na[20],d[15];
结构螺栓*下一步;
}*h=NULL,*p,*q,*t;
void add()
{
clrsc();
p=(螺柱*)malloc(螺柱大小);
printf(“\n输入学生的名字:”);
扫描频率(“%s”和&p->in);
printf(“\n输入学生的姓:”);
scanf(“%s”,&p->na);
printf(“\n请输入学生的ID,不要超过
不超过5位:“);
scanf(“%d”,&p->id);
printf(“\n输入学生卷号:”);
scanf(“%d”,&p->rn);
printf(“\n输入学生地址:”);
scanf(“%s”,&p->add);
printf(“\n输入
学生(年月日):“”;
scanf(“%s”和&p->d);
p->next=NULL;
if(h==NULL)
{
h=p;
}
其他的
{
q=h;
while(q->next!=NULL)
q=q->next;
q->next=p;
}
t++;
}
无效地址(整数r)
{
q=h;
而(q->rn!=r | | q==NULL)
q=q->next;
如果(q->rn==r)
{
clrsc();
p=(螺柱*)malloc(螺柱大小);
printf(“\n输入学生的名字:”);
扫描频率(“%s”和&p->in);
printf(“\n输入学生的姓:”);
scanf(“%s”,&p->na);
printf(“\n输入学生ID:”);
scanf(“%d”,&p->id);
printf(“\n输入学生卷号:”);
scanf(“%d”,&p->rn);
printf(“\n输入学生地址:”);
scanf(“%s”,&p->add);
printf(“\n输入
学生(年月日):“”;
scanf(“%s”和&p->d);
p->next=q->next;
q->next=p;
t++;
}
其他的
{
printf(“\n\n未找到记录”);
}
}
虚空三角形(内部r)
{
q=h;
while((q->next)->rn!=r | | q==NULL)
q=q->next;
如果((q->next)->rn==r)
{
q->next=(q->next)->next;
printf(“\n\n记录已删除”);
t--;
}
其他的
printf(“\n\n未找到记录”);
}
void modAt(内部id)
{
q=h;
int-ch;
而(q->id!=id&&q!=NULL)
q=q->next;
如果(q->id==id)
{
clrsc();
printf(“***修改***\n1.名字\n2.姓氏
名称“);
printf(“\n3.卷号。\n4.添加\n5.D.O.B.:\n输入
选择:”;
scanf(“%d”和“ch”);
开关(ch)
{
案例1:printf(“\n\n输入
学生:);
扫描(“%s”,&q->in);中断;
案例2:printf(“\n输入
学生:);
scanf(“%s”,&q->na);中断;
案例3:printf(“\n输入卷号
学生:);
scanf(“%d”,&q->rn);中断;
案例4:printf(“\n输入
学生:);
扫描(“%s”,&q->add);中断;
案例5:printf(“\n输入
学生(年月日):“”;
scanf(“%s”,&q->d);中断;
}
}
其他的
printf(“\n未找到记录”);
}
无效搜索(char ni[5])
{
int标志=0;
q=h;
clrsc();
while(q!=NULL)
{
if(stricmp(q->in,ni)==0)
{
flag=1;
printf(“\n\n学生基本资料:%s”,q-
>在),;
printf(“\n\n学生的上一个名字:%s”,q-
>na);
printf(“\n\nID学生:%d”,q-
>身份证);
printf(“\n\n学生学号:%d”,q-
>rn);
printf(“\n\n学生地址:%s”,q-
>添加);
printf(“\n\n学生的毕业证书:%s”,q->d);
printf(“\n\n\n”);
}
q=q->next;
}
如果(标志==0)
printf(“\n\n未找到匹配项”);
}
空排序()
{
p=h;
while(p!=NULL)
{
q=h;
while(q!=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;
}
}  
void disp()
{
p=h;
clrsc();
while(p!=NULL)
{
printf(“\n学生基本信息:%s”,p->in);
printf(“\n学生姓名:%s”,p->na);
printf(“\nID学生:%d”,p->id);
printf(“\n学生学号:%d”,p->rn);
printf(“\n学生地址:%s”,p-
>添加);
printf(“\n学生的毕业证书:%s”,p->d);
printf(“\n\n”);
p=p->next;
}  
} 
void main()
{
int ch=0,r;
char-ni[5];
clrsc();
while(ch!=8)
{
clrsc();
printf(“1.添加记录。\n\n2.删除
记录。”);
printf(“\n\n3.修改记录。\n\n4.搜索
记录。\n\n5.对记录进行排序。“);
printf(“\n\n6.显示\n\n8.按8退出”);
printf(“\n\n输入选项:”);
scanf(“%d”和“ch”);
开关(ch)
{
案例1:
添加();
打破
案例2:
printf(“\n输入卷号:”);
scanf(“%d”、&r);
德拉特(r);
打破
案例3:
printf(“\n输入ID:”);
scanf(“%d”、&r);
modAt(r);
打破
案例4:
printf(“\n输入首字母:”);
扫描频率(“%s”和“ni”);
搜索(ni);
打破
案例5:
排序();
printf(“\n\n被选中”);
打破
案例6:
disp();
打破
}
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("\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;
    }

}

您是否使用了调试器逐行地检查代码并查看它挂起的位置?此外,使用比一个字母更好的命名变量会使其他人更容易阅读,并且可以很好地缩进代码。请决定缩进方案并一致地应用它。谢谢您的更正。我真的很感激。我能再要求一点帮助吗?它的排序功能也不起作用了?你也能检查一下吗?