hello everybudy i'm writing a code to revers a stack but i have a wrong output when i Calling the reverse function can anyone help me please :) :)
header.h
class intnode
{
public :
intnode(int , intnode*);
int info;
intnode* next ;
};
class Stack
{
public:
Stack();
void DisplayStack();
int SizeOfStack();
int Pop();
void push(int);
bool IsEmpty();
void Reverc(Stack ,Stack,Stack);
bool Plandrome();
private:
intnode*top;
int NumberOfNodes;
};
cpp source
#include<iostream>
#include"Header.h"
#include<process.h>
using namespace std;
intnode::intnode(int x , intnode*p=0)
{
info = x ;
next = p;
}
Stack::Stack()
{
top=0;
NumberOfNodes=0;
}
bool Stack::IsEmpty()
{
return top==0;
}
int Stack::SizeOfStack()
{
return NumberOfNodes;
}
void Stack::push(int x )
{
if(IsEmpty())
{
top=new intnode(x);
NumberOfNodes++;
}
else
{
top=new intnode(x,top);
NumberOfNodes++;
}
}
int Stack::Pop()
{
if(IsEmpty())
return NULL;
else
{
int x=top->info;
intnode*p=top;
top=top->next;
delete p;
NumberOfNodes--;
return x;
}
}
void Stack::Reverc(Stack s,Stack d,Stack c)
{
int x;
while(!s.IsEmpty())
{
x=s.Pop();
d.push(x);
}
while(!d.IsEmpty())
{
x=d.Pop();
c.push(x);
}
while(!c.IsEmpty())
{
x=c.Pop();
s.push(x);
}
}
void Stack::DisplayStack()
{
intnode*p=top;
cout<<"top -->"<<endl;
while(p!=0)
{
cout<<"\t|\t"<<p->info<<"\t|"<<endl;
p=p->next;
}
cout<<"\t --------------"<<endl;
}
void main()
{
Stack s ,d,c;
s.push(5);
s.push(7);
s.Pop();
s.push(77);
s.push(44);
s.Reverc(s,d,c);
s.DisplayStack();
cout<<"The Number Of Nodes In the stack is : "<<s.SizeOfStack()<<endl;
system("pause");
}