• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stack Data Structure

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone give me an idea where to use stack data structure,  example business, and how what the process aha. Its giving me a hard time, sorry im a beginner
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is a GREAT article
https://www.guru99.com/stack-vs-heap.html

I use functions for a lot of things -- where local variables are set and used and forgotten -- clean and simple.

The main time I use a heap is when I calloc a struct.  Say, for example, I have to scan the database and I do not know how many records I have UNTIL I run the program.  I calloc the size at run time and then free when done with it.  Just remember to clean up after yourself with any *alloc.  The heap is great for sorting and such.

Real-Life Example:
I loop through a list of payments in a function (using the stack to loop through the records sequentially)
I use the calloc struct (heap) to update payment for a date range
I use calloc struct to sort and print and then free
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid that article doesn't answer the OP's question. It talks about a call stack, not the data structure with a similar name. There are three main kinds of linear data structure:-
  • 1: The sequence or list: this allows access to any of its items. Sequences/lists based on an array usually allow random access, i.e. every element can be found as quickly as another.
  • 2: The queue. this allows access to its oldest item (first in first out).
  • 3: The stack: this allows access to its newest (“top”) element (first in last out, or last in first out).
  • It is quite unusual to need access to the newest item, except when writing parsers.

    Are you wrriting a stack in C/C++? I think this topic would fit better in another forum so I shall add it there.
     
    Rancher
    Posts: 508
    15
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    It is quite unusual to need access to the newest item, except when writing parsers.


    ...or programming in FORTH:
    https://en.wikipedia.org/wiki/Forth_(programming_language)

    which (still) is a stack-based language. And you didn't have much choice if you were learning to program on one of these:
    https://en.wikipedia.org/wiki/Jupiter_Ace
     
    Campbell Ritchie
    Marshal
    Posts: 79180
    377
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    John Matthews wrote:...or programming in FORTH: . . .

    Don't scare them I once won a bottle of wine for a Forth program.
     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    well, here's the thing.  The structure tells you when to use it - when you need to access the most recent item put into a group.

    In business accounting, there are two main methods used for keeping track of inventory costs.  First In, First Out (FIFO), or Last In, First Out (LIFO).  Say I buy 100lbs of steel at $1 per pound.  a week later, I buy another 100lbs of steel at $1.15 per pound.  The next week, I USE 80 lbs of steel. I want to record the expense of my inventory used to make $100 worth of my product.  I could use FIFO, and say that steel cost me $80.  That gives me a bigger profit today of $20 ($100 in product - $80 in material), but also leaves me with more money tied up in inventory ($20 of $1/lb steel, $115 in $1.15lb steel == $135) of materials not being used.

    OR, I could use LIFO and say that steel used cost me $92 (80lbs at $1.15/lb).  Now my profit today is less  ($8), since my materials cost a bit more, but I have less money tied up in inventory (20lb * $1.15 + 100lb * $1/lb == $123) .

    If i recall correctly, a business can use either method of LIFO or FIFO, but once they pick one, they have to stick with it.
     
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The most common (and by this I mean the least rare) use case I personally have for using stacks is when I'm processing a dynamic tree-like model (such as an XML DOM) and I want to access information about the processed ancestor nodes in the method call that processes the current node.

    Here's an example (in Java):
     
    wally thurston
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As soon as I saw 'stack' -- the first thing I thought of was calloc ;)

    Thanks for another set of eyes -- I swear my glasses must have been (still are) a bit dirty.
     
    Campbell Ritchie
    Marshal
    Posts: 79180
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    wally thurston wrote:. . . Thanks for another set of eyes -- I swear my glasses must have been (still are) a bit dirty.

    Hahahahahaha! It is very easy to get confused about what people are asking; I do it myself quite frequentlly.
    reply
      Bookmark Topic Watch Topic
    • New Topic