C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

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




stack implementation trough linked list

 

stack implementation trough linked list

anis_ahmad

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

New D.I.C Head
*

Joined: 4 May, 2008
Posts: 9

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
+Quote Post


Psionics

RE: Stack Implementation Trough Linked List

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

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 158



Thanked: 11 times
Dream Kudos: 200
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
+Quote Post

AmitTheInfinity

RE: Stack Implementation Trough Linked List

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

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,355



Thanked: 67 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
+Quote Post

Reply to this topicStart new topic

Time is now: 11/21/09 07:23PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month