如何删除链表中的多个结构?
How can I delete multiple structs in linked list?
我有一个问题:我得到了一个包含 "movies" 数据的结构。我需要按 "director"(字符)搜索,如果 "movies" 数据与我通过键盘输入的相同 "director" 匹配,则删除任何记录。一切都很好,我输入 "director" 并删除与之关联的 "movies" 数据,没问题。但是当有多个 "movies" 数据具有相同 "director" 时,它只会删除第一个遇到的数据。并在那里停止功能。我需要它继续并删除每个 "movie" 数据,匹配用户输入的导演。
感谢阅读,我希望有人能帮助我,我相信这很简单,只是我不太擅长编程(几个月前开始)。祝你今天过得愉快!
它可能需要一个 while 循环,但我试过了但没有用。也许我没有做对。
再次感谢阅读,保重!
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
struct info_movies
{
int nomenclature_number;
char movie_name[50];
char director[50];
int year;
};
typedef struct info_movies MOVIES;
struct list
{
MOVIES Data;
struct list* pNext;
};
typedef struct list LIST;
MOVIES Enter_Movies_Data();
LIST* Add_New_Movie_At_The_Beggining(LIST* pF, MOVIES newMovie);
void Print_Movies_List(LIST* p);
LIST* Delete_By_Director(LIST* pF);
MOVIES Enter_Movies_Data()
{
MOVIES NewMovie;
printf("\nPlease enter movie's nomenclature number:\n");
scanf("%d", &NewMovie.nomenclature_number);
printf("\nPlease enter the movie's name: \n");
scanf("%s", NewMovie.movie_name);
printf("\nPlease enter the movies's director:\n");
scanf("%s", NewMovie.director);
printf("\nPlease enter the year of production of the movie:\n");
scanf("%d", &NewMovie.year);
printf("\nSaved.\n");
return NewMovie;
}
LIST* Add_New_Movie_At_The_Beggining(LIST* pF, MOVIES newMovie)
{
LIST* pNew = pF;
if (pF == NULL)
{
pNew = (LIST*)malloc(sizeof(LIST));
pNew->Data = newMovie;
pNew->pNext = pF;
}
else
{
pNew = (LIST*)malloc(sizeof(LIST));
pNew->Data = newMovie;
pNew->pNext = pF;
}
return pNew;
}
void Print_Movies_List(LIST* p)
{
if (p == NULL)
{
printf("the best movie!\n");
return;
}
printf("\n---------------MOVIE---------------\n");
printf("\nMovie's nomenclature number: %d\n", p->Data.nomenclature_number);
printf("\nMovie's name: %s\n", p->Data.movie_name);
printf("\nMovie's director:%s\n", p->Data.director);
printf("\nMovie's year of production: %d\n", p->Data.year);
printf("\n---------------MOVIE---------------\n");
Print_Movies_List(p->pNext);
}
LIST* Delete_By_Director(LIST* pF)
{
LIST* pCurrent;
LIST* prev;
char tmpDirector[50];
printf("Enter the director's name:");
scanf("%s", tmpDirector);
for (pCurrent = pF, prev = pF; pCurrent != NULL; prev = pCurrent, pCurrent = pCurrent->pNext)
{
if (strcmp(pCurrent->Data.director, tmpDirector) == 0)
{
printf("\n------DELETED------\n");
printf("\nnomenclature number: %d\n movie name: %s\n movie director: %s\n year of production: %d\n", pCurrent->Data.nomenclature_number, pCurrent->Data.movie_name, pCurrent->Data.director, pCurrent->Data.year);
printf("\n------DELETED------\n");
if (prev == pCurrent)
{
pF = pF->pNext;
free(pCurrent);
return pF;
}
prev->pNext = pCurrent->pNext;
free(pCurrent);
return pF;
}
}
printf("\nNO MOVIES FOUND BY DIRECTOR: '%s'\n", tmpDirector);
return pF;
}
int main() {
LIST* pFirst = NULL;
MOVIES newMovie;
int mode;
do {
system("cls");
printf(" M E N U \n");
printf("1. Enter a new movie data\n");
printf("2. Print movies list.\n");
printf("3. Delete movie by director.\n");
printf("0. exit.\n");
printf("\n !!!PLEASE ENTER COMMAND!!!\n");
fflush(stdin);
scanf("%d", &mode);
switch (mode)
{
case 1:
newMovie = Enter_Movies_Data();
pFirst = Add_New_Movie_At_The_Beggining(pFirst, newMovie);
break;
case 2:
Print_Movies_List(pFirst);
system("pause");
break;
case 3:
pFirst = Delete_By_Director(pFirst);
system("pause");
break;
case 0:
printf("Exiting...\n");
system("pause");
break;
}
} while (mode != 0);
return 0;
}
函数Add_New_Movie_At_The_Beggining
有冗余代码。可以定义的更简单。
LIST* Add_New_Movie_At_The_Beggining(LIST* pF, MOVIES newMovie)
{
LIST* pNew = (LIST*)malloc(sizeof(LIST));
pNew->Data = newMovie;
pNew->pNext = pF;
return pNew;
}
函数 Delete_By_Director
可以按以下方式更改,如下所示,以便能够删除列表中的多个节点。
LIST* Delete_By_Director(LIST* pF)
{
LIST* pCurrent;
LIST* prev;
char tmpDirector[50];
printf("Enter the director's name:");
scanf("%s", tmpDirector);
size_t count = 0;
for (pCurrent = pF, prev = pF; pCurrent != NULL; )
{
if (strcmp(pCurrent->Data.director, tmpDirector) == 0)
{
printf("\n------DELETED------\n");
printf("\nnomenclature number: %d\n movie name: %s\n movie director: %s\n year of production: %d\n", pCurrent->Data.nomenclature_number, pCurrent->Data.movie_name, pCurrent->Data.director, pCurrent->Data.year);
printf("\n------DELETED------\n");
if (prev == pCurrent)
{
pF = pF->pNext;
free(pCurrent);
prev = pCurrent = pf;
}
else
{
prev->pNext = pCurrent->pNext;
free(pCurrent);
pCurrent = prev->pNext;
}
++count;
}
else
{
prev = pCurrent;
pCurrent = pCurrent->pNext;
}
}
if ( count == 0 ) printf("\nNO MOVIES FOUND BY DIRECTOR: '%s'\n", tmpDirector);
return pF;
}
我有一个问题:我得到了一个包含 "movies" 数据的结构。我需要按 "director"(字符)搜索,如果 "movies" 数据与我通过键盘输入的相同 "director" 匹配,则删除任何记录。一切都很好,我输入 "director" 并删除与之关联的 "movies" 数据,没问题。但是当有多个 "movies" 数据具有相同 "director" 时,它只会删除第一个遇到的数据。并在那里停止功能。我需要它继续并删除每个 "movie" 数据,匹配用户输入的导演。 感谢阅读,我希望有人能帮助我,我相信这很简单,只是我不太擅长编程(几个月前开始)。祝你今天过得愉快! 它可能需要一个 while 循环,但我试过了但没有用。也许我没有做对。 再次感谢阅读,保重!
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
struct info_movies
{
int nomenclature_number;
char movie_name[50];
char director[50];
int year;
};
typedef struct info_movies MOVIES;
struct list
{
MOVIES Data;
struct list* pNext;
};
typedef struct list LIST;
MOVIES Enter_Movies_Data();
LIST* Add_New_Movie_At_The_Beggining(LIST* pF, MOVIES newMovie);
void Print_Movies_List(LIST* p);
LIST* Delete_By_Director(LIST* pF);
MOVIES Enter_Movies_Data()
{
MOVIES NewMovie;
printf("\nPlease enter movie's nomenclature number:\n");
scanf("%d", &NewMovie.nomenclature_number);
printf("\nPlease enter the movie's name: \n");
scanf("%s", NewMovie.movie_name);
printf("\nPlease enter the movies's director:\n");
scanf("%s", NewMovie.director);
printf("\nPlease enter the year of production of the movie:\n");
scanf("%d", &NewMovie.year);
printf("\nSaved.\n");
return NewMovie;
}
LIST* Add_New_Movie_At_The_Beggining(LIST* pF, MOVIES newMovie)
{
LIST* pNew = pF;
if (pF == NULL)
{
pNew = (LIST*)malloc(sizeof(LIST));
pNew->Data = newMovie;
pNew->pNext = pF;
}
else
{
pNew = (LIST*)malloc(sizeof(LIST));
pNew->Data = newMovie;
pNew->pNext = pF;
}
return pNew;
}
void Print_Movies_List(LIST* p)
{
if (p == NULL)
{
printf("the best movie!\n");
return;
}
printf("\n---------------MOVIE---------------\n");
printf("\nMovie's nomenclature number: %d\n", p->Data.nomenclature_number);
printf("\nMovie's name: %s\n", p->Data.movie_name);
printf("\nMovie's director:%s\n", p->Data.director);
printf("\nMovie's year of production: %d\n", p->Data.year);
printf("\n---------------MOVIE---------------\n");
Print_Movies_List(p->pNext);
}
LIST* Delete_By_Director(LIST* pF)
{
LIST* pCurrent;
LIST* prev;
char tmpDirector[50];
printf("Enter the director's name:");
scanf("%s", tmpDirector);
for (pCurrent = pF, prev = pF; pCurrent != NULL; prev = pCurrent, pCurrent = pCurrent->pNext)
{
if (strcmp(pCurrent->Data.director, tmpDirector) == 0)
{
printf("\n------DELETED------\n");
printf("\nnomenclature number: %d\n movie name: %s\n movie director: %s\n year of production: %d\n", pCurrent->Data.nomenclature_number, pCurrent->Data.movie_name, pCurrent->Data.director, pCurrent->Data.year);
printf("\n------DELETED------\n");
if (prev == pCurrent)
{
pF = pF->pNext;
free(pCurrent);
return pF;
}
prev->pNext = pCurrent->pNext;
free(pCurrent);
return pF;
}
}
printf("\nNO MOVIES FOUND BY DIRECTOR: '%s'\n", tmpDirector);
return pF;
}
int main() {
LIST* pFirst = NULL;
MOVIES newMovie;
int mode;
do {
system("cls");
printf(" M E N U \n");
printf("1. Enter a new movie data\n");
printf("2. Print movies list.\n");
printf("3. Delete movie by director.\n");
printf("0. exit.\n");
printf("\n !!!PLEASE ENTER COMMAND!!!\n");
fflush(stdin);
scanf("%d", &mode);
switch (mode)
{
case 1:
newMovie = Enter_Movies_Data();
pFirst = Add_New_Movie_At_The_Beggining(pFirst, newMovie);
break;
case 2:
Print_Movies_List(pFirst);
system("pause");
break;
case 3:
pFirst = Delete_By_Director(pFirst);
system("pause");
break;
case 0:
printf("Exiting...\n");
system("pause");
break;
}
} while (mode != 0);
return 0;
}
函数Add_New_Movie_At_The_Beggining
有冗余代码。可以定义的更简单。
LIST* Add_New_Movie_At_The_Beggining(LIST* pF, MOVIES newMovie)
{
LIST* pNew = (LIST*)malloc(sizeof(LIST));
pNew->Data = newMovie;
pNew->pNext = pF;
return pNew;
}
函数 Delete_By_Director
可以按以下方式更改,如下所示,以便能够删除列表中的多个节点。
LIST* Delete_By_Director(LIST* pF)
{
LIST* pCurrent;
LIST* prev;
char tmpDirector[50];
printf("Enter the director's name:");
scanf("%s", tmpDirector);
size_t count = 0;
for (pCurrent = pF, prev = pF; pCurrent != NULL; )
{
if (strcmp(pCurrent->Data.director, tmpDirector) == 0)
{
printf("\n------DELETED------\n");
printf("\nnomenclature number: %d\n movie name: %s\n movie director: %s\n year of production: %d\n", pCurrent->Data.nomenclature_number, pCurrent->Data.movie_name, pCurrent->Data.director, pCurrent->Data.year);
printf("\n------DELETED------\n");
if (prev == pCurrent)
{
pF = pF->pNext;
free(pCurrent);
prev = pCurrent = pf;
}
else
{
prev->pNext = pCurrent->pNext;
free(pCurrent);
pCurrent = prev->pNext;
}
++count;
}
else
{
prev = pCurrent;
pCurrent = pCurrent->pNext;
}
}
if ( count == 0 ) printf("\nNO MOVIES FOUND BY DIRECTOR: '%s'\n", tmpDirector);
return pF;
}