链接列表不打印列表

发布于 2024-09-17 23:52:05 字数 4283 浏览 3 评论 0原文

我制作了以下链接列表,但不打印列表。打印记录功能不起作用。很抱歉代码很长。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
    {
     char *name;
     int roll_no;
     struct student *ptr_next;
    }*ptr_this,*ptr_first;
void PrintMenu(void);
void AddRecord(void);
void DeleteRecord(void);
void SearchRecord(void);
void SortRecord(void);
void PrintRecord(void);
int main()
{
    char choice;
    ptr_this=ptr_first=(struct student*)NULL;
    while(1)
    {
        PrintMenu();
        choice=getche();
        switch(choice)
        {
            case '1':
            AddRecord();
            break;
            case '2':
            DeleteRecord();
            break;
            case '3':
            SearchRecord();
            break;
            case '4':
            SortRecord();
            break;
            case '5':
            PrintRecord();
            break;
            case '6':
            exit(0);
            default:
            printf("Enter a valid choice.\n");
            getch();
        }
    }

}
void PrintMenu(void)
{
    clrscr();
    printf("Database System.\n1.Add Record\n2.Delete Record\n3.Search Record \n4.Sort Records\n5.Display Records\n6.exit");

}
void AddRecord(void)
{
    char temp[100];
    int i;
    struct student *ptr_new;
    ptr_new=(struct student*)malloc(sizeof(struct student));
    if(ptr_new==(struct student*)NULL)
    {
         printf("Sorry but you can not add any more data becasue the computer memory is full.\n");
         return;
    }
    printf("Enter Name:\n");
    gets(temp);
    ptr_new->name=(char*)malloc(sizeof(char)*(strlen(temp)+1));
    strncpy(ptr_new->name,temp,strlen(temp)+1);
    printf("Enter Roll no:");
    gets(temp);
    ptr_new->roll_no=atoi(temp);
    ptr_new->ptr_next=(struct student*)NULL;
    if(ptr_first->ptr_next==(struct student*)NULL)
    {
        ptr_first=ptr_this=ptr_new;
    }
    else
    {
        ptr_this->ptr_next=ptr_new;
        ptr_this=ptr_new;
    }
    printf("Record Successfully added.\n");
    printf("%s",ptr_this->name);
    getch();
}

void DeleteRecord(void)
{
    char temp[100];
    int rec_no,i;
    struct student* ptr_del,*ptr_prev;
    printf("Enter Record Number to be Deleted:\n");
    gets(temp);
    rec_no=atoi(temp);
    ptr_del=ptr_prev=ptr_first;
    for(i=0;ptr_del!=(struct student*)NULL;i++,ptr_del=ptr_del->ptr_next)
    {
        if(i==rec_no)
        {
            if(ptr_del==ptr_first)
                ptr_first=ptr_first->ptr_next;
            else
                ptr_prev->ptr_next=ptr_del->ptr_next;
            free(ptr_del);
            printf("Record #%d has been deleted.",rec_no);
            getch();
            return ;
        }
        ptr_prev=ptr_del;
    }


}
void SearchRecord(void)
{
    struct student *ptr_search;
    char temp[100];
    int roll,flag=0;
    clrscr();
    printf("Enter roll no to search:\n");
    gets(temp);
    roll=atoi(temp);
    ptr_search=ptr_first;
    for(;ptr_search!=(struct student*)NULL;ptr_search=ptr_search->ptr_next)
    {
        if(ptr_search->roll_no==roll)
        {
        flag=1;
        printf("Result Found!\nName:\t%s\nRoll #:\t%d\n",ptr_search->name,ptr_search->roll_no);
        getch();
        return;
        }
    }
    if(flag==0)
    printf("Record not found!\n");
    getch();

}
void SortRecord(void)
{
    struct student *out,*in,*dummy;
    for(out=ptr_first;out!=(struct student*)NULL;out=out->ptr_next)
    {
        for(in=out->ptr_next;out->ptr_next!=(struct student*)NULL;in=in->ptr_next)
        {
            if(!(strcmpi(out->name,in->name)))
            *dummy=*in;
            *in=*out;
            *out=*dummy;
            dummy=in;
            in=out;
            out=dummy;
        }
    }
    printf("Records have been successfully sorted.");
}
void PrintRecord(void)
{
    printf("HEllo");
    getch();
    struct student *ptr_print;
    ptr_print=ptr_first;
    for(;ptr_print!=(struct student*)NULL;ptr_print=ptr_print->ptr_next)
    {
        printf("NAME:\t%s\nROLL#:\t%d\n",ptr_print->name,ptr_print->roll_no);
        printf("Press Enter to display next record or space to exit");
        getch();
    }


}

I have made the following linked list which is not printing the list .The printrecord function is not working.Sorry for the long code.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
    {
     char *name;
     int roll_no;
     struct student *ptr_next;
    }*ptr_this,*ptr_first;
void PrintMenu(void);
void AddRecord(void);
void DeleteRecord(void);
void SearchRecord(void);
void SortRecord(void);
void PrintRecord(void);
int main()
{
    char choice;
    ptr_this=ptr_first=(struct student*)NULL;
    while(1)
    {
        PrintMenu();
        choice=getche();
        switch(choice)
        {
            case '1':
            AddRecord();
            break;
            case '2':
            DeleteRecord();
            break;
            case '3':
            SearchRecord();
            break;
            case '4':
            SortRecord();
            break;
            case '5':
            PrintRecord();
            break;
            case '6':
            exit(0);
            default:
            printf("Enter a valid choice.\n");
            getch();
        }
    }

}
void PrintMenu(void)
{
    clrscr();
    printf("Database System.\n1.Add Record\n2.Delete Record\n3.Search Record \n4.Sort Records\n5.Display Records\n6.exit");

}
void AddRecord(void)
{
    char temp[100];
    int i;
    struct student *ptr_new;
    ptr_new=(struct student*)malloc(sizeof(struct student));
    if(ptr_new==(struct student*)NULL)
    {
         printf("Sorry but you can not add any more data becasue the computer memory is full.\n");
         return;
    }
    printf("Enter Name:\n");
    gets(temp);
    ptr_new->name=(char*)malloc(sizeof(char)*(strlen(temp)+1));
    strncpy(ptr_new->name,temp,strlen(temp)+1);
    printf("Enter Roll no:");
    gets(temp);
    ptr_new->roll_no=atoi(temp);
    ptr_new->ptr_next=(struct student*)NULL;
    if(ptr_first->ptr_next==(struct student*)NULL)
    {
        ptr_first=ptr_this=ptr_new;
    }
    else
    {
        ptr_this->ptr_next=ptr_new;
        ptr_this=ptr_new;
    }
    printf("Record Successfully added.\n");
    printf("%s",ptr_this->name);
    getch();
}

void DeleteRecord(void)
{
    char temp[100];
    int rec_no,i;
    struct student* ptr_del,*ptr_prev;
    printf("Enter Record Number to be Deleted:\n");
    gets(temp);
    rec_no=atoi(temp);
    ptr_del=ptr_prev=ptr_first;
    for(i=0;ptr_del!=(struct student*)NULL;i++,ptr_del=ptr_del->ptr_next)
    {
        if(i==rec_no)
        {
            if(ptr_del==ptr_first)
                ptr_first=ptr_first->ptr_next;
            else
                ptr_prev->ptr_next=ptr_del->ptr_next;
            free(ptr_del);
            printf("Record #%d has been deleted.",rec_no);
            getch();
            return ;
        }
        ptr_prev=ptr_del;
    }


}
void SearchRecord(void)
{
    struct student *ptr_search;
    char temp[100];
    int roll,flag=0;
    clrscr();
    printf("Enter roll no to search:\n");
    gets(temp);
    roll=atoi(temp);
    ptr_search=ptr_first;
    for(;ptr_search!=(struct student*)NULL;ptr_search=ptr_search->ptr_next)
    {
        if(ptr_search->roll_no==roll)
        {
        flag=1;
        printf("Result Found!\nName:\t%s\nRoll #:\t%d\n",ptr_search->name,ptr_search->roll_no);
        getch();
        return;
        }
    }
    if(flag==0)
    printf("Record not found!\n");
    getch();

}
void SortRecord(void)
{
    struct student *out,*in,*dummy;
    for(out=ptr_first;out!=(struct student*)NULL;out=out->ptr_next)
    {
        for(in=out->ptr_next;out->ptr_next!=(struct student*)NULL;in=in->ptr_next)
        {
            if(!(strcmpi(out->name,in->name)))
            *dummy=*in;
            *in=*out;
            *out=*dummy;
            dummy=in;
            in=out;
            out=dummy;
        }
    }
    printf("Records have been successfully sorted.");
}
void PrintRecord(void)
{
    printf("HEllo");
    getch();
    struct student *ptr_print;
    ptr_print=ptr_first;
    for(;ptr_print!=(struct student*)NULL;ptr_print=ptr_print->ptr_next)
    {
        printf("NAME:\t%s\nROLL#:\t%d\n",ptr_print->name,ptr_print->roll_no);
        printf("Press Enter to display next record or space to exit");
        getch();
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韬韬不绝 2024-09-24 23:52:05

打印功能很好,但这是可疑的:

if(ptr_first->ptr_next==(struct Student*)NULL)
{
    ptr_first=ptr_this=ptr_new;
}
别的
{
    ptr_this->ptr_next=ptr_new;
    ptr_this=ptr_new;
}

我想说的条件应该是

if(ptr_first==NULL)

不确定为什么你在这个地方没有出现段错误......

The print function is fine, but this is suspicious:

if(ptr_first->ptr_next==(struct student*)NULL)
{
    ptr_first=ptr_this=ptr_new;
}
else
{
    ptr_this->ptr_next=ptr_new;
    ptr_this=ptr_new;
}

I'd say the condition should read

if(ptr_first==NULL)

not sure why you didn't get segfaults in this place...

感受沵的脚步 2024-09-24 23:52:05

当您尝试添加消息时,您的代码应该崩溃(ptr_first 始终为 null)。如果您不调用 AddRecord,那么 PrintRecord 不会输出任何内容,这并不奇怪 - ptr_first 仍然为 null。

Your code should crash when you are trying to add a message (ptr_first is always null). If you don't call AddRecord it is not surprising that PrintRecord outputs nothing - ptr_first is still null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文