#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int value;
struct node* next;
}Node;
typedef struct List {
Node* head;
Node* tail;
}List;
void Add(int number, List* list);
void Print(List* list);
void DelAndSearch(int search, List* list);
int main()
{
int number;
List list;
list.head = NULL;
do {
scanf_s("%d", &number);
if (number != -1) {
Add(number, &list);
}
} while (number != -1);
Print(&list);
int search;
scanf_s("%d", &search);
//DelAndSearch(search, &list);
Node* p = list.head;
Node* q = NULL;
for (p, q; p; q = p, p = p->next) {
if (q) {
if (p->value == search) {
q->next = p->next;
free(p);
}
}
else {
p = p->next;
free(p);
}
}
Print(&list);
return 0;
}
void Add(int number,List* list)
{
Node* p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
if (list->head) {
list->tail->next = p;
list->tail = p;
}
else {
list->head = p;
list->tail = p;
}
}
void Print(List* list)
{
for (Node* i = list->head; i != NULL; i = i->next) {
printf("%d ", i->value);
}
printf("\n");
}
void DelAndSearch(int search, List* list)
{
Node* p = list->head;
Node* q = NULL;
for ( p,q ; p; q = p, p = p->next) {
if (q) {
if (p->value == search) {
q->next = p->next;
free(p);
}
}else {
p = p->next;
free(p);
}
}
Print(list);
}
DelAndSearch 函数里面的 if(p->value==search) 这句会出错,SOS求解