C语言 如何删除链表中的多个结构?



我有一个问题:我得到了一个带有"电影"数据的结构。我需要按"导演"(char(搜索,如果"电影"数据与我通过键盘输入的相同"导演"匹配,则删除它的任何记录。一切都很酷,我输入"导演",它会删除与之关联的"电影"数据,没问题。但是当有多个"电影"数据具有相同的"导演"时,它只会删除遇到的第一个。并在那里停止该功能。我需要它继续并删除每个"电影"数据,与用户输入的导演相匹配。 感谢您的阅读,我希望有人帮助我,我相信这很简单,只是我不擅长编程(几个月前开始(。有好的一天! 它可能需要一段时间循环,但我尝试过,但它没有用。也许我做得不对。 再次感谢您的阅读,保重!

#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: %dn", p->Data.nomenclature_number);
printf("nMovie's name: %sn", p->Data.movie_name);
printf("nMovie's director:%sn", p->Data.director);
printf("nMovie's year of production: %dn", 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: %dn movie name: %sn movie director: %sn year of production: %dn", 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 datan");
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: %dn movie name: %sn movie director: %sn year of production: %dn", 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;
}

相关内容

  • 没有找到相关文章

最新更新