双重链接列表FM站实施
问题是:
使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
列表:
当您使用
struct Node *temp = first;
初始化temp
时,您随后使用temp
作为循环变量,以显示站点 表示temp == null
当循环退出时。随后按“ 1”时,它会进行temp-&gt; rnext
,它将放置空指针和程序崩溃。重复使用这样的变量时,您必须非常小心,因此在需要时创建局部变量通常要安全得多:
您应该将
struct node *temp = first;
移动到opeach之前/code> -loop,例如,在
int选择之后;
否则,您的编译器可能会抱怨变量正在被遮蔽。将程序分解为较小的功能也将帮助您隔离程序的一部分,因此您只需要一次就每个部分进行推理。例如,您可以提取这样的站点列表:
更改
while(选择!= 4){...}
todo {...} while(选择!= 4) )
,因此您没有检查非初始化变量的值。您正在用
createNewnode()
重复自己,所以我建议简化它:您可能需要检查scanf的返回代码,例如,如果您输入“ A”,则无法使用“ A”。您期望的方式:
While you initialize
temp
withstruct Node *temp=first;
, you subsequently usetemp
as a loop variable to show the list of stations:which means that
temp == NULL
when the loop exits. When you subsequently press '1' it doestemp->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:
You should move
struct Node *temp=first;
to before yourchoice
-loop, say, afterint 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:
Change your
while(choice != 4) { ... }
to ado { ... } 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: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: