ashadeka
ashadeka

Reputation: 43

Push Item into Stack with C

this is my push function, which takes in a stack and an int a parameters, turns the int into part of a stackNode and returns a new stack with the added new node (StackNode and Stack have already been properly initialized):

Stack push(Stack stk,int data){
    struct StackNode *top = stk.top;
    struct StackNode *node;
    node->data = data;
    node->nextNode = top;
    stk.top = node;
    stk.count++;
    return stk;
}

The problem I am having is that each time I run the push function, it over writes other StackNode items in the stack as the parameter I ran.

For example:

push(stk, 3): stack output = 3 //good so far
push (stk, 4): stack output= 4 4//the correct output would be 3 4!
push(stk, 56): stack out put= 56 56 56 //what's going on!

what am I doing wrong, please help

Upvotes: 1

Views: 2012

Answers (3)

Alex
Alex

Reputation: 779

I read you code this way

struct StackNode *node;
node->nextNode = stk.top;
stk.top = node;

it looks like you adding a node but the reference on next is always on itself.

Edit:

struct StackNode *top = stk.top;
struct StackNode *node = malloc(sizeof(struct StackNode));
node->data = data;
top->nextNode = node;
stk.top = node;
stk.count++;
return stk;

Edit2:

Sry to forget to explain,

you wrote

node->nextNode = top;

that I replaced with

top->nextNode = node;

Your code has tacken the new node and referenced the current top as the next one and then made the new one the top, that means as you called the stack, the beginning node was the node, that you put on your stack last and it reffered to itself as the next node, hence this output:

push(stk, 3): stack output = 3 //good so far
push (stk, 4): stack output= 4 4//the correct output would be 3 4!
push(stk, 56): stack out put= 56 56 56 //what's going on!

My code changed that to, that if your put a new node on the stack, the new one is defined as the next node of the current top node, before defining the new node the new top node.

Upvotes: -1

Vlad from Moscow
Vlad from Moscow

Reputation: 311028

You did not allocate memory for the node. Add the memory allocation.

Stack push(Stack stk,int data){
    struct StackNode *top = stk.top;
    struct StackNode *node = malloc( sizeof( struct StackNode ) );

    node->data = data;
    node->nextNode = top;
    stk.top = node;
    stk.count++;

    return stk;
}

Or you can add a check of a successful allocation.

Stack push(Stack stk,int data){
    struct StackNode *top = stk.top;
    struct StackNode *node = malloc( sizeof( struct StackNode ) );

    if ( node != NULL )
    {
        node->data = data;
        node->nextNode = top;
        stk.top = node;
        stk.count++;
    }

    return stk;
}

I suppose that initially the data member top of the object of the type Stack was initialized by NULL.

Upvotes: 3

CoderGirl94
CoderGirl94

Reputation: 214

Here is an alternative solution: //You can assume that the function call passes the: address of a pointer to the node at the top of the stack, and an integer. ion: e.g. push (&stackpointer, 5) // insert a node with data value 5 onto the top of a stack

void fun(StackNodePtr *topPtr, int info)  
{ 
   StackNodePtr  *newPtr = malloc(sizeof(StackNode));


   if (newPtr != NULL) {           
      newPtr->data = info;           
     newPtr->nextPtr = *topPtr;
      *topPtr = newPtr;              
   }                     
   else { // no space available
      printf("%d not inserted. No memory available.\n", info);
   } 
}

}

Upvotes: 0

Related Questions