数据结构_链表
删除元素
#include<cstdio>
#include<cstdlib>
struct node{
int data;
node* next;
};
node* create(int Array[]){//创建链表
node *p, *pre, *head;
head = new node;
head ->next = NULL;
pre = head;
for(int i = 0; i < 4; i++){
p = new node;
p ->data = Array[i];
p ->next = NULL;
pre ->next = p;
pre = p;
}
return head;
}
void deleteNode(node* head, int x) {
node* p = head ->next;
node* pre = head;
while(p != NULL){
if(p->data == x){
pre->next = p->next;
delete(p);
p = pre->next;
}
else{
pre = p;
p = p->next;
}
}
}
int main(){
//int maxn;
//scanf("%d", &maxn);
//int *Array;
//Array = (int*) malloc(maxn * sizeof(int));
//for(int i = 0; i < 5; i++){
//scanf("%d", &Array[i]);
//}
int Array[4] = {4,5,1,9};
node* L = create(Array);
//L = L -> next;
//while(L != NULL){
//printf("%d ", L->data);
//L = L->next;
//}
int x;
scanf("%d", &x);
deleteNode(L, x);
L = L -> next;
while(L != NULL){
printf("%d ", L->data);
L = L->next;
}
system("pause");
return 0;
}