Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,150 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,884 people online right now. Registration is fast and FREE... Join Now!




stack implementation trough linked list

 
Reply to this topicStart new topic

stack implementation trough linked list

anis_ahmad
post 7 Oct, 2008 - 09:51 PM
Post #1


New D.I.C Head

*
Joined: 4 May, 2008
Posts: 7

I am trying to write a program for stack implementation through linked list.
my code is given below :
It gives an error message :void * can not convert in node *

code

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include"malloc.h"
struct node{
int data;
struct node *link;
};
struct node *top;
void main()
{
int will=1,i,numb;
int pop();
void push(int);
void display();
top=NULL;
clrscr();
printf("program for stack linked list demo\n\n");
while(will==1)
{
printf("MAIN MENU\n\n 1. push insert element on the stack\n 2. pop delete item from stack\n");
scanf("%d",&will);
switch(will)
{
case 1:
printf("enter the data....\n");
scanf("%d",&numb);
push(numb);
display();
break;
case 2:
i=pop();
printf("returned value from pop is %d\n",i);
break;
default:
printf("invalid choice\n ");
}
printf("do you want to continue \n" " 1 for yes and any other key to exit\n");
scanf("%d",&will);
}
}
void push(int y)
{
struct node *x;
x=malloc(sizeof(struct node));
printf("address of newly created node is %d",x);
x->data=y;
x->link=top;
top=x;
}
void display()
{
int i=0;
struct node *temp;
temp=top;
while(temp!=NULL)
{
printf("Item no. is %d : Data %d Link %d",i++,temp->data,temp->link);
temp=temp->link;
}
}
int pop()
{
int a;
if(top==NULL)
{
printf("stack empty\n");
return 0;
}
else
{
a=top->data;
printf("the returned value is %d",a);
free(top);
top=top->link;
return(a);
}
}

User is offlineProfile CardPM

Go to the top of the page

Psionics
post 7 Oct, 2008 - 10:17 PM
Post #2


D.I.C Head

Group Icon
Joined: 6 Sep, 2008
Posts: 122



Thanked 1 times

Dream Kudos: 100
My Contributions


Please post your code in the appropriate tags from now on! Also, let us know where the error is occurring, and then only post the necessary code ( for example, the function that you're having a problem with ).

Aside from this, your problem is the way you're getting the size of node, and then storing that value inside your variable, so try this:
cpp

void push( int y )
{
struct node *x = mallac( sizeof( node ) );
printf( "address of newly created node is %d", x );
x->data = y;
x->link = top;
top = x;
}


Hope it helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 7 Oct, 2008 - 10:32 PM
Post #3


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


See else block in pop function

cpp

else
{
a=top->data;
printf("the returned value is %d",a);
free(top); // You are releasing the top node! without traversing ahead?
top=top->link; // If you have already released the top, how you will get top->link?
return(a);
}


also here

cpp

void push(int y)
{
struct node *x;
x=malloc(sizeof(struct node)); // I would suggest to have a typecasting to (struct node *) before malloc as it returns a void * .
printf("address of newly created node is %d",x);
x->data=y;
x->link=top;
top=x;
}


I hope this will help you. smile.gif

This post has been edited by AmitTheInfinity: 7 Oct, 2008 - 10:32 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 01:30PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month