Hello people...hope your holidays was great...but now I'm back with another problem with sorting a Stack..I got the program to run smoothly but I just cant get it to "Swap and Sort" the output of the "Popped" stack..example..if the popped output is D, E, A, B, the final output should read A, B, C, D,.....can anyone help me please...
#include<iostream>
usingnamespace std;
constint MAXSTACK=100;
constint TRUE=1;
constint FALSE=0;
// Class Declaration
class Stack
{
private:
int top;
int num[MAXSTACK];
public:
Stack(); // constructor
void push(int);
int pop();
int isempty();
int isfull();
};
// implementation section
Stack::Stack()
{
top=-1; // initialize the top-of-stack position current position
}
void Stack::push(int value)
{
top++; // increment the index in top
num[top]=value; // store the value
}
int Stack::pop()
{
int topval;
topval=num[top]; // retrieve the top element
top--; // decrement the index stored in top
return(topval);
}
int Stack::isempty()
{
if(top==-1)
return TRUE;
elsereturn FALSE;
}
int Stack::isfull()
{
if(top==MAXSTACK-1)
return TRUE;
elsereturn FALSE;
}
//
int main()
{
Stack digits; // define a Stack named digits
int newnum;
cout<<"Enter as many digits as you wish, one per line."
<<"\nTo stop entering digits, enter a number greater than 9. \n";
while(1)
{
cout<<"Enter a digit: ";
cin>>newnum;
if(newnum>9)
break;
if(digits.isfull()) //check overflow
{
cout<<"\nNo more storage allocation space."
<<"\nThe last digit has not been entered on the stack."<<endl;
break;
}
else
digits.push(newnum); // push value onto the stack
}
// pop and display digits from the stack.
cout<<"\nThe values popped from the stack are: \n";
while(!digits.isempty()) // check the underflow
{
cout<<digits.pop()<<endl;
}
return 0;
}
That's Understandable in your case; but still when you swap and then sort it, it will still print out in a stack; But instead of printing the digits out of order (in a stack), it will print in descending order (in a stack).
Hello you guys...I agree with the feed backs and opinions...but the point of the matter is that I don't have no other choice in this matter but to do it...so I would deeply appreciate it if any of you guys could help me or at least point me in the right direction for me to finally solve this problem...I even went as far as this....but still I'm lost...Thanks
The array that you are sorting is just an array which happens to be encapsulated within a class called "stack". Sorting the array is no different than sorting any other kind of array. Conceptually sorting a stack will break the stack which defeats the purpose of the stack. Looks like you are doing the simplest kind of sort there is which is okay if you really want to sort the array. You could just search the web and copy and paste a sort solution. however when you sort the stack you have broken your contractual obligation with the object's users. Then again perhaps your class is simply named incorrectly. It is named stack but is it really a functional stack or is it just a sorted array of numbers?
@OP: You aren't supposed to sort a stack. It's a LIFO structure. Sorting it destroys the structure's data flow.
If you are just creating an array wrapper class then you'd be better off naming it Array or something. Stack *implies* a LIFO/FILO flow. It's not just an ordinary set or list of numbers (I was apprehensive about using either of those words because they're both STL containers). If that's the case you can do a simple but inefficient sort like bubble, or the amazing quicksort (which is going to be slightly more difficult to code). You can just look it up online (wikipedia is fine). Here's a website with some pseudocode: http://www.sorting-algorithms.com/bubble-sort
(I didn't personally find this website, someone showed it to me)
#include <iostream>
#include <string.h>
usingnamespace std;
constint MAXSTACK=5;
constint TRUE=1;
constint FALSE=0;
class Stack
{
private:
int top;
char num[MAXSTACK];
public:
Stack();
void push(char);
char pop();
char isempty();
char isfull();
char sort();
};
Stack::Stack()
{
top = -1;
}
void Stack::push(char value)
{
top++;
num[top] = value;
}
char Stack::pop()
{
char topval;
topval=num[top]; // retrieve the top element
top--; // decrement the index stored in top
return(topval);
}
char Stack::sort()
{
char ch1, ch2;
ch1 = num[top];
ch2 = num[top-1];
if (ch1 < ch2)
return ch1;
elsereturn ch2;
}
char Stack::isempty()
{
if(top==-1)
return TRUE;
elsereturn FALSE;
}
char Stack::isfull()
{
if(top==MAXSTACK-1)
return TRUE;
elsereturn FALSE;
}
void bubble(char *string[], constint size)
{
int pass, i;
char *temp;
for (pass=0; pass < size - 1; pass++) {
for (i=0; i<size - 1; i++) {
if (strcmp(string[i], string[i+1]) > 0) {
temp = string[i];
string[i] = string[i+1];
string[i+1] = temp;
}
}
}
}
char main()
{
Stack digits; // define a Stack named digits
Stack one;
char *sorting = newchar[MAXSTACK];
//sorting = new char[sorting];
char newnum;
cout<<"Enter as many digits as you wish, one per line."
<<"\nTo stop entering digits, enter a number greater than 9. \n";
while(1)
{
cout<<"Enter a digit: ";
cin>>newnum;
if (newnum == 'x')
break;
if(digits.isfull()) //check overflow
{
cout<<"\nNo more storage allocation space."
<<"\nThe last digit has not been entered on the stack."<<endl;
break;
}
else
digits.push(newnum); // push value onto the stack
}
// pop and display digits from the stack.
cout<<"\nThe values popped from the stack are: \n";
while(!digits.isempty()) // check the underflow
{
for(int x = 0; x < MAXSTACK; x++)
{
char popval = digits.pop();
sorting[x] = popval;
cout<<popval<<endl;
}
}
bubble(sorting, MAXSTACK); // i get error c2664 here
cout<<"Sorted Array"<<endl;
for(int z=0; z<MAXSTACK; z++)
cout<<sorting[z]<<endl;
return 0;
}
i tried the bubble sort alone it works but for some reason i cannot pass the array "sorting" and end up getting error number c2664. any help would be grate thanx.