arindrajit
arindrajit

Reputation: 76

Explain the output of the stack implemented using linked list

I am creating a stack with a linked list. I have coded the push and display stack portion but it is just producing the last entered data as output when I am trying to display the stack content. Why is this happening?

struct LinkedStack {
    int data;
    struct LinkedStack* next;
};

struct LinkedStack *first = NULL;
struct LinkedStack *previous = NULL;
struct LinkedStack *current = NULL;

int main(void) {
    int data = 0, choice = 0;
    if(current == NULL) {
         printf("\nNo Memory Allocated");
    }
    while(1) {
        printf("\n1. Push Data");
        printf("\n2. Pop Data");
        printf("\n3. Display The Stack");
        printf("\n4. Exit");
        printf("\nEnter Your Choice::\n");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                current = (struct node *)malloc(sizeof(struct LinkedStack));
                printf("Enter the data:: ");
                scanf("%d", &data);
                push(&current, data);
                break;

            case 3:
                printf("\n\n Stack Contents::");
                displayStack(current);
                break;
           case 4:
                exit(1);
           default:
                printf("\nWrong Choice.Enter Again");

        }
    }

}

void push(struct LinkedStack **s, int usrdata) {
    if(first == NULL) {
        first = *s;
    }  
    if(previous != NULL) {
          previous->next = *s;
    } 

    (*s)->data = usrdata;;
    (*s)->next = NULL;
    previous = *s;
}

void displayStack(struct LinkedStack *s) {
    struct LinkedStack *temp = (struct LinkedStack *)malloc(sizeof(struct LinkedStack));
    if(temp == NULL) {
        printf("No Memory Allocated");
    }
    temp = s;
    while(temp != NULL) {
        printf("\n %d", temp->data);
        temp = temp->next;
    }
} 

Upvotes: 1

Views: 169

Answers (2)

AnkitMittal
AnkitMittal

Reputation: 164

If you are pushing the data correctly, then try

  case 3:
         printf("\n\n Stack Contents::");
         displayStack(first);
         break;

instead of

     case 3:
         printf("\n\n Stack Contents::");
         displayStack(current);
         break;

The displayStack() function needs a reference to the front of your linked list.

I hope this solves your problem.

Upvotes: 1

Edmund
Edmund

Reputation: 10819

My advice is to start by removing those global variables. Try to think of the stack as a single variable of type LinkedStack *.

Then implement push and pop in terms of that. No global variables: the caller of those functions passes in a pointer to that variable, i.e. the functions should have signatures:

void push(LinkedStack **stack, int data);
int pop(LinkedStack **stack);

This is what you're doing already, so my guess is you have at least some of the right ideas. ;)

Questions to ask yourself:

  • what does an empty stack look like?
  • what happens if I push to an empty stack? (Do not worry about popping from an empty stack at this stage; worry about that when everything else is working.)
  • how does memory for a LinkedStack node get freed when something is popped?
  • how is the caller's LinkedStack * variable updated by push and pop ?

Upvotes: 0

Related Questions