gpt4 book ai didi

c - 链表添加删除功能失效

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

我想用C语言制作一个使用链表的管理系统,它存储学生的记录,以便可以查找和删除。我正在 devC 编辑器上编程。

除了 delAt 函数之外,我的所有函数都正常工作。每当我执行这个函数时,我的程序就会挂起。另一个问题是,如果我想多次执行 addAt 函数,这意味着如果我多次选择“选项 1”,程序就会挂起。尽管当我仅选择“选项 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("\n\nRecord Deleted.");
}

void main()
{
int ch = 0, r;
char ni[5];
while(ch != 8)
{
printf("1.Add the Record.\n\n2.Add Record at Locn.\n\n3.Delete Record.");
printf("\n\n4.Modify Record.\n\n5.Search Record.\n\n6.Sort Records.");
printf("\n\n7.Display\n\n8.Exit");
printf("\n\nEnter 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];
^~

编译失败。

关于c - 链表添加删除功能失效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51611774/

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