473,414 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Help understanding stacks

2
I need help understanding how a stack works in the following code

stack1.push(10)
stack1.push(8)
stack2.push(2)
stack1.push(3)

while !(stack1.isempty())
x=stack1.top;
stack1.pop();
stack1.pop();
stack2.push(x);

My interpretation is I first push all elements into either stack 1 or 2.

So stack 1 would be:
3
8
10

Stack 2 would be
2

Then after the first loop
since Stack1 is not empty x =3
stack1.pop(); will pop 3
stack1.pop(); will pop 8
leaving 10 as x?? and then I push that over to stack 2 leaving stack 1 empty??

I guess the consecutive pop() arguments are making it confusing.

Can anyone help clarify if this correct?
Mar 5 '07 #1
5 1833
hi guy
i think u got it wrong value of x will not be 10 but 3 becaz we have not change the assignment of x after 2nd stack1.pop()
this will push 3 over 2 in stack2.
in next loop stack1.top will be 1.
so 1st stack1.pop() will pop 10
next stack1.pop() will go in vain and this push 1 in stack2.
loop end here.
Mar 5 '07 #2
flaca2
2
Thank u for the help.
I got a little confused with your statement
"this will push 3 over 2 in stack2.
in next loop stack1.top will be 1."
After it pushes 3 over to stack 2 would stack1.top not be 8 instead of 1?
Mar 5 '07 #3
lqdeffx
39
FILO ( first in last out )
As it was once explained to me one, you are stacking plates so the last thing you place onto your stack, is ontop. so with that said:

stack1 is

10
8
3

and the pop function takes from the top!
Mar 5 '07 #4
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. stack1.push(10)
  2. stack1.push(8)
  3. stack2.push(2)
  4. stack1.push(3)
  5.  
  6. while !(stack1.isempty())
  7. x=stack1.top;
  8. stack1.pop();
  9. stack1.pop();
  10. stack2.push(x);
First of all, this little section of code is an error and will result in an infinite loop. There are no curly brackets around the loop, so only the first statement is executed - x is continually assigned stack1.top, andsince the stack never changes, the loop condition never changes. However, I understand that this is likely a typo, and so I'm assuming you meant to write

Expand|Select|Wrap|Line Numbers
  1. while (!(stack1.isEmpty())) {
  2.    x = stack1.top; // should be stack1.top()?
  3.    stack1.pop();
  4.    stack1.pop();
  5.    stack2.push(x);
  6. }
My interpretation is I first push all elements into either stack 1 or 2.

So stack 1 would be:
3
8
10

Stack 2 would be
2
That is correct.

Then after the first loop
since Stack1 is not empty x =3
stack1.pop(); will pop 3
stack1.pop(); will pop 8
leaving 10 as x?? and then I push that over to stack 2 leaving stack 1 empty??
Here, you make two mistakes. In one execution of the loop, x is assigned only once, at the beginning, to stack1.top. That is currently 3. So x is an integer variable holding the value 3. Then you pop two values - 3 and 8 - leaving stack1 to hold 10 and stack2 to hold 2. x still holds 3! It has not been assigned anything else! You then push x (3) onto stack2. So after 1 execution, stack1 holds 10, and stack2 holds 3, 2.

Can you see what will happen in the second execution of the loop?
Mar 5 '07 #5
hi flaca2
let me put in more clear way what i said..
exactly two lines are independent.
we push in stack2 so stack two will be

______
3
-------------
2
_______

stack1.top will be equal to no. of entry in stack 1
stack 1 will look like this


_________
1 <----------------stack1.top = 1(no of entry).
__________
Mar 8 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Madhu Gopinathan | last post by:
Hi All, I am faced with a horrible hang problem. I have a COM exe server that executes some tasks. The task execution manager is a thread that manages the pool of threads, which is 4 per processor....
1
by: LedZep | last post by:
This program has to use a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to ignore spaces, case sensitivity and...
10
by: Rich Kucera | last post by:
Holding all versions at 5.0.4, Multiple stacks with multiple-version configurations inevitable Will have to wait to see what the impact of problems such as http://bugs.php.net/bug.php?id=33643 ...
11
by: abico | last post by:
Write a program that reads in a sequence of words and prints them in reverse order.Using STACK.
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
2
by: Daniel | last post by:
Hi, I have a question regarding the memory managment in stl stacks. I want to use stacks to store a very large amount of numbers (some millions), thus I'm interested in how the stack behaves...
0
by: raghuveer | last post by:
i want to implement multiple stacks using arrays..I am able to create ,insert and print them but not poping an element form any of the stack..This is what i have #include<stdio.h>...
5
by: satan | last post by:
I need a help in my method equalStack in my class StackClass. public class StackClass { private int maxStackSize; //variable to store the maximum ...
5
by: dav3 | last post by:
I am by no means an ultra slick programmer and my problem solving skills.. well they leave much to be desired. That being said I have been working on the following problem for the past few days and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.