Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Problem in Stack Implementation using Linked list (http://www.go4expert.com/forums/stack-implementation-using-linked-list-t15221/)

back from retirement 20Nov2008 19:50

Problem in Stack Implementation using Linked list
 
I have some problem regarding my own stack program using linklist....every thing is ok except for the display function....the program while running cannot display....

Here it is....
Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct node
{
        int data;
        struct node *link;
};

void push(node **t, int item)
{
        node *temp;
        temp=(node *)malloc(sizeof(node));
        temp->data=item;
        if(*t==NULL)
                *t=temp;
        else
        {
                temp->link=*t;
                *t=temp;
        }
}

void pop(node **t)
{
        node *temp;
        if(*t==NULL)
                printf("\nStack Underflow\n");
        else
        {
                temp=*t;
                (*t)=(*t)->link;
                free(temp);
        }
}

void display(node **t)
{
        if(*t==NULL)
                printf("\nEmpty Stack\n");
        else
        {
                while(*t!=NULL)
                {
                        printf("\n%d\n", (*t)->data);
                        *t=(*t)->link;
                }
        }
}

void main()
{
        node *top;
        int i,n;
        char c;
        printf("\nWelcome\n");
        do
        {
                printf("\nEnter 1 for PUSH, 2 for POP, 3 for DISPLAY\t");
                scanf("%d", &i);
                switch(i)
                {
                        case 1:
                        {
                                printf("\nEnter the value to be pushed=");
                                scanf("%d", &n);
                                push(&top,n);
                                break;
                        }

                        case 2:
                        {
                                pop(&top);
                                break;
                        }

                        case 3:
                        {
                                display(&top);
                                break;
                        }

                        default:
                        {
                                printf("\nYou are given no other choice than 1,2 and 3\n");
                                printf("\nPlease try again\n");
                                break;
                        }
                }
                printf("\nDo you wish to continue?[y/n]\t");
                c=getch();
                printf("\n");
        }while(c=='y'||c=='Y');
        printf("\nThank You, Press Any Key To Exit\n");
        getch();
}


xpi0t0s 20Nov2008 21:12

Re: Stack Implementation using Linked list
 
This thread is just for discussing the article at the top. Please start a new thread for anything that is NOT directly about the article (and "I have a similar program that's failing" is about your similar program, not the article).

back from retirement 20Nov2008 21:31

Re: Stack Implementation using Linked list
 
Oh......excuse me.....I haven't seen it....sorry....cannot it be moved??? If possible, then please move it...

xpi0t0s 20Nov2008 22:52

Re: Stack Implementation using Linked list
 
Just start a new thread; it's easy, go to the relevant forum and click New Thread.

shabbir 20Nov2008 22:58

Re: Stack Implementation using Linked list
 
Quote:

Originally Posted by back from retirement (Post 39244)
Oh......excuse me.....I haven't seen it....sorry....cannot it be moved??? If possible, then please move it...

Moved it for you as of now.

xpi0t0s 21Nov2008 01:09

Re: Problem in Stack Implementation using Linked list
 
What exactly does "*t=(*t)->link;" do in display()?
When display() returns, what will "top" contain?

back from retirement 21Nov2008 18:46

Re: Problem in Stack Implementation using Linked list
 
Quote:

Originally Posted by xpi0t0s
What exactly does "*t=(*t)->link;" do in display()?
When display() returns, what will "top" contain?

By *t=(*t)->link; we actually move the pointer to a pointer t to the next node, in this way, beginning from the top, we traverse the whole list, displaying the data contained in each node...

When display ends its returning, top contains nothing since it point s NULL....

xpi0t0s 21Nov2008 23:34

Re: Problem in Stack Implementation using Linked list
 
> When display ends its returning, top contains nothing since it point s NULL

Is that what should happen?

xpi0t0s 21Nov2008 23:34

Re: Problem in Stack Implementation using Linked list
 
(By the way, these are hints, not me not knowing what's going on.)

back from retirement 22Nov2008 20:09

Re: Problem in Stack Implementation using Linked list
 
Oh...now I guess there must be some error in my concept....


All times are GMT +5.5. The time now is 04:11.