Hello, I'm supposed to be writing a program that sorts the largest elements of a stack with random numbers into a stack called ordered. I believe I have the program correct, however I can't get it to execute the print function at the end or to sort the other stacks?(I actually don't know if it has sorted the stacks seeing as I can't get it to print, yet I'm calling the public function the correct way). Any help would be appreciated, I've included the application file code and the header file code below.
#include <cstdlib>
#include <iostream>
#include "stack.h"
usingnamespace std;
int main(int argc, char *argv[])
{
stack ordered;
stack temp1;
stack temp2;
int number;
int item;
cout << "Enter the number of elements in the stack: ";
cin >> number;
for (int i=0; i < number; i++)
{
item = rand()%100 + 1;
temp1.push(item);
}
cout<<"Stacks at the start: ";
ordered.print();
temp1.print();
temp2.print();
while(temp1.empty() == false && temp2.empty() == false)
{
if(temp2.empty())
{
stack temp = temp1;
int largest = temp1.pop();
while(temp1.empty() == false)
{
int element = temp1.pop();
if (largest < element)
largest = element;
}
while(temp.empty() == false)
{
int element = temp.pop();
if (element!=largest)
temp2.push(element);
}
ordered.push(largest);
}
else
{
stack temp=temp2;
int largest = temp2.pop();
while(temp2.empty() == false)
{
int element = temp2.pop();
if (largest < element)
largest = element;
}
while(temp.empty() == false)
{
int element = temp.pop();
if (element!=largest)
temp1.push(element);
}
ordered.push(largest);
}
ordered.print();
temp1.print();
temp2.print();
}
system("PAUSE");
return EXIT_SUCCESS;
}
// implementation file for the stack class
#include <iostream>
#include <cstdlib>
usingnamespace std;
constint stack_size = 1000;
class stack
{
private:
int data [stack_size]; // data for the stack
int top; // index of the top of the stack
public:
stack (); // creates an empty stack
void push (int item); // puts item on the top of the stack
int pop (); // removes and returns the top of the stack
bool full (); // returns true if the stack is full
bool empty (); // returns true if the stack is empty
void print ();
};
// constructor creates an empty stack
stack::stack ()
{
top = -1;
}
// push adds a new elment, item, to the top of the stack
void stack::push (int item)
{
// if the stack is full, print an error message
if (full ())
{
cout << "\n\nStack Error: Pushing on a full stack";
cout << "\nThe element being pushed was: " << item;
}
else // OK to push an element
{
top++;
data [top] = item;
}
}
// pop removes and returns the top element of the stack
int stack::pop ()
{
// if the stack is empty, print an error message
if (empty ())
{
cout << "\n\nStack Error: Popping an empty stack";
cout << "\nReturning ?";
return'?';
}
else // OK to pop
{
top--;
return data [top + 1];
}
}
// empty returns true if the stack is empty, else it returns false
bool stack::empty ()
{
return top == -1;
}
// full returns true if the stack is full, else it returns true
bool stack::full ()
{
return top == stack_size - 1;
}
void stack::print()
{
int item;
if (empty())
{
cout << "This Stack Is Empty.";
}
while(!empty())
{
item=pop();
cout << item << " ";
}
cout << endl;
}
You cannot go first time into a while loop while(temp1.empty() == false && temp2.empty() == false)
because the temp2 is empty by default, hence you cannot have an access to sorting process and final print functions.
And why did you put print functions into a loop? Do you want to print elements every time?
Okay, I had changed that like you said, and it still skipped the while loop and didn't print out at the end(well actually if the while loop was skipped it wouldn't print again because it's inside), but that's besides the point, it's supposed to print each time something is moved from one stack to another.