What's the problem with the same error as the title? I'm going to watch it and follow it, but I don't know.
#include <stdio.h>
#include <stdlib.h>
Typedef struct list {// The connection with the address list structure and key. that declaration, named node
int key;
struct list *next;
} node;
node *top, *bottom;
Int init stack the (int) {// initialization.
top = (node*)malloc(sizeof(node));
bottom = (node*)malloc(sizeof(node));
top -> next = bottom;
bottom -> next = bottom;
return 0;
}
Int print the output () {//.
node *p;
p = top->next;
while (p != bottom) {
printf("%d ", p->key);
p = p->next;
}
puts("");
return 0;
}
int push(int n) { //push
node *p;
if ((p = (node*)malloc(sizeof(node))) == NULL) {
printf ("Memory Exceeded\n");
return -1;
}
printf("push %d ", n);
p-> key = n;
p->next = top ->next; //p becomes the top of the stack
top -> next = p; //p followed by push
print();
return n;
}
int pop() {//pop
node *p; // temporary node to delete
intn; // For integer value return deleted after deletion
if (top->next == bottom) { //Check if the stack is empty
printf ("Nothing in Stack\n");
return -1;
}
p = top->next; //top_next Save to p for node deletion
n = p->key; //Save the key value of the p you want to delete
printf("pop %d ", n);
top->next = p-> next; //head->next node points to the next node on the node you want to delete
free(p); //p Delete
print();
return n;
}
int clear() { // Empty the stack
node *p, *q;
p = top->next; //Save the first node in p
while (p != bottom) {
q = p; // Save in q to delete node
p = p -> next; //p crosses to the next node
free(q); //Delete Saved Node
} //If you don't delete a saved node, it's a waste of memory
top->next = bottom;
printf ("stack empty\n");
return 0;
}
int main() {
push(1);
push(2);
pop();
push(3);
clear();
pop();
return 0;
}
Return operation-pop() is not possible after initialization-clear().
Because there's nothing.
An error occurs because a null point (empty value) has been accessed.
There are 2 errors.
Please refer to the code below. Modified the parameter portion of the init_stack function and added init_stack function calls to the main function. To increase readability, we added a line of puts("") to the top of the print function. The rest of the code remains the same.
#include <stdio.h>
#include <stdlib.h>
Typedef struct list {// The connection with the address list structure and key. that declaration, named node
int key;
struct list* next;
} node;
node* top, * bottom;
int init_stack() { //initialization
top = (node*)malloc(sizeof(node));
bottom = (node*)malloc(sizeof(node));
top->next = bottom;
bottom->next = bottom;
return 0;
}
Int print the output () {//.
puts("");
node* p;
p = top->next;
while (p != bottom) {
printf("%d ", p->key);
p = p->next;
}
puts("");
return 0;
}
int push(int n) { //push
node* p;
if ((p = (node*)malloc(sizeof(node))) == NULL) {
printf ("Memory Exceeded\n");
return -1;
}
printf("push %d ", n);
p->key = n;
p->next = top->next; //p becomes the top of the stack
top->next = p; //p followed by push
print();
return n;
}
int pop() {//pop
node* p; // temporary node to delete
intn; // For integer value return deleted after deletion
if (top->next == bottom) { //Check if the stack is empty
printf ("Nothing in Stack\n");
return -1;
}
p = top->next; //top_next Save to p for node deletion
n = p->key; //Save the key value of the p you want to delete
printf("pop %d ", n);
top->next = p->next; //head->next node points to the next node on the node you want to delete
free(p); //p Delete
print();
return n;
}
int clear() { // Empty the stack
node* p, * q;
p = top->next; //Save the first node in p
while (p != bottom) {
q = p; // Save in q to delete node
p = p->next; //p crosses to the next node
free(q); //Delete Saved Node
} //If you don't delete a saved node, it's a waste of memory
top->next = bottom;
printf ("stack empty\n");
return 0;
}
int main() {
init_stack();
push(1);
push(2);
pop();
push(3);
clear();
pop();
return 0;
}
© 2024 OneMinuteCode. All rights reserved.