双重链接列表FM站实施

发布于 2025-01-20 03:39:27 字数 4024 浏览 1 评论 0原文

问题是:

使用C 创建一个FM 电台(下面提到)的双向链表。然后将指针设置为第一个电台。 接受用户输入: 输入1播放下一站 输入2播放上一电台 输入3播放第一站 输入 4 退出

FM 电台相关数据的链接列表应为:

Station 1 Radio_City 91.1 2号电台Radio_One 91.8 3号站 BIG_FM 92.7 电台 4 RED_FM 93.5 5 站 My_FM 94.3 6 站 Hit_FM 95.0 7号电台电台_Mirchi 98.3 8号站 Vividh_Bharti 100.1 9号站 吉安_瓦尼 105.6 电台 10_Dhamal 106.4 11站DD_新闻108.2
Station 12 Gyan_Ganga 109.6

我编写的代码是:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
    int sno;
    char sname[50];
    float sfreq;
    struct Node *Lnext,*Rnext;
};

struct Node *createnewnode()
{
    int n;
    struct Node *first,*last;
    printf("\n Enter the number of stations available:");
    scanf("%d",&n);
    first=(struct Node*)malloc(sizeof(struct Node));
    printf("\nEnter the Station number: ");
    scanf("%d",&first->sno);
    printf("Enter the Station Name: ");
    fflush(stdin);
    scanf("%s",first->sname);
    printf("Enter the station Frequency: ");
    scanf("%f",&first->sfreq);
    last=first;
    last->Lnext=NULL;
    last->Rnext=NULL;
  
    int i=2;
    while(i<=n)
    {
        struct Node *temp=(struct Node*)malloc(sizeof(struct Node));
        printf("\nEnter the Station number: ");
        scanf("%d",&temp->sno);
        printf("Enter the station name: ");
        fflush(stdin);
        scanf("%s",temp->sname);
        printf("Enter the station frequency: ");
        scanf("%f",&temp->sfreq);
        
        temp->Lnext=last;
        last->Rnext=temp;
        last=last->Rnext;
        last->Rnext=NULL;
        
        i++;
    }
    return first;
}

void display_by_position(struct Node *first)
{
    struct Node *temp=first;
    printf("\n The list of stations is....\n");
    if(temp==NULL)
    {
        printf("\nThere are no stations to display");
        return;
    }
    else
    {
        printf("\n\tSTATION NUMBER\t\tSTATION NAME\t\tSTATION FREQUENCY");
        for(temp=first;temp!=NULL;temp=temp->Rnext)
        {
            printf("\n\t%d\t\t\t%s\t\t\t%.1f",temp->sno,temp->sname,temp->sfreq);
        }
    }
    
    int choice;
    while(choice!=4)
    {
        printf("\n-------------------------------------------\n");
        printf("\nEnter \"1\" to play the next station");
        printf("\nEnter \"2\" to play the previous station");
        printf("\nEnter \"3\" to play the first station");
        printf("\nEnter \"4\" to exit");
        printf("\n-------------------------------------------\n");
        printf("\nEnter your choice: ");
        scanf("%d",&choice);
        
        if(choice==1)
        {
            if(temp->Rnext==NULL)
            printf("\nThis is the last station\n");
            else
            {
                temp=temp->Rnext;
                printf("\n The next station is...\n");
                printf("\nStationNumber:  %d      %s      %f\n",temp->sno,temp->sname,temp->sfreq);
            }
        }
        
        else if(choice==2)
        {
            if(temp->Lnext==NULL)
            printf("\nThe is the first station");
            else
            {
                temp=temp->Lnext;
                printf(" \n The previous station is...\n");
                printf("\nStationNumber:  %d      %s      %f\n",temp->sno,temp->sname,temp->sfreq);
            }
        }
        
        else if(choice==3)
        {
            temp=first;
            printf("\n The first station in the list is...\n");
            printf("\nStationNumber:  %d      %s      %f\n",temp->sno,temp->sname,temp->sfreq);
        }
        
        else if(choice==4)
        {
            printf("\n Thank you for your patience...");
        }
        
        else
        {
            printf("\n Invalid choice...");
        }
    }
}

int main()
{
    struct Node *p=createnewnode();
    display_by_position(p);
    return 0;
}

在这段代码中,当用户输入“1”播放下一站时,程序将终止。类似地,无论用户提供什么输入,程序都会终止

The Question is:

Create a bidirectional linked list of FM stations (mentioned below) using C. Then set pointer to first station.
Take user input:
Enter 1 to play next station
Enter 2 to play previous station
Enter 3 to play first station
Enter 4 to exit

The linked list for FM station related data should be:

Station 1 Radio_City 91.1
Station 2 Radio_One 91.8
Station 3 BIG_FM 92.7
Station 4 RED_FM 93.5
Station 5 My_FM 94.3
Station 6 Hit_FM 95.0
Station 7 Radio_Mirchi 98.3
Station 8 Vividh_Bharti 100.1
Station 9 Gyan_Vani 105.6
Station 10 Radio_Dhamal 106.4
Station 11 DD_News 108.2
Station 12 Gyan_Ganga 109.6

The code that I have written is:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node
{
    int sno;
    char sname[50];
    float sfreq;
    struct Node *Lnext,*Rnext;
};

struct Node *createnewnode()
{
    int n;
    struct Node *first,*last;
    printf("\n Enter the number of stations available:");
    scanf("%d",&n);
    first=(struct Node*)malloc(sizeof(struct Node));
    printf("\nEnter the Station number: ");
    scanf("%d",&first->sno);
    printf("Enter the Station Name: ");
    fflush(stdin);
    scanf("%s",first->sname);
    printf("Enter the station Frequency: ");
    scanf("%f",&first->sfreq);
    last=first;
    last->Lnext=NULL;
    last->Rnext=NULL;
  
    int i=2;
    while(i<=n)
    {
        struct Node *temp=(struct Node*)malloc(sizeof(struct Node));
        printf("\nEnter the Station number: ");
        scanf("%d",&temp->sno);
        printf("Enter the station name: ");
        fflush(stdin);
        scanf("%s",temp->sname);
        printf("Enter the station frequency: ");
        scanf("%f",&temp->sfreq);
        
        temp->Lnext=last;
        last->Rnext=temp;
        last=last->Rnext;
        last->Rnext=NULL;
        
        i++;
    }
    return first;
}

void display_by_position(struct Node *first)
{
    struct Node *temp=first;
    printf("\n The list of stations is....\n");
    if(temp==NULL)
    {
        printf("\nThere are no stations to display");
        return;
    }
    else
    {
        printf("\n\tSTATION NUMBER\t\tSTATION NAME\t\tSTATION FREQUENCY");
        for(temp=first;temp!=NULL;temp=temp->Rnext)
        {
            printf("\n\t%d\t\t\t%s\t\t\t%.1f",temp->sno,temp->sname,temp->sfreq);
        }
    }
    
    int choice;
    while(choice!=4)
    {
        printf("\n-------------------------------------------\n");
        printf("\nEnter \"1\" to play the next station");
        printf("\nEnter \"2\" to play the previous station");
        printf("\nEnter \"3\" to play the first station");
        printf("\nEnter \"4\" to exit");
        printf("\n-------------------------------------------\n");
        printf("\nEnter your choice: ");
        scanf("%d",&choice);
        
        if(choice==1)
        {
            if(temp->Rnext==NULL)
            printf("\nThis is the last station\n");
            else
            {
                temp=temp->Rnext;
                printf("\n The next station is...\n");
                printf("\nStationNumber:  %d      %s      %f\n",temp->sno,temp->sname,temp->sfreq);
            }
        }
        
        else if(choice==2)
        {
            if(temp->Lnext==NULL)
            printf("\nThe is the first station");
            else
            {
                temp=temp->Lnext;
                printf(" \n The previous station is...\n");
                printf("\nStationNumber:  %d      %s      %f\n",temp->sno,temp->sname,temp->sfreq);
            }
        }
        
        else if(choice==3)
        {
            temp=first;
            printf("\n The first station in the list is...\n");
            printf("\nStationNumber:  %d      %s      %f\n",temp->sno,temp->sname,temp->sfreq);
        }
        
        else if(choice==4)
        {
            printf("\n Thank you for your patience...");
        }
        
        else
        {
            printf("\n Invalid choice...");
        }
    }
}

int main()
{
    struct Node *p=createnewnode();
    display_by_position(p);
    return 0;
}

In this code, the program gets terminated when the user gives input as '1' to play the next station. Similarly, the program gets terminated for whatever input the user gives

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

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

发布评论

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

评论(1

讽刺将军 2025-01-27 03:39:27

列表:

               for(temp=first;temp!=NULL;temp=temp->Rnext)
                  {
                          printf("\n\t%d\t\t\t%s\t\t\t%.1f",temp->sno,temp->sname,temp->sfreq);
                  }

当您使用struct Node *temp = first;初始化temp时,您随后使用temp作为循环变量,以显示站点 表示temp == null当循环退出时。随后按“ 1”时,它会进行temp-&gt; rnext,它将放置空指针和程序崩溃。

重复使用这样的变量时,您必须非常小心,因此在需要时创建局部变量通常要安全得多:

               for(struct Node *temp=first;temp!=NULL;temp=temp->Rnext)
                  {
                          printf("\n\t%d\t\t\t%s\t\t\t%.1f",temp->sno,temp->sname,temp->sfreq);
                  }

您应该将struct node *temp = first;移动到opeach之前/code> -loop,例如,在int选择之后;否则,您的编译器可能会抱怨变量正在被遮蔽。

将程序分解为较小的功能也将帮助您隔离程序的一部分,因此您只需要一次就每个部分进行推理。例如,您可以提取这样的站点列表:

int list(struct Node *first) {
    printf("\n The list of stations is....\n");
    if(!first) {
        printf("\nThere are no stations to display");
        return 1;
    }
    printf("\n\tSTATION NUMBER\t\tSTATION NAME\t\tSTATION FREQUENCY");
    for(struct Node *t=first; t!=NULL; t=t->Rnext) {
        printf("\n\t%d\t\t\t%s\t\t\t%.1f", t->sno, t->sname, t->sfreq);
    }
    return 0;
}

void display_by_position(struct Node *first) {
    if(list(first))
       return;
    ...
}

更改while(选择!= 4){...} to do {...} while(选择!= 4) ),因此您没有检查非初始化变量的值。

您正在用createNewnode()重复自己,所以我建议简化它:

struct Node *createnewnode() {
    int n;
    printf("\n Enter the number of stations available:");
    scanf("%d",&n);

    struct Node *first = NULL;
    struct Node *last = NULL;
    while(n-- > 0) {
        struct Node *temp = malloc(sizeof(struct Node));
        if(!temp) {
            printf("malloc failed\n");
            return NULL;
        }

        printf("\nEnter the Station number: ");
        scanf("%d", &temp->sno);

        printf("Enter the station name: ");
        scanf("%49s", temp->sname);

        printf("Enter the station frequency: ");
        scanf("%f", &temp->sfreq);

        temp->Lnext = last ? last : NULL;
        temp->Rnext = NULL;

        if(!first) first = temp;

        if(last) last->Rnext = temp;
        last = temp;
    }
    return first;
}

您可能需要检查scanf的返回代码,例如,如果您输入“ A”,则无法使用“ A”。您期望的方式:

While you initialize temp with struct Node *temp=first;, you subsequently use temp as a loop variable to show the list of stations:

               for(temp=first;temp!=NULL;temp=temp->Rnext)
                  {
                          printf("\n\t%d\t\t\t%s\t\t\t%.1f",temp->sno,temp->sname,temp->sfreq);
                  }

which means that temp == NULL when the loop exits. When you subsequently press '1' it does temp->Rnext which will dereference a NULL pointer and your program crashes.

You have to be really careful when reusing variables like this, so it's usually much safer to create a local variable when needed:

               for(struct Node *temp=first;temp!=NULL;temp=temp->Rnext)
                  {
                          printf("\n\t%d\t\t\t%s\t\t\t%.1f",temp->sno,temp->sname,temp->sfreq);
                  }

You should move struct Node *temp=first; to before your choice-loop, say, after int choice; otherwise your compiler might complain that variable is being shadowed.

Breaking your program into smaller functions will also help you isolate parts of your program so you only have to reason about each part instead of the entire program all at once. For example you could extract the list of stations like this:

int list(struct Node *first) {
    printf("\n The list of stations is....\n");
    if(!first) {
        printf("\nThere are no stations to display");
        return 1;
    }
    printf("\n\tSTATION NUMBER\t\tSTATION NAME\t\tSTATION FREQUENCY");
    for(struct Node *t=first; t!=NULL; t=t->Rnext) {
        printf("\n\t%d\t\t\t%s\t\t\t%.1f", t->sno, t->sname, t->sfreq);
    }
    return 0;
}

void display_by_position(struct Node *first) {
    if(list(first))
       return;
    ...
}

Change your while(choice != 4) { ... } to a do { ... } while(choice != 4) so you are not checking the value of an uninitialized variable.

You are repeating yourself in createnewnode() so is my suggestion to streamline it:

struct Node *createnewnode() {
    int n;
    printf("\n Enter the number of stations available:");
    scanf("%d",&n);

    struct Node *first = NULL;
    struct Node *last = NULL;
    while(n-- > 0) {
        struct Node *temp = malloc(sizeof(struct Node));
        if(!temp) {
            printf("malloc failed\n");
            return NULL;
        }

        printf("\nEnter the Station number: ");
        scanf("%d", &temp->sno);

        printf("Enter the station name: ");
        scanf("%49s", temp->sname);

        printf("Enter the station frequency: ");
        scanf("%f", &temp->sfreq);

        temp->Lnext = last ? last : NULL;
        temp->Rnext = NULL;

        if(!first) first = temp;

        if(last) last->Rnext = temp;
        last = temp;
    }
    return first;
}

You may want to check the return code of scanf, for example, if you enter "a" for station number it will not work the way you expect:

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