I am studying stacks, queues, and heaps at the moment but i dont know how i can apply this in real life. Well my question is how can these may i say operations helpful and when are they really needed as opposed to others. I hope you can get my question well. Thanx a bunch

I am studying stacks, queues, and heaps at the moment but i dont know how i can apply this in real life. Well my question is how can these may i say operations helpful and when are they really needed as opposed to others. I hope you can get my question well. Thanx a bunch

If you need an easy way to relate to the data structures, you can think of a Queue as a type of line to wait in...

for example, when you're at the grocery store (or any place that gives some type of customer service) you're considered to be in a Queue-based system. If you were the first person in line, you are the first person to be served.

On the other hand, Stacks are Last-in first-out. Many businesses would not be successful if their lines were based on a "stack" system, even though it would be crazy funny to see people trying their hardest to be last in line just so they could be served first while people who came first would be waiting forever (or close to it).

A good example of a stack would be a set of heavy boxes. You stack one heavy box on top of another, and realize the first one (on the ground) is impossible to lift, so your best bet to be productive with the boxes it to remove the lightest one, which should always be the one on the top of the stack - the last one added.

As for Heaps, I still don't fully understand them. Moving along with the explanation...

Programs rely on data structures like stacks and queues.

A good example of how your program uses a stack is error-checking in your programs. How does the compiler know when you're missing a curly brace, or end of a method/class body? How is data processed/compiled? The answer is via a simple method of using a stack to "push" items that it runs across in the program (from left to right then up to down) and perform actions based on machine-language code.

Programs also utilized queues in an important way. Two very big examples are--

-Events
-Threads

--Where threads are simply a process of execution by the processor (an extra task to perform), and Events are simply messages sent from the user to the computer, in which the computer will take action based on the current parameters (if you're accessing the desktop, moving your mouse will send messages to the computer where your cursor will move... clicking on an icon will highlight it... if you're accessing DOS-prompt then keys types will input data into the standard console stream and compare your string values to commands / actions in the system... etc).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.