SlideShare a Scribd company logo
•
• KAMRAN KHAN.
• FA14-BSE-099.
• Algorithm & Data Structure.
• Stack & Queue Array implementation.
Stacks and Queues
• Stack
• A stack is a data Structure in which insertion and deletion take
place at the same end.
Stacks are known as LIFO (Last In, First Out) lists.
–The last element inserted will be the first to be retrieved.
–We can only add/remove/examine
the last element added (the "top").
STACK OPERATIONS:
● Push:
To insert an item from Top of stack is called push
operation.
● POP:
To put-off, get or remove some item from top of the
stack is the pop operation.
● IsEmpty:
Stack considered empty when there is no item on top.
IsEmpty operation return true when there is no item in stack else
false.
● IsFull:
Stack considered full if no other element can be inserted
on top of the stack.
Example of Push and Pop
• Push
– Add an element to the top of the stack.
• Pop
– Remove the element at the top of the stack.
top
IsEmpty stack
A
top
push an element
top
push another
A
B
top
pop
A
• Example of Push and Pop
• The Push Operation
• Suppose we have an empty integer stack that is capable of
holding a maximum of three values. With that stack we
execute the following push operations.
push(5);
push(10);
push(15);
• The state of the stack after each of the push operations:
• The Pop Operation
• Now, suppose we execute three consecutive pop
operations on the same stack:
• This is a sample program to demonstrate push and pop functionality in Stack in
Java:
• public class StackDemo {
• private int size = 3;
• int arr[] = new int[size];
• int front = -1; // variable declaration and initialize with -1
• public void push(int pushedElement) { // push method
• if (front < size - 1) { // when front of stack is less size-1
• front++; // increment the top
• arr[front] = pushedElement; // push value at array of front where front is
index
• System.out.println("Element " + pushedElement
• + " is pushed to Stack !");
• printElements(); // calling statement of print element method
• } else {
• System.out.println("Stack Overflow !"); // print when stack is full
• }
• }
• public void pop() { // pop method
• if (front >= 0) { // if condition true when front is greater than 0
• front--; // decrement the front
• System.out.println("Pop operation done !");
• } else {
• System.out.println("Stack Underflow !");
• }
• }
•
• public void printElements() { // display function heading
• if (front >= 0) {
• System.out.println("Elements in stack :");
• for (int i = 0; i <= front; i++) { // loop start from 0 index to the front of the
stack
• System.out.println(arr[i]); // printing statement
• }
• }
• }
• public static void main(String[] args) {
• StackDemo stackDemo = new StackDemo();
•
•
• stackDemo.push(23); // calling statement of push method with parameter
(passing value)
• stackDemo.push(2);
• stackDemo.push(73);
• stackDemo.push(21);
• stackDemo.pop(); // calling statement of pop method
• stackDemo.pop();
• stackDemo.pop();
• stackDemo.pop();
• }
•
• }
• Output
• If everything goes right you will see following output on console
demonstrating all possible cases in Stack Implementation.
• Queue
A queue is a Data structure, in which insertion is done at one
end, while deletion is performed at the other end.
---Accessing the elements of queues follows a First In, First Out
(FIFO) order.
--Like customers standing in a line in a store, the first
customer in is the first customer served.
--We can only add to the end of the
queue, and can only examine/remove
the front of the queue.
Enqueue and Dequeue
• Queue operations: Enqueue and Dequeue
• Like lines in a store, a queue has a front and a rear.
• Enqueue – insert an element at the rear of the queue
• Dequeue – remove an element from the front of the queue
Insert
(Enqueue)
Remove
(Dequeue) rearfront
• Example of Enqueue and Dequeue:
• Suppose we have an empty static integer queue that is
capable of holding a maximum of three values. With that
queue we execute the following enqueue operations.
Enqueue(3);
Enqueue(6);
Enqueue(9);
• Example of Enqueue and Dequeue:
• The state of the queue after each of the enqueue
operations.
• Example of Enqueue and Dequeue:
• Now the state of the queue after each of three consecutive
dequeue operations
Queue Implementation of Array
• There algorithms to implement Enqueue and Dequeue
– When enqueuing, the front index is always fixed and
the rear index moves forward in the array.
front
rear
Enqueue(3)
3
front
rear
Enqueue(6)
3 6
front
rear
Enqueue(9)
3 6 9
Queue Implementation of Array
– When dequeuing, the front index is fixed, and the
element at the front the queue is removed. Move all
the elements after it by one position.
Dequeue()
front
rear
6 9
Dequeue() Dequeue()
front
rear
9
rear = -1
front
• This is a sample program to demonstrate push and pop functionality in
Queue in Java.
• public class QueueDemo {
• private int size = 3; // instance variable and size of of array
• int arr[] = new int[size]; // declaration of array and assigning size to array
•
• int front = -1; // front of queue
• int rear = 0; // rear of queue
•
• public void push(int pushedElement) { // insertion method declaration
• if (front < size - 1) { // front is less then size-1
• front++; //increment the front
• arr[front] = pushedElement; // set element at array at front
• System.out.println("Element " + pushedElement
• + " is pushed to Queue !");
• display(); // display method calling statement
• } else {
• System.out.println("Overflow !");
• }
• }
• public void pop() { // deletion method declaration
• if (front >= rear) { // true till front is greater or equal to rear
• rear++;
• System.out.println("Pop operation done !");
• display(); // display method calling statement
• } else {
• System.out.println("Underflow !");
• }
• }
•
• public void display() { // display method declaration
• if (front >= rear) {
• System.out.println("Elements in Queue : ");
• for (int i = rear; i <= front; i++) { // start from rear to front
• System.out.println(arr[i]);
• }
• }
• }
•
• public static void main(String[] args) {
• QueueDemo queueDemo = new QueueDemo();
• queueDemo.pop();
• queueDemo.push(23); // calling statement of insertion
method
• queueDemo.push(2);
• queueDemo.push(73);
• queueDemo.push(21);
• queueDemo.pop(); // calling statement of deletion method
• queueDemo.pop();
• queueDemo.pop();
• queueDemo.pop();
• }
•
• }
• Output
• If everything goes right you will see following output on
console demonstrating all possible cases in Queue
Implementation
THE END
Ad

More Related Content

What's hot (20)

Queue
QueueQueue
Queue
Allana Institute of management sciences
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
Mekk Mhmd
 
Queues
QueuesQueues
Queues
Hareem Aslam
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
Senthil Kumar
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
Queue-Data Structure
Queue-Data StructureQueue-Data Structure
Queue-Data Structure
Paurav Shah
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
Farhanum Aziera
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Queue
QueueQueue
Queue
Abdur Rehman
 
Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)
Adam Mukharil Bachtiar
 
Stack
StackStack
Stack
Zaid Shabbir
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
A-Tech and Software Development
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
Tech_MX
 
Stack
StackStack
Stack
Seema Sharma
 

Similar to stack and queue array implementation, java. (20)

Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
Ddushb
 
Chapter 4 stack
Chapter 4 stackChapter 4 stack
Chapter 4 stack
jadhav_priti
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
 
Stack,queue and linked list data structure.pptx
Stack,queue and linked list data structure.pptxStack,queue and linked list data structure.pptx
Stack,queue and linked list data structure.pptx
yukti266975
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
GirT2
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
Usha P
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
SherinRappai
 
week 7,8,10,11 alll files included from .ppt
week 7,8,10,11 alll files included from .pptweek 7,8,10,11 alll files included from .ppt
week 7,8,10,11 alll files included from .ppt
LidetAdmassu
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 
STACK_IN_DATA STRUCTURE AND ALGORITHMS.pptx
STACK_IN_DATA STRUCTURE AND ALGORITHMS.pptxSTACK_IN_DATA STRUCTURE AND ALGORITHMS.pptx
STACK_IN_DATA STRUCTURE AND ALGORITHMS.pptx
bwubca22582
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
SonaPathak4
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
iloveyoucarlo0923
 
Queues
Queues Queues
Queues
nidhisatija1
 
6 - STACKS in Data Structure and Algorithm.pptx
6 - STACKS in Data Structure and Algorithm.pptx6 - STACKS in Data Structure and Algorithm.pptx
6 - STACKS in Data Structure and Algorithm.pptx
RahulRaj493025
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
Pulkitmodi1998
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
Soumen Santra
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
Halid Assen
 
Unit I-Data structures stack & Queue
Unit I-Data structures stack & QueueUnit I-Data structures stack & Queue
Unit I-Data structures stack & Queue
DrkhanchanaR
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
Ddushb
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
 
Stack,queue and linked list data structure.pptx
Stack,queue and linked list data structure.pptxStack,queue and linked list data structure.pptx
Stack,queue and linked list data structure.pptx
yukti266975
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
GirT2
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
Usha P
 
week 7,8,10,11 alll files included from .ppt
week 7,8,10,11 alll files included from .pptweek 7,8,10,11 alll files included from .ppt
week 7,8,10,11 alll files included from .ppt
LidetAdmassu
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 
STACK_IN_DATA STRUCTURE AND ALGORITHMS.pptx
STACK_IN_DATA STRUCTURE AND ALGORITHMS.pptxSTACK_IN_DATA STRUCTURE AND ALGORITHMS.pptx
STACK_IN_DATA STRUCTURE AND ALGORITHMS.pptx
bwubca22582
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
SonaPathak4
 
6 - STACKS in Data Structure and Algorithm.pptx
6 - STACKS in Data Structure and Algorithm.pptx6 - STACKS in Data Structure and Algorithm.pptx
6 - STACKS in Data Structure and Algorithm.pptx
RahulRaj493025
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
Pulkitmodi1998
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
Soumen Santra
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
Halid Assen
 
Unit I-Data structures stack & Queue
Unit I-Data structures stack & QueueUnit I-Data structures stack & Queue
Unit I-Data structures stack & Queue
DrkhanchanaR
 
Ad

Recently uploaded (20)

What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Programs as Values - Write code and don't get lost
Programs as Values - Write code and don't get lostPrograms as Values - Write code and don't get lost
Programs as Values - Write code and don't get lost
Pierangelo Cecchetto
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509
Fermin Galan
 
Download MathType Crack Version 2025???
Download MathType Crack  Version 2025???Download MathType Crack  Version 2025???
Download MathType Crack Version 2025???
Google
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
How to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryErrorHow to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025
Phil Eaton
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Artificial hand using embedded system.pptx
Artificial hand using embedded system.pptxArtificial hand using embedded system.pptx
Artificial hand using embedded system.pptx
bhoomigowda12345
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Programs as Values - Write code and don't get lost
Programs as Values - Write code and don't get lostPrograms as Values - Write code and don't get lost
Programs as Values - Write code and don't get lost
Pierangelo Cecchetto
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509
Fermin Galan
 
Download MathType Crack Version 2025???
Download MathType Crack  Version 2025???Download MathType Crack  Version 2025???
Download MathType Crack Version 2025???
Google
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
How to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryErrorHow to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025
Phil Eaton
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Artificial hand using embedded system.pptx
Artificial hand using embedded system.pptxArtificial hand using embedded system.pptx
Artificial hand using embedded system.pptx
bhoomigowda12345
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
Ad

stack and queue array implementation, java.

  • 1. • • KAMRAN KHAN. • FA14-BSE-099. • Algorithm & Data Structure. • Stack & Queue Array implementation.
  • 3. • Stack • A stack is a data Structure in which insertion and deletion take place at the same end. Stacks are known as LIFO (Last In, First Out) lists. –The last element inserted will be the first to be retrieved. –We can only add/remove/examine the last element added (the "top").
  • 4. STACK OPERATIONS: ● Push: To insert an item from Top of stack is called push operation. ● POP: To put-off, get or remove some item from top of the stack is the pop operation. ● IsEmpty: Stack considered empty when there is no item on top. IsEmpty operation return true when there is no item in stack else false. ● IsFull: Stack considered full if no other element can be inserted on top of the stack.
  • 5. Example of Push and Pop • Push – Add an element to the top of the stack. • Pop – Remove the element at the top of the stack. top IsEmpty stack A top push an element top push another A B top pop A
  • 6. • Example of Push and Pop • The Push Operation • Suppose we have an empty integer stack that is capable of holding a maximum of three values. With that stack we execute the following push operations. push(5); push(10); push(15);
  • 7. • The state of the stack after each of the push operations:
  • 8. • The Pop Operation • Now, suppose we execute three consecutive pop operations on the same stack:
  • 9. • This is a sample program to demonstrate push and pop functionality in Stack in Java: • public class StackDemo { • private int size = 3; • int arr[] = new int[size]; • int front = -1; // variable declaration and initialize with -1 • public void push(int pushedElement) { // push method • if (front < size - 1) { // when front of stack is less size-1 • front++; // increment the top • arr[front] = pushedElement; // push value at array of front where front is index • System.out.println("Element " + pushedElement • + " is pushed to Stack !"); • printElements(); // calling statement of print element method • } else { • System.out.println("Stack Overflow !"); // print when stack is full • } • }
  • 10. • public void pop() { // pop method • if (front >= 0) { // if condition true when front is greater than 0 • front--; // decrement the front • System.out.println("Pop operation done !"); • } else { • System.out.println("Stack Underflow !"); • } • } • • public void printElements() { // display function heading • if (front >= 0) { • System.out.println("Elements in stack :"); • for (int i = 0; i <= front; i++) { // loop start from 0 index to the front of the stack • System.out.println(arr[i]); // printing statement • } • } • }
  • 11. • public static void main(String[] args) { • StackDemo stackDemo = new StackDemo(); • • • stackDemo.push(23); // calling statement of push method with parameter (passing value) • stackDemo.push(2); • stackDemo.push(73); • stackDemo.push(21); • stackDemo.pop(); // calling statement of pop method • stackDemo.pop(); • stackDemo.pop(); • stackDemo.pop(); • } • • }
  • 12. • Output • If everything goes right you will see following output on console demonstrating all possible cases in Stack Implementation.
  • 13. • Queue A queue is a Data structure, in which insertion is done at one end, while deletion is performed at the other end. ---Accessing the elements of queues follows a First In, First Out (FIFO) order. --Like customers standing in a line in a store, the first customer in is the first customer served. --We can only add to the end of the queue, and can only examine/remove the front of the queue.
  • 14. Enqueue and Dequeue • Queue operations: Enqueue and Dequeue • Like lines in a store, a queue has a front and a rear. • Enqueue – insert an element at the rear of the queue • Dequeue – remove an element from the front of the queue Insert (Enqueue) Remove (Dequeue) rearfront
  • 15. • Example of Enqueue and Dequeue: • Suppose we have an empty static integer queue that is capable of holding a maximum of three values. With that queue we execute the following enqueue operations. Enqueue(3); Enqueue(6); Enqueue(9);
  • 16. • Example of Enqueue and Dequeue: • The state of the queue after each of the enqueue operations.
  • 17. • Example of Enqueue and Dequeue: • Now the state of the queue after each of three consecutive dequeue operations
  • 18. Queue Implementation of Array • There algorithms to implement Enqueue and Dequeue – When enqueuing, the front index is always fixed and the rear index moves forward in the array. front rear Enqueue(3) 3 front rear Enqueue(6) 3 6 front rear Enqueue(9) 3 6 9
  • 19. Queue Implementation of Array – When dequeuing, the front index is fixed, and the element at the front the queue is removed. Move all the elements after it by one position. Dequeue() front rear 6 9 Dequeue() Dequeue() front rear 9 rear = -1 front
  • 20. • This is a sample program to demonstrate push and pop functionality in Queue in Java. • public class QueueDemo { • private int size = 3; // instance variable and size of of array • int arr[] = new int[size]; // declaration of array and assigning size to array • • int front = -1; // front of queue • int rear = 0; // rear of queue • • public void push(int pushedElement) { // insertion method declaration • if (front < size - 1) { // front is less then size-1 • front++; //increment the front • arr[front] = pushedElement; // set element at array at front • System.out.println("Element " + pushedElement • + " is pushed to Queue !"); • display(); // display method calling statement • } else { • System.out.println("Overflow !"); • } • }
  • 21. • public void pop() { // deletion method declaration • if (front >= rear) { // true till front is greater or equal to rear • rear++; • System.out.println("Pop operation done !"); • display(); // display method calling statement • } else { • System.out.println("Underflow !"); • } • } • • public void display() { // display method declaration • if (front >= rear) { • System.out.println("Elements in Queue : "); • for (int i = rear; i <= front; i++) { // start from rear to front • System.out.println(arr[i]); • } • } • } •
  • 22. • public static void main(String[] args) { • QueueDemo queueDemo = new QueueDemo(); • queueDemo.pop(); • queueDemo.push(23); // calling statement of insertion method • queueDemo.push(2); • queueDemo.push(73); • queueDemo.push(21); • queueDemo.pop(); // calling statement of deletion method • queueDemo.pop(); • queueDemo.pop(); • queueDemo.pop(); • } • • }
  • 23. • Output • If everything goes right you will see following output on console demonstrating all possible cases in Queue Implementation
  翻译: