+ 1

operations on stack in c

I have written the following code to perform various operations on stack like pop,push etc. I have provided an option to reverse the stack using pop but it is not working due to some logical error. https://code.sololearn.com/cnMW7TP52O1T/?ref=app

16th Aug 2018, 11:48 PM
harshit
harshit - avatar
15 Antworten
+ 4
Works fine, if you used temporary like I said. https://code.sololearn.com/cA0TmQLbEzjB Note: function return is useless. setting the memory beyond the array to it's current contains does nothing.
17th Aug 2018, 10:38 AM
John Wells
John Wells - avatar
+ 7
harshit static vs non static globals won't make a difference for your program.
17th Aug 2018, 10:20 PM
John Wells
John Wells - avatar
+ 3
Your biggest issue with reverse is modifying the stack while attempting to read it. Create an a[5] so you can pop into that and, once the stack is empty, push the values back on stack in reverse. As a side note, main shouldn't know anything about the stack details. OOP principles should be maintained even though the languange doesn't support classes. Your stack data should be static data that only your stack functions touch. harshit updated... The stack shouldn't know about where the data pushed is coming from. main should read and then push.
17th Aug 2018, 7:45 AM
John Wells
John Wells - avatar
+ 3
If you want to, you could also directly copy in x by swapping. int z,c; c=top1/2; for(int i=0;i<=c;i++) { z=x[i]; x[i]=x[top1-i]; x[top1-i]=z; }
17th Aug 2018, 8:05 AM
John Wells
John Wells - avatar
+ 3
This is how I'd do it to just support integers. It could easily be updated to support any type by: - providing size to the stack function, - void* for data value in push function, - pop function returns 0/-1 error code and is passed in a callback to copy data being popped, and - peek & print are passed in a callback to display the data instead of doing it in the functions (null would be used for empty/done.) https://code.sololearn.com/ccU219Rl1XfD
17th Aug 2018, 11:57 AM
John Wells
John Wells - avatar
+ 3
Okay, I couldn't help myself... The stack now supports any type. I hard coded usage with both characters and integers. https://code.sololearn.com/c8JqT3XI9l8b
17th Aug 2018, 4:08 PM
John Wells
John Wells - avatar
+ 2
That's fine use the temporary array to hold the popped data and push it back on after you got it all off.
17th Aug 2018, 9:01 AM
John Wells
John Wells - avatar
+ 2
It is being updated. I pushed 3 values prior to reverse and it counted down 1,0,-1 after set call.
17th Aug 2018, 10:14 AM
John Wells
John Wells - avatar
+ 2
If x[0]=5,x[1]=6,x[2]=7, you change x[0] to 7. The 5 is lost as it is 7 already when popped.
17th Aug 2018, 10:21 AM
John Wells
John Wells - avatar
+ 2
Thank you so much.
17th Aug 2018, 10:57 AM
harshit
harshit - avatar
+ 1
In reverse I am returning value given by pop.So I can't return x[5]. The problem is that how can I keep track of value of top because the code involves lot of loops and complexity.
17th Aug 2018, 5:47 AM
harshit
harshit - avatar
+ 1
John Wells sir, please take a look at my code.
17th Aug 2018, 5:47 AM
harshit
harshit - avatar
+ 1
The problem is with the statement top1=set(); in reverse function. It is not updating value of top1.
17th Aug 2018, 10:00 AM
harshit
harshit - avatar
+ 1
John Wells sir,is it necessary in my code to declare t as static global variable or I can also declare it as only global variable because I am getting right answer from both. https://code.sololearn.com/cbxCpcmvi7TE/?ref=app
17th Aug 2018, 10:09 PM
harshit
harshit - avatar
0
But I need to use pop for reverse.
17th Aug 2018, 8:47 AM
harshit
harshit - avatar