Problem in Stack Implementation using Linked list

back from retirement's Avatar, Join Date: Nov 2008
Contributor
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();
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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).
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
Oh......excuse me.....I haven't seen it....sorry....cannot it be moved??? If possible, then please move it...
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Just start a new thread; it's easy, go to the relevant forum and click New Thread.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What exactly does "*t=(*t)->link;" do in display()?
When display() returns, what will "top" contain?
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
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....
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> When display ends its returning, top contains nothing since it point s NULL

Is that what should happen?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
(By the way, these are hints, not me not knowing what's going on.)
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
Oh...now I guess there must be some error in my concept....