还没搞懂的东西,果然是菜鸡
#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
typedef struct _node{
int value;
struct _node *next;
} Node;
typedef struct _list{
Node *head;
//Node *tail;
} List;
void add(List *pList, int number)
{
// add to linked_list
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
// find the last
Node *last = pList->head;
if (last){
while (last->next) {
last = last->next;
}
//attach
last->next = p;
} else {
pList->head = p;
}
}
void print(List *pList)
{
Node *p;
for (p = pList->head; p; p = p->next) {
printf("%d\t", p->value);
}
printf("\n");
}
int main()
{
List list;
list.head = NULL;
int number;
do {
scanf("%d", &number);
if (number != -1){
add(&list, number);
}
} while (number != -1);
print(&list);
// found
scanf("%d", &number);
Node *p;
int isfound = 0;
for (p = list.head; p; p = p->next) {
if (p->value == number) {
printf("找到了\n");
isfound = 1;
break;
}
}
if (!isfound) {
printf("没找到\n");
}
// delete
Node *q;
for (q = NULL, p = list.head; p; q = p, p = p->next) {
if (p->value == number) {
if (q){
q->next = p->next;
} else {
list.head = p->next;
}
free(p);
break;
}
}
return 0;
}