السلام عليكم ورحمة الله وبركاته
#include <iostream>
using namespace std ;
const int size = 10 ;
//prototypes
int IsFull(struct stack s) ;
int IsEmpty(struct stack s) ;
void push(stack &s, int c) ;
int pop(stack &s) ;
void inv(stack &s) ;
void print(stack s) ;
//struct defenation
struct stack{
int top ;
int iteam[size] ;
};
int main ()
{
cout << "\n\n\t-------------------------------------------" ;
cout << "\n\t| |" ;
cout << "\n\t| |" ;
cout << "\n\t| This program for REVERSE the |" ;
cout << "\n\t| Elements of stack |" ;
cout << "\n\t| |" ;
cout << "\n\t| |" ;
cout << "\n\t| |" ;
cout << "\n\t--------------------------------------------\n\n\t" ;
stack s ;
s.top = -1 ;
int x ;
cout << "Enter the Value of the orginal stack : \n\n\t" ;
for (int i = 0 ; i < size ; i++)
{
cin >> x ;
push (s, x) ;
}
inv(s) ;
print(s) ;
return 0 ;
}
int IsFull(stack s)
{
return (s.top == size - 1) ;
}
int IsEmpty(stack s)
{
return (s.top == -1) ;
}
void push (stack &s, int c)
{
s.iteam[++s.top] = c ;
}
int pop(stack &s)
{
return (s.iteam[s.top--]) ;
}
void inv(stack &s)
{
stack s1, s2, ;
s1.top = -1 ;
s2.top = -1 ;
while(!IsEmpty(s))
push(s1, pop(s)) ;
while(!IsEmpty(s1))
push(s2, pop(s1)) ;
while(!IsEmpty(s2))
push(s, pop(s2)) ;
}
void print(stack s)
{
cout << "\n\n\tThe shpae of stack after REVERSE the orginal stack : \n\n\t" ;
while(!IsEmpty(s))
cout << pop(s)<< " " ;
cout << "\n\n" ;
}البرنامج لايعكس المكدس الاصلي رغم اني راجعت الخوارزمية اكثر من مره وتأكدت من فعاليتها
عندما اقوم بتعديل هذه الدالة الى الشكل التالي
void inv(stack &s)
{
stack s1, s2, s3 ;
s1.top = -1 ;
s2.top = -1 ;
s3.top = -1 ;
while(!IsEmpty(s))
push(s1, pop(s)) ;
while(!IsEmpty(s1))
push(s2, pop(s1)) ;
while(!IsEmpty(s2))
push(s3, pop(s2)) ;
while(!IsEmpty(s3))
push(s, pop(s3));
}يطبع عكس المكدس
ارجوا ايضاح خطأي
وشكراً مقدماً