SlideShare une entreprise Scribd logo
KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS
ALGORITHMS
Unit -II
KCA205: DATA STRUCTURES & ANALYSIS OF
ALGORITHMS
ALGORITHMS
• Unit-I-
• Introduction to data structure: Data, Entity, Information,
Difference between Data and Information, Data type ,
Build in data type, Abstract data type, Definition of data
structures, Types of Data Structures: Linear and Non-
Linear Data Structure
• Introduction to Algorithms: Definition of Algorithms,
Difference between algorithm and programs, properties of
algorithm, Algorithm Design Techniques, Performance
Analysis of Algorithms, Complexity of various code
structures, Order of Growth, Asymptotic Notations.
• Arrays: Definition, Single and Multidimensional Arrays,
Representation of Arrays: Row Major Order, and Column
Major Order, Derivation of Index Formulae for 1-D, 2-D
Array Application of arrays, Sparse Matrices and their
representations.
• Linked lists: Array Implementation and Pointer
Implementation of Singly Linked Lists, Doubly Linked List,
KCA205: DATA STRUCTURES & ANALYSIS OF
ALGORITHMS
ALGORITHMS
• Unit-II-
• Stacks: Abstract Data Type, Primitive Stack
operations: Push & Pop, Array and Linked
Implementation of Stack in C, Application of stack:
Prefix and Postfix Expressions, Evaluation of
postfix expression.
• Iteration and Recursion- Principles of recursion,
Tail recursion, Removal of recursion Problem
solving using iteration and recursion with examples
such as binary search, Fibonacci numbers, and
Hanoi towers.
• Queues: Operations on Queue: Create, Add,
Delete, Full and Empty, Circular queues, Array and
linked implementation of queues in C, Dequeue
and Priority Queue.
KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS
ALGORITHMS
• Unit-III-
• Sorting: Insertion Sort, Selection Sort,
Bubble Sort, Heap Sort, Comparison of
Sorting Algorithms.
• Sorting in Linear Time: Counting Sort and
Bucket Sort.
• Graphs: Terminology used with Graph, Data
Structure for Graph Representations:
Adjacency Matrices, Adjacency List,
Adjacency.
• Graph Traversal: Depth First Search and
KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS
ALGORITHMS
• Unit-IV-
• Trees: Basic terminology used with Tree, Binary
Trees.
• Binary Tree Representation: Array
Representation and Pointer (Linked List)
Representation, Binary Search Tree, Complete
Binary Tree, A Extended Binary Trees.
• Tree Traversal algorithms: In-order, Preorder
and Post-order, Constructing Binary Tree from
given Tree Traversal, Operation of Insertion,
Deletion, Searching & Modification of data in
Binary Search Tree.
KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS
ALGORITHMS
• Unit-V-
• Divide and Conquer with Examples Such
as Merge Sort, Quick Sort.
• Matrix Multiplication: Strassen’s Algorithm
Dynamic Programming: Dijikstra
Algorithm, Bellman Ford Algorithm.
• Allpair Shortest Path: Warshal Algorithm,
Longest Common Sub-sequence
• Greedy Programming: Prims and Kruskal
algorithm.
KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS
ALGORITHMS
Unit -II
KCA205: DATA STRUCTURES & ANALYSIS OF
ALGORITHMS
ALGORITHMS
• Unit-II-
• Stacks: Abstract Data Type, Primitive Stack
operations: Push & Pop, Array and Linked
Implementation of Stack in C, Application of stack:
Prefix and Postfix Expressions, Evaluation of
postfix expression,
• Iteration and Recursion- Principles of recursion,
Tail recursion, Removal of recursion Problem
solving using iteration and recursion with examples
such as binary search, Fibonacci numbers, and
Hanoi towers.
• Queues: Operations on Queue: Create, Add,
Delete, Full and Empty, Circular queues, Array and
linked implementation of queues in C, Dequeue
and Priority Queue.
DATA STRUCTURES USING C
• Introduction to Stack
• There are certain situations in computer science that one wants
to restrict insertions and deletions so that they can take place
only at the beginning or the end of the list, not in the middle.
• Linear lists and arrays allow one to insert and delete elements
at any place in the list i.e., at the beginning, at the end or in the
middle.
• Two of such data structures that are useful are:
• • Stack. • Queue.
• When we add or remove data structures, they grow and shrink.
If we restrict the growth of a components or elements of linear
linear data structure so that new components or elements can
only be added or removed only at one end, we have a stack.
• Stacks are useful data structures and are used in a variety of
ways in computer science.
• What is Stack:-
• It is an ordered collection of homogeneous items of elements.
Elements are added to and removed from the top of the stack (the
most recently added items are at the top of the stack). The stack works
based on LIFO or FILO policy.
• The last element to be added is the first to be removed(LIFO: Last In,
First Out) one end, called TOP of the stack. The elements are removed
in reverse order of that in which they were inserted into the stack.
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages. It is named stack as it behaves like a real-
world stack, for example – a deck of cards or a pile of plates, etc.
• Applications of Stacks in Data Structures
• There a lot of places where we use stack without even realizing it.
• Let’s see some of the most common uses of stack data structure.
• Real life examples of Stack:-
• Stacking up dishes after washing them.
• Deck of cards.
• Stacking a pile of boxes in our store rooms.
• Libraries stack pile of books and articles.
• Some Technique/Programming /Applications of Stack
• Recursion
• Backtracking
• UNDO/REDO
• String reversal
• DFS(Depth First Search)
• Expression conversion
• Balancing of symbols
• Memory management
• Process Management
• The stack can be used -to convert some infix expression into
its postfix equivalent, or prefix equivalent.
• -Postfix or Prefix Evaluation.
• Stacks make excellent mechanisms for temporary storage of
information within procedures. A primary reason for this is
that they allow recursive invocations of procedures without
risk of destroying data from previous invocations of the
routine. They also support re-entrant code.
• Another great use of stack is during the function call and
return process. When we call a function from one other
function, that function call statement may not be the first
statement. After calling the function, we also have to come
back from the function area to the place, where we have left
our control. So we want to resume our task, not restart. For
that reason, we store the address of the program counter into
the stack, then go to the function body to execute it. After
completion of the execution, it pops out the address from
stack and assign it into the program counter to resume the
task again.
• Advantages of Using Stack over other data structures
• 1. Manages the data in a Last In First Out(LIFO) method which is not
possible with Linked list and array.
• 2. When a function is called the local variables are stored in a stack,
and it is automatically destroyed once returned.
• 3. A stack is used when a variable is not used outside that function.
• 4. It allows you to control how memory is allocated and deallocated.
• 5. Stack automatically cleans up the object.
• 6. Not easily corrupted
• 7. Variables cannot be resized.
• Disadvantages of Using Stack over other data structures
• 1. Stack memory is very limited.
• 2. Creating too many objects on the stack can increase the risk of
stack overflow.
• 3. Random access is not possible.
• 4. Variable storage will be overwritten, which sometimes leads to
undefined behaviour of the function or program.
• 5. The stack will fall outside of the memory area, which might lead to
an abnormal termination.
A stack is a list of elements in which an element may be inserted or
deleted only at one end, called the top of the stack. Stacks are
sometimes known as LIFO (last in, first out) Or FILO(First In last out)
lists.
OR
• STACK OPERATIONS:-
• There are basically three operations that can be
performed on stacks. They are-
• 1) inserting an item into a stack (push).
• 2) deleting an item from the stack (pop).
• 3) displaying the contents of the stack(pip).
• These are main two basic operations associated with
stack:
• 1. Push():- Push() is the term used to insert/add an
element into a stack. If the stack is full, then it is said to
be an Overflow condition.
• 2. Pop ():-Pop() is the term used to delete/remove an
element from a stack. If the stack is empty, then it is
said to be an Underflow condition.
• When data is PUSHed onto stack-
• To use a stack efficiently, we need to check the status of stack
as well. For the same purpose, the following functionality is
added to stacks −
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.
• peek() − Returns top element of stack. Or get the top data
element of the stack, without removing it.
• count(): It returns the total number of elements available in a
stack.
• change(): It changes the element at the given position.
• display(): It prints all the elements available in the stack.
• Other names for stacks are piles and push-down lists.
• At all times, we maintain a pointer to the last PUSHed data on
the stack. As this pointer always represents the top of the
stack, hence named top. The top pointer provides top value
of the stack without actually removing it.
• 1. PUSH operation():-
• The steps involved in the PUSH operation is given
below:
• Before inserting an element in a stack, we check
whether the stack is full.
• If we try to insert the element in a stack, and the stack
is full, then the overflow condition occurs.
• When we initialize a stack, we set the value of top as -1
to check that the stack is empty.
• When the new element is pushed in a stack, first, the
value of the top gets incremented, i.e., top=top+1, and
the element will be placed at the new position of
the top.
• The elements will be inserted until we reach
the max size of the stack.
• OR-Push Operation:-Push an item onto the top of the stack (insert an item)
• Algorithm for PUSH:
Or-Push(Stack, Maxsize, Top, Item)
1. [stack is filled ]
If Top=Maxsize-1,then
print: Stack is Overflow.
2. Set Top:=Top+1 . [Increase Top by 1].
3.Set Stack[Top]=Item.[Insert Item in new Top position]
4. return .
• Size of stack=4
• POP operation():-
• The steps involved in the POP operation is given below:
• Before deleting the element from the stack, we
check whether the stack is empty.
• If we try to delete the element from the empty
stack, then the underflow condition occurs.
• If the stack is not empty, we first access the element
which is pointed by the top.
• Once the pop operation is performed, the top is
decremented by 1, i.e., top=top-1
• Pop Operation():-Deletion from stack is also known as POP
operation in stack.
•
• Algorithm for Pop:-
• pop(stack, top, item) --This procedure deletes the top element eleme
of stack.
• 1.[stack has an item to be removed]
• If top=-1 then print underflow/ stack is empty, and return.
• 2. Set item =stack[top].
3. Set top=top-1. ----decrease by top by 1 4.
return.
• Size of stack=4
• Analysis of Stack Operations
• 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)
• The time complexities for push() and pop() functions
are O(1) because we always have to insert or remove the
data from the top of the stack, which is a one step process.
DATA STRUCTURES USING C
• Representation of Stack:- Let us consider a stack with 6
elements capacity. This is called as the size of the stack. The
number of elements to be added should not exceed the
maximum size of the stack. If we attempt to add new element
beyond the maximum size, we will encounter a stack overflow
condition.
• Similarly, you cannot remove elements beyond the base of the
stack. If such is the case, we will reach a stack underflow
condition.
• There are two ways to represent Stack in memory. One is using
array and other is using linked list.
• 1. Array representation of stack/Array implementation of
stack/implementation of stack using array
• 2. Linked list representation of stack/Linked list implementation
of stack/implementation of stack using linked list.
DATA STRUCTURES USING C
• 1. Array representation of stack/Array implementation of
stack/implementation of stack using array:-
• Usually the stacks are represented in the computer by a linear array.
In the following algorithms/procedures of pushing and popping an
item from the stacks, we have considered, a linear array STACK, a
variable TOP which contain the location of the top element of the
stack; and a variable STACKSIZE which gives the maximum number of
elements that can be hold by
• the stack.
• Here are the minimal operations we'd need for an abstract stack (and
their typical names):
• o Push: Places an element/value on top of the stack.
• o Pop: Removes value/element from top of the stack.
• o IsEmpty: Reports whether the stack is Empty or not.
• o IsFull: Reports whether the stack is Full or not.
• Example on Stack(Push Operation):-
void push()
{
if(top>=n-1)
{
printf("nt STACK is
over flow");
}
else
{
printf(" Enter a value
to be pushed:");
scanf("%d", &x);
top++;
stack[top]=x;
}}
void pop()
{
if(top<=-1)
{
printf("nt Stack is
under flow");
}
else
{
printf("nt The
popped elements is
%d", stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("n The elements in
STACK n");
for(i=top; i>=0; i--)
printf("n%d",stack[i]);
printf("n Press Next
Choice");
}
else
{
printf("n The STACK is
empty");
}
}
Implementation of Stack Using
Array in C
#include<stdio.h>
int
stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{ top=-1;
printf("n Enter the size of
STACK[MAX=100]:");
scanf("%d",&n);
printf("nt STACK
OPERATIONS USING ARRAY");
printf("nt------------------);
printf("nt 1.PUSHnt
2.POPnt 3.DISPLAYnt 4.EXIT");
do
{
printf("n Enter the Choice:");
scanf("%d",&choice);
switch(choice) {
case 1: { push(); break;
}
case 2: { pop(); break; }
case 3: { display(); break; }
case 4: { printf("nt EXIT POINT
"); break; }
default: {
printf ("nt Please Enter a
Valid Choice(1/2/3/4)");
} } }
while(choice!=4);
return 0;
}
void push()
{ if(top>=n-1) {
printf("ntSTACK is over flow");
}
else {
printf(" Enter a value to be
pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}}
void pop()
{
if(top<=-1)
{
printf("nt Stack is under flow");
} else
{
printf("nt The popped
elements is %d",stack[top]);
top--;
}}
void display()
{ if(top>=0)
{
printf("n The elements in STACK
n");
for(i=top; i>=0; i--)
printf("n%d",stack[i]);
printf("n Press Next Choice");
}
else {
printf("n The STACK is empty");
}
}
• Question:
• If the sequence of operations- push (1), push (2),
pop, push (1), push (2), pop, pop, pop, push (2),
pop, are performed on a stack, the sequence of
popped out values are-.
• Question:
• The following sequence of the operations is
performed on a stack PUSH(10), PUSH(20), POP,
PUSH(10), PUSH(20), POP, POP, POP, PUSH(20), POP
the sequence of values popped out is
stack.pptx

Contenu connexe

Similaire à stack.pptx

Unit 1.ppt
Unit 1.pptUnit 1.ppt
Unit 1.ppt
Minakshee Patil
 
Data strucer
Data strucerData strucer
Data strucer
shahab zebari
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
Sunipa Bera
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
sabithabanu83
 
Data Structure & Algorithm.pptx
Data Structure & Algorithm.pptxData Structure & Algorithm.pptx
Data Structure & Algorithm.pptx
Mumtaz
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
Rajitha Reddy Alugati
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
abinathsabi
 
DS.ppt Datatastructures notes presentation
DS.ppt Datatastructures notes presentationDS.ppt Datatastructures notes presentation
DS.ppt Datatastructures notes presentation
SakkaravarthiShanmug
 
Javascript stack
Javascript   stackJavascript   stack
Javascript stack
Samuel Santos
 
Think Like Spark: Some Spark Concepts and a Use Case
Think Like Spark: Some Spark Concepts and a Use CaseThink Like Spark: Some Spark Concepts and a Use Case
Think Like Spark: Some Spark Concepts and a Use Case
Rachel Warren
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
blessyboban92
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
NathanielAdika
 
Stacks and Queue,Concept of Stack,LIFO,Fifo,
Stacks and Queue,Concept of Stack,LIFO,Fifo,Stacks and Queue,Concept of Stack,LIFO,Fifo,
Stacks and Queue,Concept of Stack,LIFO,Fifo,
shaikhdaniyal8603
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
classall
 
linked list in c++
linked list in c++linked list in c++
linked list in c++
YaminiLakshmi Meduri
 
ds bridge.pptx
ds bridge.pptxds bridge.pptx
ds bridge.pptx
GOOGLEINTERNETCAFE1
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
SnehilKeshari
 
Data structures
Data structuresData structures
Data structures
Manaswi Sharma
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
classall
 

Similaire à stack.pptx (20)

Unit 1.ppt
Unit 1.pptUnit 1.ppt
Unit 1.ppt
 
Data strucer
Data strucerData strucer
Data strucer
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
 
Data Structure & Algorithm.pptx
Data Structure & Algorithm.pptxData Structure & Algorithm.pptx
Data Structure & Algorithm.pptx
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
DS.ppt Datatastructures notes presentation
DS.ppt Datatastructures notes presentationDS.ppt Datatastructures notes presentation
DS.ppt Datatastructures notes presentation
 
Javascript stack
Javascript   stackJavascript   stack
Javascript stack
 
Think Like Spark: Some Spark Concepts and a Use Case
Think Like Spark: Some Spark Concepts and a Use CaseThink Like Spark: Some Spark Concepts and a Use Case
Think Like Spark: Some Spark Concepts and a Use Case
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
 
Stacks and Queue,Concept of Stack,LIFO,Fifo,
Stacks and Queue,Concept of Stack,LIFO,Fifo,Stacks and Queue,Concept of Stack,LIFO,Fifo,
Stacks and Queue,Concept of Stack,LIFO,Fifo,
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
linked list in c++
linked list in c++linked list in c++
linked list in c++
 
ds bridge.pptx
ds bridge.pptxds bridge.pptx
ds bridge.pptx
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
 
Data structures
Data structuresData structures
Data structures
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
 

Dernier

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
Amin Marwan
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 

Dernier (20)

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 

stack.pptx

  • 1. KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS ALGORITHMS Unit -II
  • 2. KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS ALGORITHMS • Unit-I- • Introduction to data structure: Data, Entity, Information, Difference between Data and Information, Data type , Build in data type, Abstract data type, Definition of data structures, Types of Data Structures: Linear and Non- Linear Data Structure • Introduction to Algorithms: Definition of Algorithms, Difference between algorithm and programs, properties of algorithm, Algorithm Design Techniques, Performance Analysis of Algorithms, Complexity of various code structures, Order of Growth, Asymptotic Notations. • Arrays: Definition, Single and Multidimensional Arrays, Representation of Arrays: Row Major Order, and Column Major Order, Derivation of Index Formulae for 1-D, 2-D Array Application of arrays, Sparse Matrices and their representations. • Linked lists: Array Implementation and Pointer Implementation of Singly Linked Lists, Doubly Linked List,
  • 3. KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS ALGORITHMS • Unit-II- • Stacks: Abstract Data Type, Primitive Stack operations: Push & Pop, Array and Linked Implementation of Stack in C, Application of stack: Prefix and Postfix Expressions, Evaluation of postfix expression. • Iteration and Recursion- Principles of recursion, Tail recursion, Removal of recursion Problem solving using iteration and recursion with examples such as binary search, Fibonacci numbers, and Hanoi towers. • Queues: Operations on Queue: Create, Add, Delete, Full and Empty, Circular queues, Array and linked implementation of queues in C, Dequeue and Priority Queue.
  • 4. KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS ALGORITHMS • Unit-III- • Sorting: Insertion Sort, Selection Sort, Bubble Sort, Heap Sort, Comparison of Sorting Algorithms. • Sorting in Linear Time: Counting Sort and Bucket Sort. • Graphs: Terminology used with Graph, Data Structure for Graph Representations: Adjacency Matrices, Adjacency List, Adjacency. • Graph Traversal: Depth First Search and
  • 5. KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS ALGORITHMS • Unit-IV- • Trees: Basic terminology used with Tree, Binary Trees. • Binary Tree Representation: Array Representation and Pointer (Linked List) Representation, Binary Search Tree, Complete Binary Tree, A Extended Binary Trees. • Tree Traversal algorithms: In-order, Preorder and Post-order, Constructing Binary Tree from given Tree Traversal, Operation of Insertion, Deletion, Searching & Modification of data in Binary Search Tree.
  • 6. KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS ALGORITHMS • Unit-V- • Divide and Conquer with Examples Such as Merge Sort, Quick Sort. • Matrix Multiplication: Strassen’s Algorithm Dynamic Programming: Dijikstra Algorithm, Bellman Ford Algorithm. • Allpair Shortest Path: Warshal Algorithm, Longest Common Sub-sequence • Greedy Programming: Prims and Kruskal algorithm.
  • 7. KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS ALGORITHMS Unit -II
  • 8. KCA205: DATA STRUCTURES & ANALYSIS OF ALGORITHMS ALGORITHMS • Unit-II- • Stacks: Abstract Data Type, Primitive Stack operations: Push & Pop, Array and Linked Implementation of Stack in C, Application of stack: Prefix and Postfix Expressions, Evaluation of postfix expression, • Iteration and Recursion- Principles of recursion, Tail recursion, Removal of recursion Problem solving using iteration and recursion with examples such as binary search, Fibonacci numbers, and Hanoi towers. • Queues: Operations on Queue: Create, Add, Delete, Full and Empty, Circular queues, Array and linked implementation of queues in C, Dequeue and Priority Queue.
  • 9. DATA STRUCTURES USING C • Introduction to Stack • There are certain situations in computer science that one wants to restrict insertions and deletions so that they can take place only at the beginning or the end of the list, not in the middle. • Linear lists and arrays allow one to insert and delete elements at any place in the list i.e., at the beginning, at the end or in the middle. • Two of such data structures that are useful are: • • Stack. • Queue. • When we add or remove data structures, they grow and shrink. If we restrict the growth of a components or elements of linear linear data structure so that new components or elements can only be added or removed only at one end, we have a stack. • Stacks are useful data structures and are used in a variety of ways in computer science.
  • 10. • What is Stack:- • It is an ordered collection of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). The stack works based on LIFO or FILO policy. • The last element to be added is the first to be removed(LIFO: Last In, First Out) one end, called TOP of the stack. The elements are removed in reverse order of that in which they were inserted into the stack. • A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real- world stack, for example – a deck of cards or a pile of plates, etc.
  • 11. • Applications of Stacks in Data Structures • There a lot of places where we use stack without even realizing it. • Let’s see some of the most common uses of stack data structure. • Real life examples of Stack:- • Stacking up dishes after washing them. • Deck of cards. • Stacking a pile of boxes in our store rooms. • Libraries stack pile of books and articles. • Some Technique/Programming /Applications of Stack • Recursion • Backtracking • UNDO/REDO • String reversal • DFS(Depth First Search) • Expression conversion • Balancing of symbols • Memory management • Process Management
  • 12.
  • 13. • The stack can be used -to convert some infix expression into its postfix equivalent, or prefix equivalent. • -Postfix or Prefix Evaluation. • Stacks make excellent mechanisms for temporary storage of information within procedures. A primary reason for this is that they allow recursive invocations of procedures without risk of destroying data from previous invocations of the routine. They also support re-entrant code. • Another great use of stack is during the function call and return process. When we call a function from one other function, that function call statement may not be the first statement. After calling the function, we also have to come back from the function area to the place, where we have left our control. So we want to resume our task, not restart. For that reason, we store the address of the program counter into the stack, then go to the function body to execute it. After completion of the execution, it pops out the address from stack and assign it into the program counter to resume the task again.
  • 14. • Advantages of Using Stack over other data structures • 1. Manages the data in a Last In First Out(LIFO) method which is not possible with Linked list and array. • 2. When a function is called the local variables are stored in a stack, and it is automatically destroyed once returned. • 3. A stack is used when a variable is not used outside that function. • 4. It allows you to control how memory is allocated and deallocated. • 5. Stack automatically cleans up the object. • 6. Not easily corrupted • 7. Variables cannot be resized. • Disadvantages of Using Stack over other data structures • 1. Stack memory is very limited. • 2. Creating too many objects on the stack can increase the risk of stack overflow. • 3. Random access is not possible. • 4. Variable storage will be overwritten, which sometimes leads to undefined behaviour of the function or program. • 5. The stack will fall outside of the memory area, which might lead to an abnormal termination.
  • 15. A stack is a list of elements in which an element may be inserted or deleted only at one end, called the top of the stack. Stacks are sometimes known as LIFO (last in, first out) Or FILO(First In last out) lists. OR
  • 16. • STACK OPERATIONS:- • There are basically three operations that can be performed on stacks. They are- • 1) inserting an item into a stack (push). • 2) deleting an item from the stack (pop). • 3) displaying the contents of the stack(pip). • These are main two basic operations associated with stack: • 1. Push():- Push() is the term used to insert/add an element into a stack. If the stack is full, then it is said to be an Overflow condition. • 2. Pop ():-Pop() is the term used to delete/remove an element from a stack. If the stack is empty, then it is said to be an Underflow condition.
  • 17. • When data is PUSHed onto stack- • To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks − • isFull() − check if stack is full. • isEmpty() − check if stack is empty. • peek() − Returns top element of stack. Or get the top data element of the stack, without removing it. • count(): It returns the total number of elements available in a stack. • change(): It changes the element at the given position. • display(): It prints all the elements available in the stack. • Other names for stacks are piles and push-down lists. • At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always represents the top of the stack, hence named top. The top pointer provides top value of the stack without actually removing it.
  • 18. • 1. PUSH operation():- • The steps involved in the PUSH operation is given below: • Before inserting an element in a stack, we check whether the stack is full. • If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs. • When we initialize a stack, we set the value of top as -1 to check that the stack is empty. • When the new element is pushed in a stack, first, the value of the top gets incremented, i.e., top=top+1, and the element will be placed at the new position of the top. • The elements will be inserted until we reach the max size of the stack.
  • 19. • OR-Push Operation:-Push an item onto the top of the stack (insert an item) • Algorithm for PUSH: Or-Push(Stack, Maxsize, Top, Item) 1. [stack is filled ] If Top=Maxsize-1,then print: Stack is Overflow. 2. Set Top:=Top+1 . [Increase Top by 1]. 3.Set Stack[Top]=Item.[Insert Item in new Top position] 4. return .
  • 20.
  • 21. • Size of stack=4
  • 22. • POP operation():- • The steps involved in the POP operation is given below: • Before deleting the element from the stack, we check whether the stack is empty. • If we try to delete the element from the empty stack, then the underflow condition occurs. • If the stack is not empty, we first access the element which is pointed by the top. • Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1
  • 23. • Pop Operation():-Deletion from stack is also known as POP operation in stack. • • Algorithm for Pop:- • pop(stack, top, item) --This procedure deletes the top element eleme of stack. • 1.[stack has an item to be removed] • If top=-1 then print underflow/ stack is empty, and return. • 2. Set item =stack[top]. 3. Set top=top-1. ----decrease by top by 1 4. return.
  • 24. • Size of stack=4
  • 25. • Analysis of Stack Operations • 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) • The time complexities for push() and pop() functions are O(1) because we always have to insert or remove the data from the top of the stack, which is a one step process.
  • 26. DATA STRUCTURES USING C • Representation of Stack:- Let us consider a stack with 6 elements capacity. This is called as the size of the stack. The number of elements to be added should not exceed the maximum size of the stack. If we attempt to add new element beyond the maximum size, we will encounter a stack overflow condition. • Similarly, you cannot remove elements beyond the base of the stack. If such is the case, we will reach a stack underflow condition. • There are two ways to represent Stack in memory. One is using array and other is using linked list. • 1. Array representation of stack/Array implementation of stack/implementation of stack using array • 2. Linked list representation of stack/Linked list implementation of stack/implementation of stack using linked list.
  • 27. DATA STRUCTURES USING C • 1. Array representation of stack/Array implementation of stack/implementation of stack using array:- • Usually the stacks are represented in the computer by a linear array. In the following algorithms/procedures of pushing and popping an item from the stacks, we have considered, a linear array STACK, a variable TOP which contain the location of the top element of the stack; and a variable STACKSIZE which gives the maximum number of elements that can be hold by • the stack. • Here are the minimal operations we'd need for an abstract stack (and their typical names): • o Push: Places an element/value on top of the stack. • o Pop: Removes value/element from top of the stack. • o IsEmpty: Reports whether the stack is Empty or not. • o IsFull: Reports whether the stack is Full or not.
  • 28. • Example on Stack(Push Operation):- void push() { if(top>=n-1) { printf("nt STACK is over flow"); } else { printf(" Enter a value to be pushed:"); scanf("%d", &x); top++; stack[top]=x; }} void pop() { if(top<=-1) { printf("nt Stack is under flow"); } else { printf("nt The popped elements is %d", stack[top]); top--; } } void display() { if(top>=0) { printf("n The elements in STACK n"); for(i=top; i>=0; i--) printf("n%d",stack[i]); printf("n Press Next Choice"); } else { printf("n The STACK is empty"); } }
  • 29. Implementation of Stack Using Array in C #include<stdio.h> int stack[100],choice,n,top,x,i; void push(void); void pop(void); void display(void); int main() { top=-1; printf("n Enter the size of STACK[MAX=100]:"); scanf("%d",&n); printf("nt STACK OPERATIONS USING ARRAY"); printf("nt------------------); printf("nt 1.PUSHnt 2.POPnt 3.DISPLAYnt 4.EXIT"); do { printf("n Enter the Choice:"); scanf("%d",&choice); switch(choice) { case 1: { push(); break; } case 2: { pop(); break; } case 3: { display(); break; } case 4: { printf("nt EXIT POINT "); break; } default: { printf ("nt Please Enter a Valid Choice(1/2/3/4)"); } } } while(choice!=4); return 0; } void push() { if(top>=n-1) { printf("ntSTACK is over flow"); } else { printf(" Enter a value to be pushed:"); scanf("%d",&x); top++; stack[top]=x; }} void pop() { if(top<=-1) { printf("nt Stack is under flow"); } else { printf("nt The popped elements is %d",stack[top]); top--; }} void display() { if(top>=0) { printf("n The elements in STACK n"); for(i=top; i>=0; i--) printf("n%d",stack[i]); printf("n Press Next Choice"); } else { printf("n The STACK is empty"); } }
  • 30. • Question: • If the sequence of operations- push (1), push (2), pop, push (1), push (2), pop, pop, pop, push (2), pop, are performed on a stack, the sequence of popped out values are-. • Question: • The following sequence of the operations is performed on a stack PUSH(10), PUSH(20), POP, PUSH(10), PUSH(20), POP, POP, POP, PUSH(20), POP the sequence of values popped out is