473,387 Members | 1,569 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,387 software developers and data experts.

Stacks in C++

17
I am creating a program that takes a specified number of nodes in a stack...from here, certain nodes are to be removed based on criteria within the node. Once these certain nodes are removed, they are to be output to the screen. Then, the remaining nodes are to be output. I have created both push and pop functions, and I also need a "remove" function. The remove function is to remove the top node that meets the criteria, and return its properties using reference parameters.

I am really new to stacks...I understand the functionality of push and pop. However, what I don't get is how I would remove these certain nodes and output them. I assume I need to 'pop' them all...? and from there remove the ones of interest? and push the remaining? I am obviously completely lost. Any suggestions anybody may have would be greatly appreciated. Thanks!!
Nov 27 '07 #1
4 1794
Ganon11
3,652 Expert 2GB
I am creating a program that takes a specified number of nodes in a stack...from here, certain nodes are to be removed based on criteria within the node. Once these certain nodes are removed, they are to be output to the screen. Then, the remaining nodes are to be output. I have created both push and pop functions, and I also need a "remove" function. The remove function is to remove the top node that meets the criteria, and return its properties using reference parameters.
What I interpret is that you are writing a function separate from, but dependent on, the stack class. In other words, you will need to use stacks and stack functions in this function.

So that we can felp you better, can I ask a clarification question?

Let's say each node is an int, and your 'criteria' is that the node must be even. So, we push the following values onto a stack (in this order):

Expand|Select|Wrap|Line Numbers
  1. 1, 5, 3, 2, 8, 9, 4, 6, 11
Now, in your remove, you want to get the first node that meets the criteria. So you pop, and get 11. 11 isn't odd, so you add that to some other data structure (another stack maybe? or a vector, or a list, or whatever). Then you pop 6. 6 is even, so you output 6 and then discard it. Continuing, you will output 6 4 8 2, and then 11 9 3 5 1. Did I get that?
Nov 27 '07 #2
aemado
17
Yes...sort of. I am writing a function in the stack class. This function has two parameters, passed by reference. As you used the example that I am looking for an even....I am looking for a node that meets a criteria of one of the parameters or the criteria of the other parameter. I want to find the first node in which either of these criteria are met. If it is met, I want to remove it from the stack...and therefore return the parameters for output (as it is passed by reference). I don't believe I have another stack or anything to pass it to...just to remove it from the stack, and output the node contents. I want to process through the stack until, like you said, I find all that meet that criteria...and output. Then, I want to output the remaining contents of the stack. In this remove function I will need a loop...as if none of the criteria are met, the two parameters will output -1. Therefore I will know when the stack processing has completed.

Here is what I am trying to accomplish in the main:

Expand|Select|Wrap|Line Numbers
  1. stack.remove(condition, brand);
  2. while( condition != -1)
  3. {
  4. cout<<condition<<brand;
  5. stack.remove(condition, brand);
  6. }
  7.  

I am just really unsure how to implement the remove function. I have created an if/else loop (though I'm not sure this is the right approach)....in which I use if to determine if the top->condition and/or top->brand criteria are met. But in else, I am lost. I think I should create another loop and tmp to move through the stack... and then, if none of the criteria are met, return -1 for both parameters.

Thank you so much for your help.

What I interpret is that you are writing a function separate from, but dependent on, the stack class. In other words, you will need to use stacks and stack functions in this function.

So that we can felp you better, can I ask a clarification question?

Let's say each node is an int, and your 'criteria' is that the node must be even. So, we push the following values onto a stack (in this order):

Expand|Select|Wrap|Line Numbers
  1. 1, 5, 3, 2, 8, 9, 4, 6, 11
Now, in your remove, you want to get the first node that meets the criteria. So you pop, and get 11. 11 isn't odd, so you add that to some other data structure (another stack maybe? or a vector, or a list, or whatever). Then you pop 6. 6 is even, so you output 6 and then discard it. Continuing, you will output 6 4 8 2, and then 11 9 3 5 1. Did I get that?
Nov 27 '07 #3
Ganon11
3,652 Expert 2GB
I am just really unsure how to implement the remove function. I have created an if/else loop (though I'm not sure this is the right approach)....in which I use if to determine if the top->condition and/or top->brand criteria are met. But in else, I am lost. I think I should create another loop and tmp to move through the stack... and then, if none of the criteria are met, return -1 for both parameters.
Have you tried doing this? What worked/didn't work? I should think you'd have to use a loop to access all the elements of the stack.

Now, you face a problem inherent in stacks - you can only access the top element. In order to get any other element, you will have to pop() it off the stack. But, if you don't want to lose that element (i.e. to be output later), you need to put it somewhere else. This is where the 'other data structure' comes in. So, in order to eventually access every element, you need to continually get the top element, pop that element from the stack, perform your check, and (possibly) put this element in a safe place for outputting later.
Nov 27 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Yes, Ganon11, I was thinking that a linked list was needed and not a stack.
Nov 27 '07 #5

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

Similar topics

5
by: Vanessa T. | last post by:
Hello All! Is there a place where I can learn stacks, (push and pop) etc. Thanks,
4
by: Ice | last post by:
Hi there, I'm not sure if this is the right group for this- If it isn't, could anyone point me in the right direction? For our data structures exam, we are usually asked to implement the...
6
by: Sathyaish | last post by:
I've searched Google and found a few, but I am not so satisfied. Any good reading on "stacks and heaps" about how and when memory is allocated from the heap?
18
by: pmm | last post by:
Hi all, Plz dont fire at me if this is a silly question Is there any way to know in which direction stack grows pmm
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 ...
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...
2
by: chubbykelly | last post by:
hi, i got a prob with the new topic my teacher in data struct taught us this afternoon. he introduced the concept of stacks. simulated how 5 elements will be pushed and popped out to the array...
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>...
8
by: cerise | last post by:
I can't figure out how to make and handle multiple stacks and use them so I could create four linked list stacks representing each suit of cards, one stack each for diamonds, hearts, spades, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.