November 28th, 2012, 05:04 AM
Function to Sort Stack - high to low with 2 temp stack
please help , i dont now what to do ,
we get queshtion that we need to :
sort stack with high to low sorted,
with no Array!!!
some suggest me to write program with 2 stack ,
1- to sorted
2- for temp stack to help me sorted numbers to stack 1
please help me
November 28th, 2012, 05:55 AM
Lets name the temporary stacks as A and B. They are initially empty, of course.
1) examine the first element of the original stack: let's call it F; if empty go to 6
2) while the top element of A is bigger than F move it to B
3) move F to A (pop from original stack, push to A)
4) move all elements in B to A (pop from B, push to A)
5) go to 1
6) move all elements of A to the original stack
Of course you can change the comparison to order the other way around ... and save on stack transfers with some clever tests.
November 28th, 2012, 06:58 AM
first of all , thank u that u reply me an anser,
i now the algoritem on my paper but how can i write the function to do it ?!
please help me to write just the function.
November 28th, 2012, 07:12 AM
No "copy/paste" programming here.
Originally Posted by tsahar
1) Write the function.
2) Publish it here with a comment about what does not work, the input, expected output and actual output ...
3) Wait for comments from fellow programmers.
4) Inprove your code with the presented suggestion.
5) if still not happy, go back to step 2
November 29th, 2012, 12:28 AM
function to - sort stack with 2 temp stack, no array!!!
02 # include<string.h>
03 # include<conio.h>
04 # include <stdlib.h>
05 # define N 10
07 void my_sort(stackint a, int head)
09 int /*a[N],*/ sorted[N] , help[N];
10 int head1 , head2 , head3, num, nums;
15 while (!is_empty(a))
17 num = pop(a);
18 nums = top(sorted);
19 if (num>= nums) push(sorted,num);
22 while (num< nums)
25 nums = top(sorted);
26 } // while
34 //getch = ch;
November 29th, 2012, 04:41 AM
You're creating arrays at line 9
Also, use code tags when posting code ... and lose the line numbers.