I have implemented my stack using doubly linked list and I don’t know why it’s not printing, I think it has something to do with the right C syntax or something?
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
//---------------------Stack---------------------
typedef struct Stack {
int size;
Node* head;
Node* tail;
int top;
} Stack;
const Stack stack_init = { .size = 0, .head = NULL, .tail = NULL, .top = -1 };
Node* create_node(int elm) {
Node* node = malloc(sizeof * node);
if (!node) return node;
node->data = elm;
node->prev = NULL;
node->next = NULL;
return node;
}
void push(Stack s, int elm) {
Node* updated_head = create_node(elm);
if (!s.head) {
s.head = updated_head;
s.tail = s.head;
}
else {
updated_head->next = s.head;
s.head->prev = updated_head;
s.head = updated_head;
}
s.size++;
s.top = s.head->data;
// printf("%d ", s.tail->data);
}
int pop(Stack s) {
Node* node = s.head;
int elm = node->data;
s.head = s.head->next;
if (s.head) {
s.head->prev = NULL;
s.top = s.head->data;
}
else {
s.tail = NULL;
s.top = -1;
}
s.size--;
free(node);
return elm;
}
int top(Stack s) {
return s.top;
}
void clear_s(Stack s) {
while (s.tail)
pop(s);
}
Stack reverse_s(Stack s) { // iterative
Stack s2= stack_init;
while (s.tail) {
push(s2, pop(s));
}
while (s2.tail) {
push(s, pop(s2));
}
return s;
}
int is_empty_s(Stack s) {
return s.tail == NULL;
}
void print_s(Stack s) {
Node* trav = s.head;
while (trav) {
printf("%d ", trav->data);
trav = trav->next;
}
printf("\n");
}
int main() {
Stack s1 = stack_init;
push(s1, 5);
push(s1, 4);
push(s1, 3);
push(s1, 2);
push(s1, 1);
print_s(s1);
return 0;
}
As you can see, the stack is based off my doubly linked list which is functional and working, but the print function when starting from the head node doesn’t output anything.
>Solution :
C uses call by value which means that push(s1, 5) will pass a copy of s1 and 5 are to the function. As you want to mutation you need to pass the address of s1:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
typedef struct Stack {
int size;
Node* head;
Node* tail;
int top;
} Stack;
const Stack stack_init = { .size = 0, .head = NULL, .tail = NULL, .top = -1 };
Node* create_node(int elm) {
Node* node = malloc(sizeof * node);
if (node) {
node->data = elm;
node->prev = NULL;
node->next = NULL;
}
return node;
}
void push(Stack *s, int elm) {
Node* updated_head = create_node(elm);
if (!s->head) {
s->head = updated_head;
s->tail = s->head;
} else {
updated_head->next = s->head;
s->head->prev = updated_head;
s->head = updated_head;
}
s->size++;
s->top = s->head->data;
}
void print_s(Stack *s) {
Node* trav = s->head;
while (trav) {
printf("%d ", trav->data);
trav = trav->next;
}
printf("\n");
}
int main() {
Stack s1 = stack_init;
push(&s1, 5);
push(&s1, 4);
push(&s1, 3);
push(&s1, 2);
push(&s1, 1);
print_s(&s1);
}
and the resulting output is:
1 2 3 4 5