0

I'm trying to incorporate push/pop into a linked list and I can't seem to get it to work. When I run my test function, I set my linked list to zero and I try to push on values but the list keeps getting returned with no values in it. Could anyone possibly tell me what I'm doing wrong?

7 Answers 7

1
if (top == NULL){
      current = top;
      current->next = NULL; //NULL->next : will cause segfault
  }

if top is NULL, you set current = top [which is NULL], and then you access current->next, which will cause a segfault, you are trying to access NULL..

EDIT: follow up to comments:
your if statement seems redundant, you should probably only need to set: current->next = head; and head = current; [in addition to the current allocation]

Sign up to request clarification or add additional context in comments.

4 Comments

I changed it around from top=current but I'm still not getting returned anything.
@BleuCheese: The problem is that you're changing only the local variable top when you would need to be changing the global variable head -- otherwise, your changes won't "survive" beyond the end of the function.
your if statement seems redundant, you should probably only need to set: current->next = head; and head = current;
I'm so thankful that you guys pointed this out. I've been having so much trouble with pointers and stuff lately but you guys cleared so much up. My code works perfect now. Thank you so much. :)))))
0

Instead of

 if (top == NULL){
      current = top;
      current->next = NULL;
 }

you want

 if (top == NULL){
      top = current;
      current->next = NULL;
 }

And of course, after this, you have to make sure that you actually set head to top again.

Now that you've made this change, it should be clear that both cases do the same thing -- so no case distinction is actually necessary. So the function can be simplified to

void push(Data * newPushData){
    LinkNode * current = new LinkNode(newPushData);
    current->next = head;
    head = current;
}

Comments

0

The top variable is local variable for push(...) function. You can use head instead, and I'd rather modify the if statement.

I think that function should look like this:

void push(Data * newPushData){
    LinkNode * current = new LinkNode(newPushData);
    if (head != NULL){
        current->next = head;
        head = current;
    }
    else{
        head = current;
        current->next = NULL; // if you haven't done it in LinkNode constructor
    }
}

Comments

0

can you please specify the attributes of the linked list class ? [ is there slightly chance you are doing something wrong]

Instead of you , I'd do :

void push(Data * newPushData){
if (head   == NULL) 
    head->data = newPushData
    tail = head ; 

else // regular situation 
     { 
     Node  * node  = new Node() ; 
     tail->next = node; 
     node->data  = newPushData; 
     node->next  = NULL ;
     tail  = node ; 
   } 

}

In a linked list you have got to maintain the head pointer point on the head of the list , maintain that the tail pointer is point on the tail of the list , You must take care of the 2 cases of enlarging the list. the best way for learning is to illustrate an insertion on a blank linked list.

Take care S

Comments

0
void push(Data * newPushData)
{
    if( head != NULL )
    {
        LinkNode current = new LinkNode(newPushData);
        current->next = head;
        head = current;
    }
    else
    {
        head = new LinkNode(newPushData);
    }
}

Comments

0

Try this code...

void push(data * newpushdata){
    if(head !=null){
        linkednode current = new linkednode(newpushdata);
        current->next = head;
        head = current;
    }
    else {
       head = new linkednode(newpushdata);
    }
}

Comments

0

that is my working solution for a Stack containing int elements, but maybe it's better to create a void pushStack using Stack **S instead of Stack *S.

in pop(Stack **S) i created a sentinel, so if the stack is empty -1 is returned.:

typedef struct StackT {

    int val;
    struct StackT *next;

} Stack;

int isStackEmpty (Stack *S) {

    if (S == NULL)
        return 1;
    else
        return 0;
}


int *pop(Stack **S) {
    Stack *tmp = *S;
    int i = -1;
    if (isStackEmpty(tmp) == 0) {
        i = tmp->val;
        *S = tmp->next;
    }
    return i;
}

Stack *pushStack (Stack *S, int x) {

    Stack *node = (Stack *) malloc (sizeof (Stack));
    node->val = x;
    node->next = S;

    return node;
}

you can call pop and stack easly:

    Stack *S = NULL;
    int x = somevalue;
    int y;
    S = pushStack(S, x);
    y = pop(&S);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.