I have made this code to implement stack by linked list with some operations like add elements, delete, etc. There's some issue with my output, which is unexpected. If you can modify the freeStack(),pop() and push() functions, feel free to modify them. Thanks
// C program for linked list implementation of stack
#include <stdio.h>
#include <stdlib.h>
// Implement a stack as a linked list
typedef struct Node {
int data;
struct StackNode* next;
}Node;
typedef struct Stack {
int size;
Node *pTop;
}Stack;
// Initialize the stack
void init (Stack *s) {
s = (Stack*)malloc(sizeof(Stack));
s->size = 0;
s->pTop = NULL;
}
// Check if a stack is empty
int isEmpty (Stack st) {
return (st.size == 0);
}
// Push operation to add new element
int push (int newData, Stack *st)
{
// put newData in a stack node -> this node becomes the top element
Node *p = (Node*)malloc(sizeof(Node));
if (p == NULL) // check if memory allocation for the new node is successful
return 0; // return 0 to indicate failure
p->data = newData;
p->next = st->pTop;
st->pTop = p;
st->size++;
return 1;
}
// Pop operation to delete the first top element
int pop (Stack *st)
{
Node *p;
if (isEmpty(*st))
return 0; // fail to pop
p = st->pTop;
st->pTop = st->pTop->next; // change the pointer of the top element to the second top element
st->size--;
free(p);
return 1;
}
// Top operation to return the top element
int top(Stack st) {
if (isEmpty(st))
return 1;
return st.pTop->data;
}
// Display elements of the stack
void display(Stack *s)
{
Node *p = s->pTop;
printf("Stack: ");
for (; p != NULL; p = p->next) {
printf("%d ", p->data);
}
printf("\n");
}
// Free all memory used by the stack
void freeStack(Stack* st) {
Node* current = st->pTop;
Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
st->pTop = NULL;
st->size = 0;
}
int main()
{
// Memory allocation
Stack st;
init(&st);
// Push elements
push(10, &st);
push(20, &st);
push(30, &st);
display(&st);
printf("30 is popped from stack\n", pop(&st));
display(&st);
printf("Top element is %d\n", top(st));
freeStack(&st);
return 0;
}
Here's the output:
linked_list_stack.c: In function 'push':
linked_list_stack.c:36:17: warning: assignment to 'struct StackNode *' from incompatible pointer type 'Node *' [-Wincompatible-pointer-types]
36 | p->next = st->pTop;
| ^
linked_list_stack.c: In function 'pop':
linked_list_stack.c:50:14: warning: assignment to 'Node *' from incompatible pointer type 'struct StackNode *' [-Wincompatible-pointer-types]
50 | st->pTop = st->pTop->next; // change the pointer of the top element to the second top element
| ^
linked_list_stack.c: In function 'display':
linked_list_stack.c:68:25: warning: assignment to 'Node *' from incompatible pointer type 'struct StackNode *' [-Wincompatible-pointer-types]
68 | for (; p != NULL; p = p->next) {
| ^
linked_list_stack.c: In function 'freeStack':
linked_list_stack.c:80:14: warning: assignment to 'Node *' from incompatible pointer type 'struct StackNode *' [-Wincompatible-pointer-types]
80 | next = current->next;
| ^
Stack: 30 20 10 -489483407
30 is popped from stack
Stack: 20 10 -489483407
Top element is 20
Compiler does not know what the struct StackNode
is. You need to:
typedef struct StackNode {
int data;
struct StackNode* next;
}Node;
Also, I do not have an idea what you tried to archive. Return reference to allocated struct.
// Initialize the stack
Stack *init (void) {
Stack *s = malloc(sizeof(*s));
if(s)
{
s->size = 0;
s->pTop = NULL;
}
return s;
}
In main
function:
Stack *st;
st = init();
if(st)
{
push(10, st);
/* ... */
}