include stdio include iostream include cstdlib using namespace std str

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <iostream>
#include <cstdlib>
using namespace std;
struct stack {
int data;
stack *next;
};
void push(stack **top, int data) {
stack *q;
q = new stack();
q->data = data;
if (top == NULL) {
*top = q;
} else {
q->next = *top;
*top = q;
}
}
void delete_key(stack **top, int key) {
stack *q = *top;
stack *prev = NULL;
while (q != NULL) {
if (q->data == key) {
if (q == *top) {
*top = q->next;
free(q);
q->data = NULL;
q->next = NULL;
} else {
prev->next = q->next;
free(q);
q->data = NULL;
q->next = NULL;
}
}
prev = q;
q = q->next;
}
}
void echo(stack *top) {
stack *q = top;
while (q) {
printf_s("%i", q->data);
q = q->next;
}
}
int main() {
stack *top = NULL;
push(&top, 1);
push(&top, 2);
push(&top, 3);
push(&top, 4);
echo(top);
delete_key(&top, 1);
cout << endl;
echo(top);
return 0;
}