I try to make a coding to show a stack that state input, show and delete. But there is a mistake in the coding that i cant find by myself. Help me to solve this please. I need to know the answer
#include<iostream>
usingnamespace std;
class nodeStack
{
//int data;
//nodeStack *next;
public:
int data;
nodeStack *next;
};
class stack
{
//private: // pengisytiharan ahli data
nodeStack *top;
public:
stack()
{
top=NULL;
}
void push(); // to insert an element
void pop(); // to delete an element
void show(); // to show the stack
bool isEmpty ();
};
void push(int newitem)
{ // create newnode
nodeStack *head;
nodeStack *newnode;
newnode = new (nodeStack);
if( newnode == NULL)
cout << "Cannot allocate memory…"<< endl;
else // add to empty stack, or to front stack
{
newnode->data = newitem;
newnode->next = head;
head = newnode;
}// end if
} //
void pop()
{ //char stackTop ();
bool isEmpty();
nodeStack *head;
nodeStack *delnode;
stackTop();
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);
}// end else
}
int stackTop()
{
bool isEmpty();
nodeStack *head;
if (isEmpty())
cout <<"Sorry,Stack is still empty!"<<endl;
elsereturn head->data;
} // end check item at top
void show()
{
nodeStack *top;
nodeStack *newnode=top;
cout<<"\nThe stack is: ";
while(top!=NULL)
{
cout<<newnode->data<<" ->";
newnode=newnode->next;
}
cout<<"NULL\n";
}
//main function
int main()
{
stack s;
int choice;
while(1)
{
//cout<<"\n-----------------------------------------------------------";
cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
cout<<"1:PUSH, 2:POP, 3:DISPLAY STACK, 4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
exit(0);
break;
default:
cout<<"Please enter correct choice(1-4)!";
break;
}
}
return 0;
}