SlideShare a Scribd company logo
2
Most read
Stacks
Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that
allows adding and removing elements in a particular order. Every time an element is added, it goes
on the top of the stack, the only element that can be removed is the element that was at the top of
the stack, just like a pile of objects.
Basic features of Stack
1. Stack is an ordered list of similar data type.
2. Stack is a LIFO structure. (Last in First out).
3. push() function is used to insert new elements into the Stack and pop() is used to delete an
element from the stack. Both insertion and deletion are allowed at only one end of Stack
called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.
Applications of Stack
The simplest application of a stack is to reverse a word. You push a given word to stack - letter by
letter - and then pop letters from the stack.
There are other uses also like : Parsing, Expression Conversion(Infixto Postfix, Postfix to Prefix
etc) and many more.
Implementation of Stack
Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are limited in
size and Linked List requires overhead to allocate, link, unlink, and deallocate, but is not limited in
size. Here we will implement Stack using array.
/* Below program is written in C++ language */
Class Stack
{
int top;
public:
int a[10]; //Maximum size of Stack
Stack()
{
top = -1;
}
};
void Stack::push(int x)
{
if( top >= 10)
{
cout << "Stack Overflow";
}
else
{
a[++top] = x;
cout << "Element Inserted";
}
}
int Stack::pop()
{
if(top < 0)
{
cout << "Stack Underflow";
return 0;
}
else
{
int d = a[top--];
return d;
}
}
void Stack::isEmpty()
{
if(top < 0)
{
cout << "Stack is empty";
}
else
{
cout << "Stack is not empty";
}
}
Position of Top Status of Stack
-1 Stack is Empty
0 Only one element in Stack
N-1 Stack is Full
N Overflow state of Stack
Analysis of Stacks
Below mentioned are the time complexities for various operations that can be performed on the
Stack data structure.
 Push Operation : O(1)
 Pop Operation : O(1)
 Top Operation : O(1)
 Search Operation : O(n)

More Related Content

PPTX
Constructor in java
PPTX
Method overloading
PPTX
Oop c++class(final).ppt
PDF
Run time storage
PPT
Data structure lecture 1
PPTX
Queue - Data Structure - Notes
PPTX
Computer Science-Data Structures :Abstract DataType (ADT)
PPTX
Unit II - LINEAR DATA STRUCTURES
Constructor in java
Method overloading
Oop c++class(final).ppt
Run time storage
Data structure lecture 1
Queue - Data Structure - Notes
Computer Science-Data Structures :Abstract DataType (ADT)
Unit II - LINEAR DATA STRUCTURES

What's hot (20)

PPTX
Application of Data structure
PPT
Unit 1 introduction to data structure
DOCX
Processes and threads
PPTX
AutoBoxing and Unboxing in java ppt.pptx
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
PPTX
Applications of data structures
PDF
Array data structure
PPTX
JAVA AWT
PPTX
Multithreading in java
PPT
Java packages
PPT
Searching in c language
PPTX
Queue
PPTX
Java string handling
PPTX
PPT
Java non access modifiers
PPT
Abstract data types
PDF
Java Applet and Graphics
PPTX
Stack using Linked List
PPTX
Data structures
PPTX
OOPS In JAVA.pptx
Application of Data structure
Unit 1 introduction to data structure
Processes and threads
AutoBoxing and Unboxing in java ppt.pptx
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Applications of data structures
Array data structure
JAVA AWT
Multithreading in java
Java packages
Searching in c language
Queue
Java string handling
Java non access modifiers
Abstract data types
Java Applet and Graphics
Stack using Linked List
Data structures
OOPS In JAVA.pptx
Ad

Similar to Stacks in data structure (20)

PPTX
Stack and Queue by M.Gomathi Lecturer
PPTX
Data structure Stack
PPTX
Stack in Sata Structure
PPT
Stack data structures with definition and code
PPTX
Stack and Queue
PPTX
STACK is a linear data structure it follows lifo approach
PPT
Lec 4 Stack of Data Structures & Algorithms
DOCX
In C++ a function used to put data into a stack is typically called a.docx
PDF
04 stacks
PPT
Algo>Stacks
PDF
Queues and Stacks
PPT
Lecture5
PPT
Difference between stack and queue
PPTX
Stacks Data structure.pptx
PPTX
Stacks in c++
PPTX
Stack project
PPTX
Stack and its operations
PDF
104332 sri vidhya eng notes
PPTX
Stack data structure
PDF
Stack and Queue by M.Gomathi Lecturer
Data structure Stack
Stack in Sata Structure
Stack data structures with definition and code
Stack and Queue
STACK is a linear data structure it follows lifo approach
Lec 4 Stack of Data Structures & Algorithms
In C++ a function used to put data into a stack is typically called a.docx
04 stacks
Algo>Stacks
Queues and Stacks
Lecture5
Difference between stack and queue
Stacks Data structure.pptx
Stacks in c++
Stack project
Stack and its operations
104332 sri vidhya eng notes
Stack data structure
Ad

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PDF
Basic Mud Logging Guide for educational purpose
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Lesson notes of climatology university.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Classroom Observation Tools for Teachers
Basic Mud Logging Guide for educational purpose
FourierSeries-QuestionsWithAnswers(Part-A).pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025
2.FourierTransform-ShortQuestionswithAnswers.pdf
Final Presentation General Medicine 03-08-2024.pptx
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Complications of Minimal Access Surgery at WLH
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
TR - Agricultural Crops Production NC III.pdf
Microbial disease of the cardiovascular and lymphatic systems
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Lesson notes of climatology university.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Stacks in data structure

  • 1. Stacks Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack, the only element that can be removed is the element that was at the top of the stack, just like a pile of objects. Basic features of Stack 1. Stack is an ordered list of similar data type. 2. Stack is a LIFO structure. (Last in First out). 3. push() function is used to insert new elements into the Stack and pop() is used to delete an element from the stack. Both insertion and deletion are allowed at only one end of Stack called Top. 4. Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is completely empty. Applications of Stack The simplest application of a stack is to reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack. There are other uses also like : Parsing, Expression Conversion(Infixto Postfix, Postfix to Prefix etc) and many more.
  • 2. Implementation of Stack Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but are limited in size and Linked List requires overhead to allocate, link, unlink, and deallocate, but is not limited in size. Here we will implement Stack using array. /* Below program is written in C++ language */ Class Stack { int top; public: int a[10]; //Maximum size of Stack Stack() { top = -1; } }; void Stack::push(int x) { if( top >= 10) {
  • 3. cout << "Stack Overflow"; } else { a[++top] = x; cout << "Element Inserted"; } } int Stack::pop() { if(top < 0) { cout << "Stack Underflow"; return 0; } else { int d = a[top--]; return d; } } void Stack::isEmpty() { if(top < 0) { cout << "Stack is empty"; } else { cout << "Stack is not empty"; } }
  • 4. Position of Top Status of Stack -1 Stack is Empty 0 Only one element in Stack N-1 Stack is Full N Overflow state of Stack Analysis of Stacks Below mentioned are the time complexities for various operations that can be performed on the Stack data structure.  Push Operation : O(1)  Pop Operation : O(1)  Top Operation : O(1)  Search Operation : O(n)