#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Node {
int data;
struct Node* node;
}Node;
Node* init(Node*first) {
first = (Node*)malloc(sizeof(Node));
first->node = NULL;
return first;
}
void creatlist(Node* first) {
Node* p;
int j = 0, i = 1;
while (i) {
scanf_s("%d", &j);
if (j != -1) {
p = (Node*)malloc(sizeof(Node));
p->data = j;
p->node = first->node;
first->node = p;
}
else {
i = 0;
}
}
}
void deletes(Node* first, int x) {
Node* p = first->node;
Node* q = p, * l = first;
while (p->node != NULL) {
if (p->data != x) {
l = p;
p = p->node;
}
else {
q = p;
p = p->node;
l->node = p;
free(q);
}
}
if (p->data == x)
free(p);
}
void output(Node* first) {
Node* p = first->node;
while (p->node != NULL) {
printf("%d", p->data);
p = p->node;
}
printf("%d", p->data);
}
int main() {
Node* first = (Node*)malloc(sizeof(Node));
first->node = NULL;
creatlist(first);
deletes(first, 1);
output(first);
return 0;
}
c语言编程实现删除单链表中所有关键字为x的元素
最新推荐文章于 2023-08-24 12:10:03 发布