0% found this document useful (0 votes)
69 views72 pages

Stack and Queue Project For Class 12 Project

The document explains the stack data structure, which operates on a Last In First Out (LIFO) principle, allowing operations like push (adding an element) and pop (removing an element). It provides algorithms for both operations, as well as functions to check the stack's status (peek, isFull, isEmpty). Additionally, it includes Java code examples for implementing stack operations using arrays.

Uploaded by

arkadippanja21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views72 pages

Stack and Queue Project For Class 12 Project

The document explains the stack data structure, which operates on a Last In First Out (LIFO) principle, allowing operations like push (adding an element) and pop (removing an element). It provides algorithms for both operations, as well as functions to check the stack's status (peek, isFull, isEmpty). Additionally, it includes Java code examples for implementing stack operations using arrays.

Uploaded by

arkadippanja21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

STACK OPERATION

Stack is a linear data structure(elements in it have a fixed position). It


is an abstract data type. It allows data operations at one end only. At
any time we can access the top value only. It follows LIFO principle.
LIFO stands last in first out. The element entered at last is accessed
first. The following operations are performed in stack:-
Push()- Storing an element in the stack.
Pop()-Deleting an element from the stack.

To check the status of a stack following functions are used


peek()- To get the top element.
isFull()- To check whether stack is full or not.
isEmpty()- To check whether stack is empty or not.

ARKADIP PANJA Page | 1


PUSH OPERATION
The process of entering the elements in the stack is called push.

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

ARKADIP PANJA Page | 2


78
TOP
23

67

56

Stack after entering 89

89
TOP

78

23

67

56

ARKADIP PANJA Page | 3


POP OPERATION
It is used for deleting an element from the stack

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

ARKADIP PANJA Page | 4


78 ​TOP

34
Deleting 78 from the stack
56

23

34 TOP
Stack after deleting 78
56

23

ARKADIP PANJA Page | 5


PROGRAM 1
Write a program to accept 5 elements using array and perform stack
operation(push,pop,display).

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

ARKADIP PANJA Page | 6


CODE:

import java.util.*;//importing package


class stacks_string
{
static int top = -1; // Initializing pointer variable
static String x; // To accept input from user
static String ar[] = new String[10];// Array to store stack elements
static int c,n,ch, i; // Used for iteration and stack operation
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
do

{
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");

ARKADIP PANJA Page | 7



}
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(String x) // Push function
{
if ( top ==n-1) // Condition for overflow
{
System.out.println("STACK IS OVERFLOWED"); System.exit(0); // Termination of
program
}
else
{
top++;
ar[top] = x;// Push the element onto stack
System.out.println("THE STACK IS AS FOLLOWS :"); for(int i = top; i>=0; i--)
{
// Printing of stack elements
System.out.println(ar[i]);

}
}
}
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
}
}
}

ARKADIP PANJA Page | 8



VARIABLE DESCRIPTION TABLE:

Data Type Name Description

int ar[] Array to store elements


and implement stack.

int top Pointer variable that


indicates the top of the
stack.

int ch User's choice from the


menu options.

int c Variable to determine if


the user wants to
continue.

int x Value to be pushed onto


the stack.

ARKADIP PANJA Page | 9



OUTPUT:
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter a value: 7
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter a value: 18
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter a value: 45
Want to continue? (1 for Yes, 0 for No): 1
STACK

ARKADIP PANJA Page | 10



1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter a value: 1
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter a value: 10
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
10
1
45
18
7

ARKADIP PANJA Page | 11



Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
The deleted item is: 10
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
The deleted item is: 1
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
The deleted item is: 45
Want to continue? (1 for Yes, 0 for No): 1
STACK

ARKADIP PANJA Page | 12



1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
18
7
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
The deleted item is: 18
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
The deleted item is: 7
Want to continue? (1 for Yes, 0 for No): 1
STACK
1. Push

ARKADIP PANJA Page | 13



2. Pop
3. Display
4. Exit
Enter your choice: 2
Stack is underflowed
Want to continue? (1 for Yes, 0 for No):

ARKADIP PANJA Page | 14



PROGRAM 2
Write a program to accept 5 names using array and perform stack
operation(push,pop,display).

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

ARKADIP PANJA Page | 15



CODE:

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
{

ARKADIP PANJA Page | 16



System.out.println("Stack is overflowed");
}
else
{
ar[++top] = x; // Push the element onto stack System.out.println("The stack is as
follows:"); display(); // Print stack elements
}

}
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
}
}
}

ARKADIP PANJA Page | 17



VARIABLE DESCRIPTION TABLE:

Data Type Name Description


String ar[] Array to store elements
and implement stack.
int top Pointer variable that
indicates the top of the
stack.

int ch User's choice from the


menu options.
int c Variable to determine if
the user wants to
continue.

String x Variable to accept input


from the user..
int n Variable representing
the size of the stack, set
to 10 in the ‘main’
method.

int i Variable used for


iteration in loops,
especially in the display
logic.

ARKADIP PANJA Page | 18



OUTPUT:

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

ARKADIP PANJA Page | 19



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
France
THE STACK IS AS FOLLOWS :
France
Brazil
Argentina
Germany
India
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
3
THE STACK IS AS FOLLOWS
France
Brazil
Argentina
Germany
India
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
2
THE DELETED ELEMENT IS:France
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
2
THE DELETED ELEMENT IS:Brazil
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push

ARKADIP PANJA Page | 20



2.pop
3.display
4.exit
2
THE DELETED ELEMENT IS:Argentina
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
3
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
2
THE DELETED ELEMENT IS:Germany
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
2
THE DELETED ELEMENT IS:India
Want to continue? (1 for Yes, 0 for No): 1
STACKS STRING
1.push
2.pop
3.display
4.exit
2
THE STACK IS UNDERFLOWED

ARKADIP PANJA Page | 21



QUEUE OPERATION
Queue is a linear data structure in which the insertion an deletion operation are
performed at two different ends. The insertion operation is performed at the
rear position while the deletion operation is performed at the front position. It
uses the principle of FIFO(First In First Out).
OPERATIONS ON QUEUE
❭enQueue()-To insert an element in the queue
❭deQueue()-To delete an element from the queue
❭display()-To display the elements of the queue

56 61 45 60

Front​ Rear

Entering 7 in the queue

56 61 45 60 7

Front​ Rear

Deleting 7 in the queue

56 61 45 60

Front​ Rear


ARKADIP PANJA Page | 22



PROGRAM 3
Write a program to accept 5 elements using array and
perform queue operation using enqueue(),dequeue() and
display() function

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

ARKADIP PANJA Page | 23


CODE:

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);
}

static void enqueue(int x)// method to enter values in queue


{
if (rear == 5 - 1)
{
System.out.println("Queue is overflowed");
}
else
ARKADIP PANJA Page | 24
{
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

ARKADIP PANJA Page | 25


VARIABLE DESCRIPTION TABLE:

Data Type Name Description

int arr[] Array to store elements


and implement queue.

int front Stores the 1st element of


the queue at an instant.

int rear Stores the last element


of the queue at an
instant and checks
whether queue is empty
or not.

int ch Stores the choice of the


user to perform
operations.

int x Stores the value to be


entered in queue.

int c Variable to determine if


the user wants to
continue.

ARKADIP PANJA Page | 26


OUTPUT:

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

ARKADIP PANJA Page | 28


3. Display
4. Exit
3
The queue is as follows:
4
9
16
25
36
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: 4
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: 9

ARKADIP PANJA Page | 29


Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: 16
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
3
The queue is as follows:
25
36
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit

ARKADIP PANJA Page | 30


2
The deleted item is: 25
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: 36
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The queue is underflowed
Want to continue? Press 1 for Yes, any other key for No

ARKADIP PANJA Page | 31


PROGRAM 4
Write a program to accept 5 words using array and perform
queue operation using enqueue(),dequeue() and display()
function

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

ARKADIP PANJA Page | 32


CODE:

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

ARKADIP PANJA Page | 34


VARIABLE DESCRIPTION TABLE:

Data Type Name Description

String arr[] Array to store elements


and implement queue.

int front Stores the 1st element of


the queue at an instant.

int rear Stores the last element


of the queue at an
instant and checks
whether queue is empty
or not.

int ch Stores the choice of the


user to perform
operations.

String x Stores the value to be


entered in queue.

int c Variable to determine if


the user wants to
continue.

ARKADIP PANJA Page | 35


OUTPUT:

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

ARKADIP PANJA Page | 36


Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
3
The queue is as follows:
Russia
Netherland
Spain
Uruguay
Australia
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: Russia
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: Netherland
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: Spain
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
3
The queue is as follows:

ARKADIP PANJA Page | 37


Uruguay
Australia
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: Uruguay
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The deleted item is: Australia
Want to continue? Press 1 for Yes, any other key for No
1
QUEUE
1. Enqueue
2. Dequeue
3. Display
4. Exit
2
The queue is underflowed
Want to continue? Press 1 for Yes, any other key for No

ARKADIP PANJA Page | 38


What is Double Ended Queue (DeQueue)?
DeQueue stands for Double Ended Queue.
It is just like a queue but does not support FIFO structure.
Insertion and deletion can be done from both side( FRONT &
REAR).
The Operations in DeQueue are
Initialize –same as circular queue.
Insertion at rear – same as circular queue.
Deletion from front – same as circular queue.
Insertion at from
Deletion from rear.
Full status check – same a circular queue.
Empty status check – same as circular queue.

ARKADIP PANJA Page | 39


PROGRAM 5

Class name: DeQueue


Data members/instance variables:
qrr[]: array to hold integer elements
lim: maximum capacity of the dequeue
front: to point the index of the front end
rear: to point the index of the rear end
Methods/Member functions:
DeQueue(int l): constructor to initialize lim = l, front = 0 and rear = 0
void addFront(int v): to add integers in the dequeue at the
front end if possible, otherwise display the message “OVERFLOW FROM
FRONT”
void addRear(int v): to add integers in the dequeue at the rear end
if possible, otherwise display the message “OVERFLOW FROM REAR”
int popFront(): removes and returns the integers from the front end
of the dequeue if any, else returns -999
int popRear(): removes and returns the integers from the rear end
of the dequeue if any, else returns -999
void show(): displays the elements of the dequeue

(i) Specify the class DeQueue giving details of the


functions void addFront(int) and int popFront().

ARKADIP PANJA Page | 40


Note:Algorithm INSERT-AT-FRONT and DELETE-FROM-REAR are new in DeQueue and
other are same as circular queue.

ALGORITHM:

INSERT-AT-FRONT (DEQUEUE, FRONT,REAR,MAX,COUNT,ITEM)


This algorithm is used to insert item at front,
usually insertion in queue is done from rear but ​
in dequeue we can insert item from front also.

1.​ If ( COUNT = MAX ) then


a.​ Print “DeQueue Overflow”;
b.​ Return;
2.​ Otherwise
a.​ If( FRONT = 1 ) then
i.​ FRONT := MAX;
b.​ Otherwise
i.​ FRONT := FRONT – 1;
c.​ DEQUEUE(FRONT) := ITEM;
d.​ COUNT : = COUNT + 1;
3.​ Return;
DELETE-FROM-REAR(DEQUEUE,FRONT,REAR,COUNT,ITEM)
This algorithm is used to delete item from rear,
usually deletion in queue is done from front but
in dequeue we can delete item from rear also.

1.​ If(COUNT = 0 ) then


a.​ Print “DeQueue underflow”;
b.​ Return;
2.​ Otherwise
a.​ ITEM := DEQUEUE(REAR);
b.​ If ( REAR = 1) then
i.​ REAR := MAX;
c.​ Otherwise
i.​ REAR := REAR -1;
d.​ COUNT := COUNT – 1;
3.​ Return;

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.

ARKADIP PANJA Page | 41


CODE:

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;
}
}
}
}

ARKADIP PANJA Page | 44


VARIABLE DESCRIPTION TABLE:

VARIABLE TABLE DATA TYPE DESCRIPTION

qrr int[] Array that holds the


element of the dequeue
lim int Limit of the dequeue

front int Index pointing to the


front of the DeQueue
for insertion and
deletion
rear int Index pointing to the
rear of the DeQueue
for insertion and
deletion
l int Parameter to set the
size of the lim of the
DeQueue
v int Used to store values
for the insertion and
deletion from the
DeQueue
size int Accepts the user
entered size for the
Dequeue
choice int Stores the user’s menu
choice for the
DeQueue
in Scanner Scanner object to read
user input
dq DeQueue Object of the DeQueue
class used to perform
DeQueue operations

ARKADIP PANJA Page | 45


OUTPUT:

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

ARKADIP PANJA Page | 46


4. Pop from rear
5. Display elements
Enter your choice: 3
18 popped
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 5
45 99
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 4
99 popped
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 4
45 popped
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice: 4
Underflow from rear
1. Add from front
2. Add from rear
3. Pop from front
4. Pop from rear
5. Display elements
Enter your choice:

ARKADIP PANJA Page | 47


PROGRAM 6 :{MATRIX}

Write a program to declare a matrix A[][] of order (M x N) where 'M' is the


number of rows
and 'N' is the number of columns such that the value of 'M' must be greater than
0 and less than 10 and the value of 'N' must be greater than 2 and less than 6.
Allow the user to input digits (0 - 7) only at each location, such that each row
represents
an octal number.
Example:
2​ 3​ 1​ (decimal equivalent of 1st row = 153 i.e. 2x82 + 3x81 +
1x80)
4​ 0​ 5​ (decimal equivalent of 2nd row = 261 i.e. 4x82 + 0x81 +
5x80)
1​ 5​ 6​ (decimal equivalent of 3rd row = 110 i.e. 1x82 + 5x81 +
6x80)
Perform the following tasks on the matrix:
1.​ Display the original matrix.
2.​ Calculate the decimal equivalent for each row and display as per the
format given below.
Test your program for the following data and some random data:
Example 1:
INPUT:
M=
1
N=3
ENTER ELEMENTS FOR ROW 1: 1 4 4
OUTPUT:
FILLED MATRIX​DECIMAL EQUIVALENT
1​ 4​ 4​ 100

ARKADIP PANJA Page | 48


ALGORITHM:

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

ARKADIP PANJA Page | 49


CODE:

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();

if (m <= 0 || m >= 10 || n <= 2 || n >= 6) {


System.out.println("OUT OF RANGE");
return;
}

int a[][] = new int[m][n];

for (int i = 0; i < m; i++) {


System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
if (a[i][j] < 0 || a[i][j] > 7) {
System.out.println("INVALID INPUT");
return;
}
}
}

System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT");

for (int i = 0; i < m; i++) {


int decNum = 0;
for (int j = 0; j < n; j++) {
decNum += a[i][j] * Math.pow(8, n - j - 1 );
System.out.print(a[i][j] + " ");
}
System.out.print("\t\t" + decNum);
System.out.println();
}
}}

ARKADIP PANJA Page | 50


VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION

m int Number of rows entered


by the user

n int Number of columns


entered by the user

a Int[][] A double dimensional


array to store the octal
numbers from (0-7)

i int Loop counter variable

ARKADIP PANJA Page | 51


OUTPUT:

Enter the number of rows (M): 3


Enter the number of columns (N): 3
ENTER ELEMENTS FOR ROW 1:
7
5
2
ENTER ELEMENTS FOR ROW 2:
4
1
3
ENTER ELEMENTS FOR ROW 3:
0
4
6
FILLED MATRIX​DECIMAL EQUIVALENT
7 5 2 ​​ 490
4 1 3 ​​ 267
0 4 6 ​​ 38
Enter the number of rows (M): 3
Enter the number of columns (N): 3
ENTER ELEMENTS FOR ROW 1:
8
INVALID INPUT

ARKADIP PANJA Page | 52


PROGRAM 7:

*Write a program to declare a square matrix A[ ][ ] of order MxM where ‘M’ is


the number
* of rows and the number of columns, such that M must be greater than 2 and
less than 10.
* Accept the value of M as user input. Display an appropriate message for an
invalid input.
* Allow the user to input integers into this matrix. Perform the following tasks:

(a) Display the original matrix.


(b) Rotate the matrix 90° clockwise as shown below:
Original matrix Rotated matrix
123 741
456 852
789 963

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

ARKADIP PANJA Page | 53


CODE:

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];

/* Inputting the matrix */


for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}

/* Printing the original matrix */


// System.out.println("*************************");
System.out.println("The Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
// System.out.println("*************************");
int B[][]=new int[m][m];
int x;
/*Rotation of matrix begins here */
for(int i=0;i<m;i++)
{
ARKADIP PANJA Page | 54
x = m-1;
for(int j=0;j<m;j++)
{
B[i][j]=A[x][i];
x--;
}
}

/* Printing the rotated matrix */


System.out.println("Matrix After Rotation is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
// System.out.println("*************************");
}
}
}

ARKADIP PANJA Page | 55


VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION


m int To store the size
of the square
matrix
A Int[][] To store the
original matrix
element
i int Loop counter
variable
j int Loop counter
variable
x int Temporary
variable used to
rotate the
column index for
rotating the
matrix
B int[][] A 2D array to
store the rotated
matrix
sum int To store the sum
of the four
corner elements
of the matrix

ARKADIP PANJA Page | 56


OUTPUT:

Enter the size of the matrix : 3


Enter an element : 1
Enter an element : 2
Enter an element : 3
Enter an element : 4
Enter an element : 5
Enter an element : 6
Enter an element : 7
Enter an element : 8
Enter an element : 9
The Original Matrix is :
1​ 2​ 3​
4​ 5​ 6​
7​ 8​ 9​
Matrix After Rotation is :
7​ 4​ 1​
8​ 5​ 2​
9​ 6​ 3​
Enter the size of the matrix : 2
Size Out Of Range

ARKADIP PANJA Page | 57


PROGRAM 8:

Write a program to declare a square matrix a[][] of order (M × M) where ‘M’ is


the number of rows and the number of columns such that M must be greater
than 2 and less than 20.

Allow the user to input integers into this matrix. Display appropriate error
message for an invalid input. Perform the following tasks:

(a) Display the input matrix.


(b) Create a mirror image of the inputted matrix.
(c) Display the mirror image matrix.

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

ARKADIP PANJA Page | 58


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 create the mirror image of
the matrix
STEP 6:Display the mirror image of the matrix
STEP 7:Stop

ARKADIP PANJA Page | 59


CODE:

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();
}
}
}

ARKADIP PANJA Page | 60


VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION

m int To store the size


of the square
matrix
a int[][] 2d array to store
the elements of
the matrix
i int Loop counter
variable

j int Loop counter


variable

temp int Temporary


variable used to
swap the
elements while
mirroring the
matrix

ARKADIP PANJA Page | 61


OUTPUT:

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

ARKADIP PANJA Page | 62


PROGRAM 9:

Perform the following tasks on the matrix:


(i) Output the original matrix.
(ii) Find the SADDLE POINT for the matrix. If the matrix has no saddle point,
output the message “NO SADDLE POINT”.
[Note: A saddle point is an element of the matrix such that it is the minimum
element for the row
to which it belongs and the maximum element for the column to which it
belongs.
Saddle point for a given matrix is always unique.]
Example: In the Matrix
456
789
513
Saddle point = 7 because it is the minimum element of row 1 and maximum
element of column 0

ARKADIP PANJA Page | 63


ALGORITHM:

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

ARKADIP PANJA Page | 64


CODE:

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(); }

int max, min, x, f=0;


for(int i=0;i<n;i++)
{
/* Finding the minimum element of a row */
min = A[i][0]; // Initializing min with first element of every row
x = 0;
for(int j=0;j<n;j++)
{
if(A[i][j]<min)
{
min = A[i][j];
x = j; // Saving the column position of the minimum element of the
row
} }
/* Finding the maximum element in the column corresponding to the
minimum element of row */
ARKADIP PANJA Page | 65
max = A[0][x]; // Initializing max with first element of that column
for(int k=0;k<n;k++)
{
if(A[k][x]>max)
{
max = A[k][x];
} }
/* If the minimum of a row is same as maximum of the corresponding
column,
then, we have that element as the Saddle point */

if(max==min)
{

System.out.println("Saddle point = "+max);

f=1;
}
}

if(f==0)
{

System.out.println("No saddle point");


System.out.println("********************");
}
}
}

ARKADIP PANJA Page | 66


VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION

i,j,k int Loop counter variable

n int To store the no.of rows


and columns in a square
matrix
A int[][] 2D array to store matrix
elements

min int Stores the minimum


element of the particular
row
max int Stores the maximum
element of the particular
column
f int Flag to indicate if a
saddle point is found

sc Scanner Used to take input from


the user

ARKADIP PANJA Page | 67


OUTPUT:

Enter the order of the matrix : 3


Inputting the elements in the matrix
Enter Element at [0][0] : 1
Enter Element at [0][1] : 2
Enter Element at [0][2] : 3
Enter Element at [1][0] : 4
Enter Element at [1][1] : 5
Enter Element at [1][2] : 6
Enter Element at [2][0] : 7
Enter Element at [2][1] : 8
Enter Element at [2][2] : 9
The Original Matrix is
1​ 2​ 3​
4​ 5​ 6​
7​ 8​ 9​
Saddle point = 7
Enter the order of the matrix : 3
Inputting the elements in the matrix
Enter Element at [0][0] : 2
Enter Element at [0][1] : 4
Enter Element at [0][2] : 6
Enter Element at [1][0] : 8
Enter Element at [1][1] : 1
Enter Element at [1][2] : 3
Enter Element at [2][0] : 5
Enter Element at [2][1] : 7
Enter Element at [2][2] : 9
The Original Matrix is
2​ 4​ 6​
8​ 1​ 3​
5​ 7​ 9​
No saddle point
********************

ARKADIP PANJA Page | 68


PROGRAM 10:
Write a program to declare a matrix of A[][] of ‘n’ order
1. Display the original matrix
2. Find out the maximum & minimum element from the matrix

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

ARKADIP PANJA Page | 69


CODE:
import java.util.*;
class mat_min_max
{
public static void main(String args[])
{
int i,j,m,n;
Scanner sc= new Scanner (System.in);
System.out.print("Enter the no. of rows and columns: ");
n=sc.nextInt();
int A[][]=new int[n][n];
System.out.println("Enter the elements: ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
A[i][j]=sc.nextInt();
}}
int min=A[0][0]; int max=min;
System.out.println("matrix: ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]);
System.out.print(" ");
}
System.out.println( );
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if (A[i][j]<min)
min=A[i][j];
if (A[i][j]>max)
max=A[i][j];
}
}
System.out.println("maximum elements = "+ max);
System.out.println("minimum elements = "+ min);
} }

ARKADIP PANJA Page | 70


VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE DESCRIPTION

min int Stores the minimum value


found in the matrix

max int Stores the maximum value


found in the matrix

i,j int Loop counter variables

n int To store the no. of rows


and columns of the square
matrix

A int[][] 2D array to store the


matrix elements

ARKADIP PANJA Page | 71


OUTPUT:
Enter the no. of rows and columns: 3
Enter the elements:
1
2
3
4
5
6
7
8
9
matrix:
123
456
789
maximum elements = 9
minimum elements = 1

ARKADIP PANJA Page | 72

You might also like