Reputation: 251
I try to implement a stack in C but I get a very strange error. For some reason my push function does not work..
typedef struct node
{
int v;
struct node* next;
}Node;
void push(Node *stack,int val)
{
Node *p = (Node *)calloc(1,sizeof(Node));
p->v = val;
Node *aux = stack;
if(aux == NULL)
{
stack = p;
return;
}
while(aux->next != NULL)
aux = aux->next;
aux->next = p;
}
I initialized my stack with NULL
Node *stack = NULL;
and I call the function something like this
push(stack,value)
L.E. I tried to create a pop function with parameter double pointer but the result is the same as for push.
void pop(Node **l)
{
if((*l) == NULL)
return;
else
{
Node *aux,*prev;
prev = *l;
aux = prev->next;
if(aux == NULL)
{
free(prev->v);
free(prev);
return;
}
while(aux != NULL)
{
prev = aux;
aux = aux->next;
}
prev->next = NULL;
free(aux->v);
free(aux);
}
}
Upvotes: 2
Views: 13514
Reputation: 310910
First of all stack is a data organization that satisfies the policy LIFO (Last Input First Output). That is a new data is added at the top of the stack and popped from the top of the stack.
You should not add a loop to find the tail of the stack that to add a new data.
And you need to pass the top of the stack by reference. Take into account that function parameters are its local variable. Thus in your function
void push(Node *stack,int val);
you are changing the local variable stack
of the function that will be destroyed after exiting the function. The original argument will not be changed.
Also the memory allocation can fail You should report an error some way.
Here is a demonstrative program that shows how functions push
and pop
can be implemented
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int v;
struct node *next;
} Node;
int push( Node **stack, int val )
{
Node *p = malloc( sizeof( Node ) );
int success = p != NULL;
if ( success )
{
p->v = val;
p->next = *stack;
*stack = p;
}
return success;
}
int pop( Node **stack, int *val )
{
int success = *stack != NULL;
if ( success )
{
Node *p = *stack;
*stack = ( *stack )->next;
*val = p->v;
free( p );
}
return success;
}
int main(void)
{
const int N = 10;
Node *stack = NULL;
int i = 0;
int val;
while ( i < N && push( &stack, i ) ) i++;
while ( i != 0 && pop( &stack, &val ) ) printf( "%d ", val );
printf( "\n" );
return 0;
}
Its output is
9 8 7 6 5 4 3 2 1 0
Upvotes: 1
Reputation: 5220
Remember to initialize the next field to NULL (p->next)
Since stack is a pointer being passed to the push function, the pointer value changes in the function don't get reflected outside of the function.
Ie: if in the push function, we set stack = something, the stack pointer outside the function wont be set to that.
The way around that is either pass the stack in as a pointer to the pointer, then you can modify the pointer value)
void push(Node **stack,int val)
{
Node *p = (Node *)calloc(1,sizeof(Node));
p->v = val;
p->next = NULL;
Node *aux = *stack;
if(aux == NULL)
{
*stack = p;
return;
}
while(aux->next != NULL)
aux = aux->next;
aux->next = p;
}
Call it via:
push(&stack, 10);
Or perhaps easier to understand for beginners, return the stack value from the push function.
Node * push(Node *stack,int val)
{
Node *p = (Node *)calloc(1,sizeof(Node));
p->v = val;
p->next = NULL;
Node *aux = stack;
if(aux == NULL)
{
stack = p;
return stack;
}
while(aux->next != NULL)
aux = aux->next;
aux->next = p;
return stack;
}
Call via:
stack = push(stack, 10);
Upvotes: 0
Reputation: 6070
your push function needs a pointer to a stack node.
void push(Node **stack, int val) {
...
*stack = p;
....
}
push(&stack, value);
Upvotes: 1
Reputation: 171
You need to pass stack by reference:
void push(Node **stack,int val)
{
Node *p = (Node *)calloc(1,sizeof(Node));
p->v = val;
Node *aux = *stack;
if(aux == NULL)
{
*stack = p;
return;
}
while(aux->next != NULL)
aux = aux->next;
aux->next = p;
}
Andcall as push(&stack,value)
Upvotes: 0