Problem in Stack Implementation using Linked list

Contributor
20Nov2008,19:50   #1
back from retirement's Avatar
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();
}
Mentor
20Nov2008,21:12   #2
xpi0t0s's Avatar
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).
Contributor
20Nov2008,21:31   #3
back from retirement's Avatar
Oh......excuse me.....I haven't seen it....sorry....cannot it be moved??? If possible, then please move it...
Mentor
20Nov2008,22:52   #4
xpi0t0s's Avatar
Just start a new thread; it's easy, go to the relevant forum and click New Thread.
Go4Expert Founder
20Nov2008,22:58   #5
shabbir's Avatar
Quote:
Originally Posted by back from retirement View Post
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.
Mentor
21Nov2008,01:09   #6
xpi0t0s's Avatar
What exactly does "*t=(*t)->link;" do in display()?
When display() returns, what will "top" contain?
Contributor
21Nov2008,18:46   #7
back from retirement's Avatar
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....
Mentor
21Nov2008,23:34   #8
xpi0t0s's Avatar
> When display ends its returning, top contains nothing since it point s NULL

Is that what should happen?
Mentor
21Nov2008,23:34   #9
xpi0t0s's Avatar
(By the way, these are hints, not me not knowing what's going on.)
Contributor
22Nov2008,20:09   #10
back from retirement's Avatar
Oh...now I guess there must be some error in my concept....