Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Stack Data Structure

Stacks are a data structure that follow the last-in, first-out principle. Elements are pushed onto the stack and popped off the stack in the reverse order. Stacks are used to implement functions like undo/redo and for recursion by storing data in the order it was entered.

Uploaded by

sudersen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Stack Data Structure

Stacks are a data structure that follow the last-in, first-out principle. Elements are pushed onto the stack and popped off the stack in the reverse order. Stacks are used to implement functions like undo/redo and for recursion by storing data in the order it was entered.

Uploaded by

sudersen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Stacks are the data structure that supports LIFO (Last in first out) functionality that is last

inserted element will get popped first. In some cases, when we need to process the data which is
entered last at the beginning, we sue this stack data structure. As the stack data structure uses
LIFO feature, they become the storage structure for recursion functions. They are used in UNDO
and REDO functions in Editor as we need to UNDO last operation we have done. For instance,
Microsoft word, Adobe photoshop applications has the UNDO and REDO feature which is based
on LIFO. They are also used in Operating system functions in trace backing of operations.

The main functions used in stack are:


 push - pushes the data into the stack
 pop - removes the last element / topmost element out of the stack
 isEmpty - check if the index of the stack empty or not
 peek - getting the last element inserted / topmost element without deleting
the element

Diagram 1.1 and 1.2 represents an example of Stack data structure which uses the LIFO
functionality.

Diagram 1.1 Diagram 1.2


Output :

Diagram 1.3

Based on the output from diagram 1.3, three numbers have been pushed into the stack
(50,10,99). Once pop method is used “s.pop( )” the last element inserted is being removed.
Therefore, the remaining element would be 10 and 50. Diagram 1.4 below shows a simple
representation of the code and output based on diagram 1.1 – 1.3.

LAST-IN-FIRST-
PUSH OUT POP

99

10
10

50
50

Diagram 1.4

You might also like