menu driven program to perform the following stack operations using a linked list: push pop and display elements.

#include<iostream.h>//header file i/o stream flow 
#include<conio.h> 

class Stack 
{ 
int A[10]; 
int top; 
public: 
Stack() 
{ 
top=-1; 
} 
void push(); 
void pop(); 
void show(); 
}; 

void Stack::push() 
{ 
int x; 
cout<<"Enter the value to push"; 
cin>>x; 
if(top==-1) 
top=0; 
A[top++]=x; 
} 

void Stack::pop() 
{ 
cout<<top; 
cout<<" \n Element Poped is:"<<A[--top]; 
} 
void Stack::show() 
{ 
cout<<"\n top:"; 
for(int i=top-1;i>=0;i--) 
cout<<A[i]<<"\t"; 
} 
void main() 
{ 
int choice; 
Stack s; 
do 
{ 
cout<<"\n 1 push "; 
cout<<"\n 2 pop "; 
cout<<"\n 3 show"; 
cout<<"\n 4 exit"; 
cout<<"\n input your choice:"; 
cin>>choice; 
switch(choice) 
{ 
case 1: 
s.push(); 
break; 
case 2: 
s.pop(); 
break; 
case 3: 
s.show(); 
break; 
default: 
cout<<"\n invalid input "; 
} 
}while(choice !=4); 
getch(); 
} 

is this program correct according to the question?

Recommended Answers

All 3 Replies

No. It says you have to implement this with a linked list.

#include<iostream.h>
#include<conio.h>

class node
{
int d;
node next;
}

class linklist
{
node head;
linklish()
{
head=null;
}
void createnode(int value)
{
if(head==null)
{
node n=new node();
n.d=value;
n.next=null;
head=n;
}
else
{
node n=new node();
n.d=value;
n.next=null;
node move=head;
while(move.next!=null)
move=move.next;
move.next=n;
}
}

int deletenode()
{
int data=-1;
node move=head;
if(move==null)
return -1;
if(move.next==null)
{
data=move.d;
head=null;
return data;
}
node pre=null;
while(move.next!=null)
{
pre=move;
move=move.next;
}
data=move.d;
pre.next=null;
return data;
}

}


main()
{
linklist ll=new linklist();
ll.createnode(2);
ll.createnode(4);
cout<<ll.deletenode();
getch();
}

is this the program which the question wants?

No. You need to create a stack that uses a linked list for its data structure. You also need to indent you code correctly. If you look at the first post in your linked list thread the way the code is formatted there is how you should format your code when you post. I personally won’t read code that has no indentation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.