Stack and Queue Project For Class 12 Project
Stack and Queue Project For Class 12 Project
ALGORITHM:
❭STEP 1: Start
❭STEP 2:-Checks if stack is empty
❭STEP 3:- If the stack is full produce an error and exit.
❭STEP 4: If the stack is not full increment top to next empty space.
❭STEP 5:-Add data element to stack location.
❭STEP 6: Returns success.
❭ STEP 7:- Stop
67
56
89
TOP
78
23
67
56
ALGORITHM:
STEP 1:-Start
STEP 2:-Checks if the stack is empty.
STEP 3:- If the stack is empty produce an error and exit.
STEP 4:- If the stack is not empty, access the element at which top is
pointing.
STEP 5:- Decrease the top value by 1.
STEP 6:- Returns success
34
Deleting 78 from the stack
56
23
34 TOP
Stack after deleting 78
56
23
ALGORITHM:
❭ STEP 1:-Start
❭ STEP 2:-Create a main method which performs pop, push, and display task
using switch case method.
❭ STEP 3:- Create a function push() which pushes the value inside the array
and check whether the queue is overflowed or not.
❭ STEP 4:- Create a function pop()which deletes the front value and also
checks whether the queue is underflowed or not.
❭ STEP 5:- Display the output accordingly
❭ STEP 6:-Stop
{
n = 10;
System.out.println("STACKS STRING");
System.out.println("1.push \n2.pop \n3.display \n4.exit"); ch = sc.nextInt(); // For user’s
choice
switch(ch)
{
case 1:
System.out.println("ENTER A WORD"); x = sc.next(); // Input from user
push(x); // Push the element onto stack
break;
case 2:
pop(); // Pop the top element from stack
break;
case 3:
System.out.println("THE STACK IS AS FOLLOWS");
for ( i = top ; i >= 0 ;i --)
{
// Printing the stack elements
System.out.println(ar[i]);
}
break; case 4:
// To end stack operations
System.out.println("THE END");
System.exit(0); default:
// Display an appropriate message
System.out.println("WRONG CHOICE");
}
}
}
static void pop()
{
if (top == -1) // Condition for underflow
{
System.out.println("THE STACK IS UNDERFLOWED"); System.exit(0);
}
else
{
// Printing the deleted element
System.out.println("THE DELETED ELEMENT IS:" + ar[top]); top--;// Decrement to
remove the element from stack
}
}
}
ALGORITHM:
❭ STEP 1:-Start
❭ STEP 2:-Create a main method which performs pop, push, and display task
using switch case method.
❭ STEP 3:- Create a function push() which pushes the value inside the array
and check whether the queue is overflowed or not.
❭ STEP 4:- Create a function pop()which deletes the front value and also
checks whether the queue is underflowed or not.
❭ STEP 5:- Display the output accordingly
❭ STEP 6:-Stop
import java.util.Scanner;
class stack_Num
{
static int[] ar = new int[10]; // Array to store stack elements
static int top = -1; // Initializing pointer variable
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
int ch, c; do
{
System.out.println("STACK");
System.out.println("1. Push\n2. Pop\n3. Display\n4. Exit"); System.out.print("Enter
your choice: ");
ch = sc.nextInt(); // For user's choice
switch (ch)
{
case 1:
System.out.print("Enter a value: "); int x = sc.nextInt(); // Input from user
push(x); // Push the element onto stack
break;
case 2:
pop(); // Pop the top element from stack
break;
case 3:
display(); // Display the stack elements
break;
case 4:
System.out.println("The End"); return; // Exit the program
default:
System.out.println("Wrong choice"); break;
}
System.out.print("Want to continue? (1 for Yes, 0 for No): "); c = sc.nextInt(); // Ask
user to continue
} while (c == 1); // Repetition of loop
}
static void push(int x)
{
if (top == ar.length - 1)// Condition for overflow
{
}
static void pop()
{
if (top == -1)// Condition for underflow
{
System.out.println("Stack is underflowed");
}
else
{
System.out.println("The deleted item is: " + ar[top--]); // Print the deleted element
}
}
static void display()
{
if (top == -1)
{
System.out.println("The stack is empty.");
}
else
{
for (int i = top; i >= 0; i--)
{
System.out.println(ar[i]); // Print stack elements
}
}
}
STACKS STRING
1.push
2.pop
3.display
4.exit
1
ENTER A WORD
India
THE STACK IS AS FOLLOWS :
India
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
1
ENTER A WORD
Germany
THE STACK IS AS FOLLOWS :
Germany
India
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
1
ENTER A WORD
Argentina
THE STACK IS AS FOLLOWS :
Argentina
Germany
India
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
1
ENTER A WORD
Brazil
THE STACK IS AS FOLLOWS :
Brazil
Argentina
56 61 45 60
Front Rear
56 61 45 60 7
Front Rear
56 61 45 60
Front Rear
ALGORITHM:
❭ STEP 1:-Start
❭ STEP 2:-Create a main method which performs enqueue,
dequeue, and display task using switch case method.
❭ STEP 3:- Create a function enqueue() which pushes the
value inside the array and check whether the queue is
overflowed or not.
❭ STEP 4:- Create a function dequeue()which deletes the front
value and also checks whether the queue is underflowed or not.
❭ STEP 5:- Display the output accordingly
❭ STEP 6:-Stop
import java.util.*;
class QueueNum// class declaration
{
static int arr[] = new int[5];
static int front = -1, rear = -1, x, ch, l; static int c;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)// main method declaration
{
do
{
System.out.println("QUEUE");
System.out.println("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit"); ch
= sc.nextInt();
switch (ch)
{
case 1:
System.out.println("Enter a value"); x = sc.nextInt();
enqueue(x); break;
case 2:
dequeue(); break;
case 3:
display(); break;
case 4:
System.out.println("The end"); System.exit(0);
default:
System.out.println("Wrong choice");
}
System.out.println("Want to continue? Press 1 for Yes, any other key for
No"); // for repeating the process
c = sc.nextInt();
} while (c == 1);
}
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a value
4
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a value
9
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
ARKADIP PANJA Page | 27
Enter a value
16
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a value
25
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a value
36
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
ALGORITHM:
❭ STEP 1:-Start
❭ STEP 2:-Create a main method which performs enqueue,
dequeue, and display task using switch case method.
❭ STEP 3:- Create a function enqueue() which pushes the
value inside the array and check whether the queue is
overflowed or not.
❭ STEP 4:- Create a function dequeue()which deletes the front
value and also checks whether the queue is underflowed or not.
❭ STEP 5:- Display the output accordingly
❭ STEP 6:-Stop
import java.util.*;
class queue_string// class declaration
{
static String arr[] = new String[5]; static int front = -1, rear = -1,ch;
static String x;
static int c;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)// main method declaration
{
do
{
System.out.println("QUEUE");
System.out.println("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit"); ch
= sc.nextInt();
switch (ch)
{
case 1:
System.out.println("Enter a word"); x = sc.next();
enqueue(x); break;
case 2:
dequeue(); break;
case 3:
display(); break;
case 4:
System.out.println("The end"); System.exit(0);
default:
System.out.println("Wrong choice");
}
System.out.println("Want to continue? Press 1 for Yes, any other key for
No"); // for repeating the process
c = sc.nextInt();
} while (c == 1);
}
static void enqueue(String x)// method to enter values in queue
{
if (rear == 5 - 1)
{
System.out.println("Queue is overflowed");
}
ARKADIP PANJA Page | 33
else
{
if (front == -1)
{
front = 0;
}
rear++;
arr[rear] = x;
}
}
static void dequeue()// method to remove values from queue
{
if (front == -1)
{
System.out.println("The queue is underflowed");
}
else
{
System.out.println("The deleted item is: " + arr[front]);
front++;
if (front > rear)
{
front = -1;
rear = -1;
}
}}
static void display()// method to display queue elements
{
if (front == -1)
{
System.out.println("The queue is empty");
}
else
{
System.out.println("The queue is as follows:"); for (int i = front; i <=
rear; i++)
{
System.out.println(arr[i]);
}
}}
} // end of class
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a word
Russia
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a word
Netherland
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a word
Spain
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a word
Uruguay
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
1
Enter a word
Australia
ALGORITHM:
Types of DeQueue
Input Restricted DeQueue
Output Restricted DeQueue
In INPUT RESTRICTED DEQUEUE , insertion can be done from REAR only, but deletion can be
done from both FRONT and REAR.
In OUTPUT RESTRICTED DEQUEUE, deletion can be done from FRONT only, but insertion can
be done from both FRONT and REAR.
import java.util.Scanner;
class DeQueue{
int qrr[];
int lim;
int front;
int rear;
public DeQueue(int l){
lim = l;
qrr = new int[lim];
front = 0;
rear = 0;
}
public void addFront(int v){
if(front == 0)
System.out.println("OVERFLOW FROM FRONT");
else
qrr[--front] = v;
}
public void addRear(int v){
if(rear == lim)
System.out.println("OVERFLOW FROM REAR");
else
qrr[rear++] = v;
}
public int popFront(){
if(front < rear){
int d = qrr[front++];
if(front == rear){
front = 0;
rear = 0;
}
return d;
}
return -999;
}
public int popRear(){
if(front < rear){
int d = qrr[--rear];
if(front == rear){
front = 0;
rear = 0;
ARKADIP PANJA Page | 42
}
return d;
}
return -999;
}
public void show(){
if(front > rear)
System.out.println("DE-QUEUE EMPTY");
else{
for(int i = front; i < rear; i++)
System.out.print(qrr[i] + " ");
System.out.println();
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Dequeue limit: ");
int size = Integer.parseInt(in.nextLine());
DeQueue dq = new DeQueue(size);
while(true){
System.out.println("1. Add from front");
System.out.println("2. Add from rear");
System.out.println("3. Pop from front");
System.out.println("4. Pop from rear");
System.out.println("5. Display elements");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(in.nextLine());
switch(choice){
case 1:
System.out.print("Element to be added: ");
int v = Integer.parseInt(in.nextLine());
dq.addFront(v);
break;
case 2:
System.out.print("Element to be added: ");
v = Integer.parseInt(in.nextLine());
dq.addRear(v);
break;
case 3:
v = dq.popFront();
if(v == -999)
System.out.println("Underflow from front");
else
ARKADIP PANJA Page | 43
System.out.println(v + " popped");
break;
case 4:
v = dq.popRear();
if(v == -999)
System.out.println("Underflow from rear");
else
System.out.println(v + " popped");
break;
case 5:
dq.show();
break;
default:
System.out.println("Bye!");
return;
}
}
}
}
Dequeue limit: 3
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 1
Element to be added: 7
OVERFLOW FROM FRONT
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 2
Element to be added: 18
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 2
Element to be added: 45
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 2
Element to be added: 99
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 2
Element to be added: 63
OVERFLOW FROM REAR
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 5
18 45 99
1. Add from front
2. Add from rear
3. Pop from front
STEP 1: START
STEP 2: Input the no of rows and columns
STEP 3: Declare the matrix a[m][n]
STEP 4:Input matrix elements
STEP 5:Display the original matrix
STEP 6:Apply the necessary conditions to calculate the decimal
equivalent of the rows and display it.
STEP 7: STOP
import java.util.Scanner;
public class OctalMatrix
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows (M): ");
int m = in.nextInt();
System.out.print("Enter the number of columns (N): ");
int n = in.nextInt();
ALGORITHM:
STEP 1: Start
STEP 2: Enter the matrix size(m<3 || m>9)
STEP 3:Enter the matrix elements
STEP 4:Display the original matrix
STEP 5:Apply the conditions necessary to rotate the matrix 90° clockwise{
B[i][j]=A[x][i]}
STEP 6:Display the rotated matrix
STEP 7:Stop
import java.util.*;
class mat_rotate
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
if(m<3 || m>9)
System.out.println("Size Out Of Range");
else
{
int A[][]=new int[m][m];
Allow the user to input integers into this matrix. Display appropriate error
message for an invalid input. Perform the following tasks:
Test your program for the following data and some random data:
Example 1
INPUT: M = 3
4 16 12
8 2 14
6 1 3
OUTPUT:
ORIGINAL MATRIX
4 16 12
8 2 14
6 1 3
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 6
STEP 1: Start
STEP 2: Enter the matrix size(m<3 || m>9)
STEP 3:Enter the matrix elements
STEP 4:Display the original matrix
STEP 5:Apply the conditions necessary to create the mirror image of
the matrix
STEP 6:Display the mirror image of the matrix
STEP 7:Stop
import java.util.Scanner;
class Mirror{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("M = ");
int m = Integer.parseInt(in.nextLine());
if(m < 3 || m > 19){
System.out.println("SIZE OUT OF RANGE");
return;
}
int a[][] = new int[m][m];
System.out.println("Enter matrix elements:");
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
a[i][j] = Integer.parseInt(in.nextLine());
}
}
System.out.println("ORIGINAL MATRIX");
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
for(int i = 0; i < m; i++){
for(int j = 0; j < m / 2; j++){
int temp = a[i][j];
a[i][j] = a[i][m - 1 - j];
a[i][m - 1 - j] = temp;
}
}
System.out.println("MIRROR IMAGE MATRIX");
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
}
}
M=3
Enter matrix elements:
1
2
3
4
5
6
7
8
9
ORIGINAL MATRIX
1 2 3
4 5 6
7 8 9
MIRROR IMAGE MATRIX
3 2 1
6 5 4
9 8 7
M=2
SIZE OUT OF RANGE
STEP 1: Start
STEP 2: Accepting the order of the matrix
STEP 3: Accepting the individual elements inside the matrix
STEP 4: Display the original matrix
STEP 5: Find out the saddle point of the matrix by applying the
necessary conditions
(Row = minimum & Column = maximum)
STEP 6: Display the saddle point of the matrix
STEP 7: Stop
import java.util.*;
class SaddlePoint11
{
public static void main(String args[])
{
Scanner sc= new Scanner (System.in);
System.out.print("Enter the order of the matrix : ");
int n=sc.nextInt();
int A[][]=new int[n][n];
System.out.println("Inputting the elements in the matrix");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("Enter Element at ["+i+"]["+j+"] : ");
A[i][j]=sc.nextInt();
} }
/* Printing the Original Matrix */
System.out.println("The Original Matrix is");
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
} System.out.println(); }
if(max==min)
{
f=1;
}
}
if(f==0)
{
ALGORITHM:
STEP 1: Start
STEP 2 :Accepting the order of the matrix
STEP 3: Accepting the individual elements of the matrix
STEP 4: Display the original matrix
STEP 5: Apply the necessary condition to get the minimum & maximum
element present in the matrix
STEP 6: Display the minimum & maximum element of the matrix
STEP 7: Stop