[英]Issue when comparing a value in a linked list in C
我正在用C語言編寫一個“簡單”程序,我們在其中創建一個鏈表,該鏈表的結構類似於電影,用於存儲影片,標題,制作年份,評分(1-5)和指向該影片的指針。下一個節點。 我們不允許在此結構中添加任何內容或定義函數。
最重要的是,(出於某種原因)我們需要在main()的主體中編寫整個鏈接列表,以便為該問題添加一定程度的意大利面條。 無論如何,在此程序中,我們應該讓用戶輸入“ U”進行更新,或者輸入“ S”進行電影搜索。 更新完成您所期望的,然后輸入標題,年份,等級。 由此,我們應該將節點插入到鏈表的末尾 。
我們的搜索應循環訪問此鏈接列表,如果找到匹配項,則應打印出電影,標題和年份。
盡管代碼的更新部分有效,但似乎無法使搜索生效。 我正在使用一個名為temp的電影結構,該結構從頭開始並遍歷列表以嘗試查找電影。 在通過printf運行了一些測試之后,我發現temp不管是什么,無論我輸入什么電影,都只是一個空節點。
我假設這與我調用malloc的方式有關? 還是與未正確分配節點有關? 老實說,我不確定,很遺憾,實驗室的TA也不知道D怎么了。
這是我的代碼:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct movie_node {
char title[250];
int year;
unsigned char rating;
struct movie_node *next;
};
typedef struct movie_node movie;
int main() {
// variables
int loop = 1;
movie *head = NULL; // represents first
movie *current; // represents the last node
movie *temp; // used for traversing the linked list
head = malloc(sizeof(movie));
int amountOfMovies = 0; // increment after each addition
char choice; // either 'u' (update) or 's' (search)
// ask user for input
while(loop) {
printf("%d Movie(s) in the database. Update or search (U/S): ", amountOfMovies);
scanf(" %c", &choice);
/* CHOICE 1, UPDATE */
if(choice == 'U') {
// case 1, head is null, we must create a new node
if(head == NULL) {
// get input
printf("Name of the movie: ");
scanf(" %[^\n]%*c", head->title);
printf("Year: ");
scanf("%d", &head->year);
printf("Rating: ");
scanf("%hhu", &head->rating);
head->next = NULL;
head = current; // set head to point to current
} else {
current = head;
// need to find where current is
while(current != NULL) {
current = current->next;
} // end while
// current is now at the null position, indicating empty node
current = malloc(sizeof(movie)); // allocate mem
// get user input
printf("Name of the movie: ");
scanf(" %[^\n]%*c", current->title);
printf("Year: ");
scanf("%d", ¤t->year);
printf("Rating: ");
scanf("%hhu", ¤t->rating);
current->next = NULL;
} // end else
// output movie
printf("Movie \"%s\" is added to the database.\n", current->title);
amountOfMovies++; // increment amount of movies in database
} else if(choice == 'S') {
/* CHOICE 2, SEARCH */
// temp string
char tempTitle[250];
// flag to let us know if we found something
bool found = false;
// temp linked list to traverse
temp = head;
temp = malloc(sizeof(movie));
// ask user for input
printf("Name of movie: ");
scanf(" %[^\n]%*c", tempTitle);
printf("NAME OF MOVIE IN HEAD: %s\n", temp->title); // test, take out later
while(temp != NULL) {
printf("NAME OF CURRENT MOVIE TO COMPARE TO: %s\n", temp->title); // test
if(strcmp(temp->title, tempTitle) == 0) {
// match
printf("Year: %d\n", temp->year);
printf("Rating: %hhu\n", temp->rating);
found = true;
break;
} else { // no match so far
temp = temp->next;
printf("HAVEN'T FOUND MATCH, NEXT TITLE TO CHECK IS: %s\n", temp->title); // test print
found = false;
} // end else
} // end while
if(found == false) { // no match confirmed
printf("Movie \"%s\" does not exist in the database.\n", tempTitle);
}
} else { // choice is invalid
loop = 0; // exit
} // end if-else
} // end while
// free all the nodes
return 0;
}
注意:我還沒有實現的唯一事情就是釋放內存。.我不確定百分百應該如何實現。
任何幫助是極大的贊賞..
問題出在您的malloc()
調用上。 首先,您要做的是:
movie *head = NULL;
// ...
head = malloc(sizeof(movie));
這意味着head不再為null,您將無法以所需的方式插入第一部電影-將malloc()
移動到其他位置。
其次,執行以下幾行代碼:
current = head; // <- this is OK
// ...
current = malloc(sizeof(movie)); // allocate mem <- this is NOT OK, for the same reason as before
同樣,您可以像這樣讀取電影的標題: scanf("%249s", head->title)
。
如果您知道該怎么走,請告訴我。
除了代碼中的問題外,還有另一個問題: 實驗室的TA也不知道哪里出了問題 。
樣品修復
movie *new_node(void){//aggregate the creation of a new node
movie *node = malloc(sizeof(*node));//error check omit
printf("Name of the movie: ");
scanf(" %249[^\n]%*c", node->title);
printf("Year: ");
scanf("%d", &node->year);
printf("Rating: ");
scanf("%hhu", &node->rating);
node->next = NULL;
return node;
}
int main() {
int loop = 1;
movie *head = NULL;
movie *current;
movie *temp;
//head = malloc(sizeof(movie));//"if(head == NULL) {" don't work
int amountOfMovies = 0;
char choice;
while(loop) {
printf("%d Movie(s) in the database. Update or search (U/S): ", amountOfMovies);
scanf(" %c", &choice);
if(choice == 'U') {
if(head == NULL) {
current = head = new_node();//need set to current
} else {
current = head;
while(current->next != NULL) {//"current != NULL" can't link
current = current->next;
}
current = current->next = new_node();
}
printf("Movie \"%s\" is added to the database.\n", current->title);
amountOfMovies++;
} else if(choice == 'S') {
char tempTitle[250];
bool found = false;//need <stdbool.h>
temp = head;
//temp = malloc(sizeof(movie));//Unnecessary
printf("Name of movie: ");
scanf(" %249[^\n]%*c", tempTitle);
//printf("NAME OF MOVIE IN HEAD: %s\n", temp->title);
while(temp != NULL) {
//printf("NAME OF CURRENT MOVIE TO COMPARE TO: %s\n", temp->title);
if(strcmp(temp->title, tempTitle) == 0) {
printf("Year: %d\n", temp->year);
printf("Rating: %hhu\n", temp->rating);
found = true;
break;
} else {
temp = temp->next;
printf("HAVEN'T FOUND MATCH, NEXT TITLE TO CHECK IS: %s\n", temp->title);
//found = false;//Unnecessary
}
}
if(found == false) {
printf("Movie \"%s\" does not exist in the database.\n", tempTitle);
}
} else {
loop = 0;
}
}
// free all the nodes
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.