Stack in C++

Advertisements

Prev Tutorial Next Tutorial

Real Life Example of Stack in C++

Stack is linear data structure. In stack addition of new data item and deletion of already existing data item is done from only one end, known as top. Working of stack on the basis of Last-in-First-out (LIFO) principal, it means last entered item remove first.

A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the push-down stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack.

A stack is a limited access data structure - elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top. A stack is a recursive data structure.

stack in c++

Structural definition of a Stack

  • Stack is either empty or
  • It consists of a top and the rest which is a stack

Application of Stack

  • To reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack.
  • An "undo" mechanism in text editors; this operation is accomplished by keeping all text changes in a stack.
  • Undo/Redo stacks in Excel or Word.
  • space for parameters and local variables is created internally using a stack.
  • compiler's syntax check for matching braces is implemented by using stack.
  • A stack of plates/books in a cupboard.
  • A garage that is only one car wide. To remove the first car in we have to take out all the other cars in after it.
  • Wearing/Removing Bangles.
  • Back/Forward stacks on browsers.
  • Support for recursion
  • Activation records of method calls.

Real Life Example of Stack in C++

A most popular example of stack is plates in marriage party. Fresh plates are pushed onto to the top and popped from the top.

stack real life example in c++

A queue of people at ticket-window: The person who comes first gets the ticket first. The person who is coming last is getting the tickets in last. Therefore, it follows first-in-first-out (FIFO) strategy of queue.

Vehicles on toll-tax bridge: The vehicle that comes first to the toll tax booth leaves the booth first. The vehicle that comes last leaves last. Therefore, it follows first-in-first-out (FIFO).

One of the best example I've gone through is Browser back button

stack real life example in c++

Every web browser has a Back button. As you navigate from web page to web page, those pages are placed on a stack (actually it is the URLs that are going on the stack). The current page that you are viewing is on the top and the first page you looked at is at the base. If you click on the Back button, you begin to move in reverse order through the pages.

  • During Function Calls because it Follows A LIFO Structure
  • In Evaluating an Expression Stack is Used
  • Converting an Infix to Postfix
  • During Depth First Search (DFS) Stack is used
  • Stack is used in some Scheduling Algorithms

Stack Operation in C++

In stack data structure mainly perform two operation; push and pop

  • pop: In case of stack deletion of any item from stack is called pop.
  • push: In case of stack Insertion of any item in stack is called push.

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements