Sorting a Stack

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
using namespace std;
const int MAXSTACK=100;
const int TRUE=1;
const int 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;
	else
	return FALSE;
}

int Stack::isfull()
{
	if(top==MAXSTACK-1)
	return TRUE;
	else
	return 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;
}
Last edited on
I hope that you are mistaken, because Stack is a stack, I can't image why do you need to sort it, I think it's quit stupid idea.
There is no mistake here...believe you me when I say this...this can and must be done.
If you sort it, it's no longer a stack (FILO).
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).
I agree with Denis - a sorted stack just seems plain wrong
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Stack::Swap(int x,int y)
{
      int t=num[x];
      num[x]=num[y];
      num[y]=t;
}
void Stack::Sort()
{
    int s="size now";
      for(int i=s-1;i>0;i--)
	    for(int j=0;j<i;j++)
		 if(num[j]>num[(j)+1])
		  Swap(j,(j)+1); 
}
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <string.h>

using namespace std;
const int MAXSTACK=5;
const int TRUE=1;
const int 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;
	else
		return ch2;
}

char Stack::isempty()
{
	if(top==-1)
		return TRUE;
	else
		return FALSE;
}

char Stack::isfull()
{
	if(top==MAXSTACK-1)
		return TRUE;
	else
		return FALSE;
}

void bubble(char *string[], const int 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 = new char[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.
Topic archived. No new replies allowed.