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

More Related Content

What's hot (20)

PPTX
Binary Tree in Data Structure
Meghaj Mallick
 
PPT
Queue implementation
Rajendran
 
PPTX
Arrays in Data Structure and Algorithm
KristinaBorooah
 
PPT
stack presentation
Shivalik college of engineering
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PPSX
Stacks Implementation and Examples
greatqadirgee4u
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPTX
Binary Heap Tree, Data Structure
Anand Ingle
 
PPT
Red black tree
Rajendran
 
PPTX
Graph traversals in Data Structures
Anandhasilambarasan D
 
PPTX
stack & queue
manju rani
 
PPT
Heap sort
Mohd Arif
 
PPTX
Sorting algorithms
Trupti Agrawal
 
PPTX
Stack using Linked List
Sayantan Sur
 
PPTX
Queues
Ashim Lamichhane
 
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
PPT
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
PPT
Stack
srihariyenduri
 
PPT
Stack a Data Structure
ForwardBlog Enewzletter
 
PPTX
Queue
Raj Sarode
 
Binary Tree in Data Structure
Meghaj Mallick
 
Queue implementation
Rajendran
 
Arrays in Data Structure and Algorithm
KristinaBorooah
 
linear search and binary search
Zia Ush Shamszaman
 
Stacks Implementation and Examples
greatqadirgee4u
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Binary Heap Tree, Data Structure
Anand Ingle
 
Red black tree
Rajendran
 
Graph traversals in Data Structures
Anandhasilambarasan D
 
stack & queue
manju rani
 
Heap sort
Mohd Arif
 
Sorting algorithms
Trupti Agrawal
 
Stack using Linked List
Sayantan Sur
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Stack a Data Structure
ForwardBlog Enewzletter
 
Queue
Raj Sarode
 

Viewers also liked (20)

PPT
Arrays
archikabhatia
 
PPT
Splay tree
Rajendran
 
PPTX
B tree &amp;
memonayounas
 
PPT
B trees and_b__trees
Rakhi Srivastava
 
PPTX
B tree long
Nikhil Sharma
 
PPT
B tree
Rajendran
 
PPTX
Arrays C#
Raghuveer Guthikonda
 
PPSX
Data Structure (Dynamic Array and Linked List)
Adam Mukharil Bachtiar
 
PPTX
5 Array List, data structure course
Mahmoud Alfarra
 
PPT
03 stacks and_queues_using_arrays
tameemyousaf
 
PPTX
Data Structure -List Stack Queue
surya pandian
 
PDF
C and data structure
prabhatjon
 
PPT
1.8 splay tree
Krish_ver2
 
PPT
Stack Implementation
Zidny Nafan
 
PPTX
BTree, Data Structures
Jibrael Jos
 
PPT
chapter - 6.ppt
Tareq Hasan
 
PPT
Arrays Data Structure
student
 
PPTX
B tree
Tech_MX
 
PPTX
Stack Data structure
B Liyanage Asanka
 
Splay tree
Rajendran
 
B tree &amp;
memonayounas
 
B trees and_b__trees
Rakhi Srivastava
 
B tree long
Nikhil Sharma
 
B tree
Rajendran
 
Data Structure (Dynamic Array and Linked List)
Adam Mukharil Bachtiar
 
5 Array List, data structure course
Mahmoud Alfarra
 
03 stacks and_queues_using_arrays
tameemyousaf
 
Data Structure -List Stack Queue
surya pandian
 
C and data structure
prabhatjon
 
1.8 splay tree
Krish_ver2
 
Stack Implementation
Zidny Nafan
 
BTree, Data Structures
Jibrael Jos
 
chapter - 6.ppt
Tareq Hasan
 
Arrays Data Structure
student
 
B tree
Tech_MX
 
Stack Data structure
B Liyanage Asanka
 
Ad

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

PPTX
Ds stack & queue
Sunipa Bera
 
PPT
Stack linked list
bhargav0077
 
PPTX
stacks and queues for public
iqbalphy1
 
PPT
Lecture9_StackQueuepresentationaaaaa.ppt
PrernaPasoriya
 
PPT
Lecture9_StackQueue operation on stacks .ppt
ssuser7b9bda1
 
PPT
StackQueue.ppt
AfrozAlamKiVines
 
PPT
Lecture9_StackQueue.ppt
MuhammadSheraz836877
 
PPTX
Queue Data Structure
Poulami Das Akuli
 
PPTX
queue.pptx
Dr.Shweta
 
PPTX
Stack & Queue
Hasan Mahadi Riaz
 
PPT
23 stacks-queues-deques
Rishabh Jindal
 
PPTX
VCE Unit 03vv.pptx
skilljiolms
 
PPTX
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
PPT
Stacks, Queues, Deques
A-Tech and Software Development
 
PDF
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
vtunali
 
PPTX
Stack and Queue.pptx
Ddushb
 
PPT
05-stack_queue.ppt
Sarojkumari55
 
PPTX
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
PPTX
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
Ds stack & queue
Sunipa Bera
 
Stack linked list
bhargav0077
 
stacks and queues for public
iqbalphy1
 
Lecture9_StackQueuepresentationaaaaa.ppt
PrernaPasoriya
 
Lecture9_StackQueue operation on stacks .ppt
ssuser7b9bda1
 
StackQueue.ppt
AfrozAlamKiVines
 
Lecture9_StackQueue.ppt
MuhammadSheraz836877
 
Queue Data Structure
Poulami Das Akuli
 
queue.pptx
Dr.Shweta
 
Stack & Queue
Hasan Mahadi Riaz
 
23 stacks-queues-deques
Rishabh Jindal
 
VCE Unit 03vv.pptx
skilljiolms
 
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Stacks, Queues, Deques
A-Tech and Software Development
 
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
vtunali
 
Stack and Queue.pptx
Ddushb
 
05-stack_queue.ppt
Sarojkumari55
 
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
Stack_and_Queue_Presentation_Final (1).pptx
binduraniha86
 
Ad

Recently uploaded (20)

PPTX
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
PDF
Dealing with JSON in the relational world
Andres Almiray
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PDF
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
PDF
Rewards and Recognition (2).pdf
ethan Talor
 
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PDF
Transparency into Your Software’s True Reach
team-WIBU
 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
PPTX
Mistakes to Avoid When Selecting Policy Management Software
Insurance Tech Services
 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
PPTX
For my supp to finally picking supp that work
necas19388
 
PPTX
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
Dealing with JSON in the relational world
Andres Almiray
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
Rewards and Recognition (2).pdf
ethan Talor
 
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Transparency into Your Software’s True Reach
team-WIBU
 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
Mistakes to Avoid When Selecting Policy Management Software
Insurance Tech Services
 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
For my supp to finally picking supp that work
necas19388
 
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 

stack and queue array implementation in 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