我有問題,從列表中刪除元素刪除鏈表元素,直到我找到單詞
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
typedef struct elem{
char name[MAX];
int statistic;
int price;
struct elem *next;
struct elem *prev;
} shop;
我在列表中搜索主定義的話造成的功能,然後,如果它發現它,它保持刪除1 ST元素,直到找到單詞,然後將其刪除所選的詞,使未來的第一:
void delete_from_list(shop *first, char word[MAX], int* check)
{
shop *tmp= first;
while (tmp!=NULL && strcmp(tmp->name, word) != 0)
{
tmp = tmp->next;
}
if (tmp != NULL && strcmp(tmp->name, word)==0)
{
printf("FOUND!");
}
else
{
*check=1;
}
if (check==0)
{
while (strcmp(first->name, tmp->name)!=0)
{
first=first->next;
free(first->prev);
first->prev=NULL;
}
first=first->next;
free(first->prev);
first->prev=NULL;
}
}
void print_list(shop *first)
{
first=first->next;
if(first->name==NULL)
printf("There is nothing!!!\n");
while(first->next!=NULL){
printf("%20s",first->name);
printf("%20d \t\t",first->statistic);
printf("%d\n",first->price);
first=first->next;
}
}
我打印的清單,然後取詞。
main()
{
int check = 0;
print_list(first);
scanf("%s", word);
delete_from_list(first, word, &check);
if (check!=1)
{
print_list(first);
}
else
{
check=0;
}
}
問題是,鏈表不會如何改變,所以我應該添加指針,但我不知道如何。
你真的應該顯示一些輸入,什麼是預期的,以及你得到了什麼。 – coredump
你的意圖不明確。如果元素不是**,您是否希望**完整列表**被刪除? – wildplasser