stack problem. help!!
I want to display input,delete and show in stack, but there a problem in switch case loop, expected before int please help me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
};
class stack
{
private:
node *top;
node *head;
public:
stack() // constructor
{
top=NULL;
}
void push(int); // to insert an element
void pop(); // to delete an element
void show(); // to show the stack
bool isEmpty ();
int stackTop ();
};
void stack::push(int item)
{
node *newnode;
newnode =new (node);
if(newnode==NULL)
cout<<"cannot allocate memory" <<endl;
else
{
newnode ->data= item;
newnode ->next= head;
head = newnode;
}
}
int stack::stackTop()
{
if (isEmpty())
cout <<"Sorry,Stack is still empty!"<<endl;
else
return head->data;
}
void stack::pop()
{
node *delnode;
if (isEmpty())
cout <<"Sorry, Cannot pop item from stack.Stack is still empty!" <<endl;
else
{
delnode = head;
cout << "Item to be popped from stack is: " << stackTop()<<endl;
head = delnode->next;
delete(delnode);
}
}
void stack::show()
{
node *top;
while(top>=0)
{
cout<<"[top]"<<endl;
top--;
}
}
int main()
{
stack s;
int choice;
while(1)
{
cout<<"\n";
cout<<"\n\t\tSTACK MENU\n\n";
cout<<"1:PUSH, 2:POP, 3:SHOW STACK, 4:EXIT";
cout<<"\nChoose the menu: ";
cin>>choice;
switch(choice)
{
case 1:
s.push(int);
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
exit(0);
break;
default:
cout<<"Please choose correct menu!";
break;
}
}
return 0;
}
|
Last edited on
You need another cin where the user enters the value to push:
1 2 3 4 5 6 7 8 9 10
|
switch(choice)
{
case 1:
{
int value;
cout<<"\nValue: ";
cin>>choice;
s.push(value);
break;
}
|
Topic archived. No new replies allowed.