链接列表中的添加和删除功能发生故障

时间:2018-07-31 10:57:27

标签: c function linked-list

我想创建一个使用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++;
    }
}

1 个答案:

答案 0 :(得分:0)

发布的代码无法完全编译!请更正并重新发布。

这是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];
      ^~

编译失败。