c-链表中的添加和删除功能出现故障



我想用C中的链表创建一个管理系统,它存储学生记录,这样就可以搜索和删除。我在devC编辑器上编程。

除了delAt函数外,我的所有函数都在工作。每当我执行这个函数时,我的程序就会挂起。另一个问题是,如果我想多次执行addAt函数,这意味着如果我多次选择"选项1",程序将挂起。虽然当我只选择"选项1"一次时,它不会挂起。

以下是代码:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<dos.h>
struct stud
{
int rn, id, ph[15];
char add[30], na[20], d[15], in[5];
struct stud *next;
} *h = NULL, *p, *q, *t, *ts;
void add()
{
p =(struct stud*)malloc(sizeof(struct stud*));
printf("nEnter the Initials 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 Ph No. of Student : ");
scanf("%d", &p->ph);
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;
}
ts++;
}
void delAt(int r)
{
q=h;
r=x;
if (q == NULL)
{
printf("list is empty");
}
while (q->rn != r - 1)
{
q = q->next;
}
p = q->next;
q->next = p->next;
free(p);
printf("nnRecord Deleted.");
}
void main()
{
int ch = 0, r;
char ni[5];
while(ch != 8)
{
printf("1.Add the Record.nn2.Add Record at Locn.nn3.Delete Record.");
printf("nn4.Modify Record.nn5.Search Record.nn6.Sort Records.");
printf("nn7.Displaynn8.Exit");
printf("nnEnter the Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
break;
case 3:
printf("nEnter the Roll No. : ");
scanf("%d",&r);
delAt(r);
break;
case 7:
disp();
break;
}
ch++;
}
}

发布的代码没有干净地编译!请更正并转发。

以下是C编译器输出的消息列表:

gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" (in directory: /home/richard/Documents/forum)
untitled.c: In function ‘add’:
untitled.c:18:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[5]’ [-Wformat=]
scanf("%s", &p->in);
~^   ~~~~~~
untitled.c:20:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat=]
scanf("%s", &p->na);
~^   ~~~~~~
untitled.c:26:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[15]’ [-Wformat=]
scanf("%d", &p->ph);
~^   ~~~~~~
untitled.c:28:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat=]
scanf("%s", &p->add);
~^   ~~~~~~~
untitled.c:30:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[15]’ [-Wformat=]
scanf("%s", &p->d);
~^   ~~~~~
untitled.c: In function ‘delAt’:
untitled.c:53:7: error: ‘x’ undeclared (first use in this function)
r=x;
^
untitled.c:53:7: note: each undeclared identifier is reported only once for each function it appears in
untitled.c: At top level:
untitled.c:70:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main()
^~~~
untitled.c: In function ‘main’:
untitled.c:94:17: warning: implicit declaration of function ‘disp’; did you mean ‘div’? [-Wimplicit-function-declaration]
disp();
^~~~
div
untitled.c:73:10: warning: unused variable ‘ni’ [-Wunused-variable]
char ni[5];
^~

编译失败。

相关内容

  • 没有找到相关文章

最新更新