[go: up one dir, main page]

0% found this document useful (0 votes)
10 views13 pages

EX - 2 Oops and Java

The document outlines the implementation of stack and queue data structures in Java using classes and objects. It provides algorithms and sample code for both data structures, detailing operations such as push, pop for stack and enqueue, dequeue for queue. The programs are designed to handle user input and display results, demonstrating successful execution.

Uploaded by

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

EX - 2 Oops and Java

The document outlines the implementation of stack and queue data structures in Java using classes and objects. It provides algorithms and sample code for both data structures, detailing operations such as push, pop for stack and enqueue, dequeue for queue. The programs are designed to handle user input and display results, demonstrating successful execution.

Uploaded by

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

EX.NO.

: 2(a) DEVELOP STACK DATA STRUCTURES USING CLASSES AND


DATE: OBJECTS

AIM:
To develop a Java program to implement stack data structure using classes and objects.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a class named Stack and declare the instance variables st[], top,
maxsize as private.
Step3:Use constructor to allocate memory space for the array and initialize top as -1.
Step 4: Define methods such as isFull(), isEmpty(), push(), pop() and printStack();
Step 5: Push Operation
Step 5.1: Check whetherstack has some space or stack is full.
Step 5.2: If the stack has no space then display “overflow” and exit.
Step 5.3: If the stack has space then increase top by 1 to point next empty space.
Step 5.4: Add element to the new stack location, where top is pointing.
Step 5.5: Push operation performed successfully.
Step 6: Pop operation
Step 6.1: Check whether stack has some element or stack is empty.
Step 6.2: If the stack has no element means it is empty then display “underflow”
Step 6.3: If the stack has some element, accesses the data element at which top is
pointing.
Step 6.4: Decrease the value of top by 1.
Step 6.5: Pop operation performed successfully.
Step 7: PrintStack operation
Step 7.1:Check whether stack has some element or stack is empty.
Step 7.2: If the stack has no element means it is empty then display “underflow”.
Step 7.3: If the stack has some element, traverse the array from top to bottom and
display the elements.
Step 8: Define the main() method
Step 9: Create object of Stack class.
Step 10: Display the menu and get the user choice and invoke appropriate method.
Step 11: Stop the program.

9
PROGRAM:

Stack.java

import java.util.Scanner;
public class Stack
{
private intmaxsize, top;
private int[] st;
public Stack(int size)
{
maxsize = size;
st = new int[maxsize];
top = -1;
}
booleanisEmpty()
{
return top==-1;
}
booleanisFull()
{
return top==maxsize-1;
}
public void push(int element)
{
if(isFull())
System.out.println("Overflow");
else
st[++top] = element;
}
public intpop()
{
if(isEmpty())
{
System.out.println("UnderFlow");
return (-1);
}
return (st[top--]);
}
public void printStack()
{
System.out.println("Stack Elements:");
for (int i = top; i>=0; i--)

10
System.out.println(st[i]);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter stack size");
int size=sc.nextInt();
Stack obj = new Stack(size);
while (true)
{
System.out.println("\nSTACK\n*****\n1.PUSH\n2.POP
\n3.Display\n4.EXIT\nEnter your choice");
intch = sc.nextInt();
switch (ch)
{
case 1:
System.out.println("Enter Element");
int n = sc.nextInt();
obj.push(n);
break;
case 2:
System.out.printf("Poped element is %d", obj.pop());
break;
case 3:
obj.printStack();
break;
case 4:
System.exit(0);
default:
System.out.println("Wrong option");
}
}
}
}

OUTPUT:

D:\Java\CS3381>java Stack
Enter stack size
5

STACK
*****
1.PUSH
2. POP

11
3. Display
4.EXIT
Enter your choice
1
Enter Element
12

STACK
*****
1.PUSH
2. POP
3. Display
4.EXIT
Enter your choice
1
Enter Element
34

STACK
*****
1.PUSH
2. POP
3. Display
4.EXIT
Enter your choice
1
Enter Element
56

STACK
*****
1.PUSH
2. POP
3. Display
4.EXIT
Enter your choice
1
Enter Element
78

STACK
*****
1.PUSH
2. POP
3. Display
4.EXIT
Enter your choice
3
Stack Elements:
78

12
56
34
12

STACK
*****
1.PUSH
2. POP
3. Display
4.EXIT
Enter your choice
2
Poped element is 78
STACK
*****
1.PUSH
2. POP
3. Display
4.EXIT
Enter your choice
3
Stack Elements:
56
34
12

STACK
*****
1.PUSH
2. POP
3. Display
4.EXIT
Enter your choice
4

D:\Java\CS3381>

RESULT:

Thus, the Java program to implement stack data structure using classes and objects has
developed and executed successfully.

13
EX.NO.: 2(b) DEVELOP QUEUE DATA STRUCTURES USING CLASSES
DATE: AND OBJECTS

AIM:
To develop a Java program to implement queue data structure using classes and objects.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a class named Queue and declare the instance variables items[], front, rear,
maxsize as private.
Step3:Use constructor to allocate memory space for the array and initialize front as -1 and
rear as -1.
Step 4: Define methods such as isFull(), isEmpty(), enQueue(), deQueue() and display();
Step 5: Enqueue Operation
Step 5.1: Check whetherqueue has some space or queue is full.
Step 5.2: If the queue has no space then display “overflow” and exit.
Step 5.3: If the queue has space then increase rear by 1 to point next empty space.
Step 5.4: Add element to the new location, where rear is pointing.
Step 5.5: Enqueue operation performed successfully.
Step 6: Dequeue operation
Step 6.1: Check whether queue has some element or queue is empty.
Step 6.2: If the queue has no element means it is empty then display “underflow”
Step 6.3: If the queue has some element, accesses the data element at which front is
pointing.
Step 6.4: Incrementthe value of front by 1.
Step 6.5:Dequeue operation performed successfully.
Step 7: Displayoperation
Step 7.1:Check whether queue has some element or queue is empty.
Step 7.2: If the stack has no element means it is empty then display “underflow”.
Step 7.3: If the stack has some element, traverse the array from front to rear and
display the elements.
Step 8: Define the main() method
Step 9: Create object of Queue class.
Step 10: Display the menu and get the user choice and invoke appropriate method.
Step 11: Stop the program.

14
PROGRAM:
Queue.java
import java.util.Scanner;
public class Queue
{
private intitems[];
private intmaxsize, front, rear;
Queue(int size)
{
maxsize=size;
items = new int[size];
front = -1;
rear = -1;
}
booleanisFull()
{
if (front == 0 && rear ==maxsize-1)
{
return true;
}
return false;
}
booleanisEmpty()
{
if (front == -1)
return true;
else
return false;
}
void enQueue(int element)
{
if (isFull())
{
System.out.println("Queue is full");

15
}
else
{
if (front == -1)
front = 0;
rear++;
items[rear] = element;
System.out.println("Inserted " + element);
}
}
intdeQueue()
{
int element;
if (isEmpty())
{
System.out.println("Queue is empty");
return (-1);
}
else
{
element = items[front];
if (front >= rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
return (element);
}
}
void display()

16
{
inti;
if (isEmpty())
{
System.out.println("Empty Queue");
}
else
{
System.out.println("\nFront index-> " + front);
System.out.println("Items -> ");
for (i = front; i<= rear; i++)
System.out.print(items[i] + " ");
System.out.println("\nRear index-> " + rear);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter queue size");
int size=sc.nextInt();
Queue obj = new Queue(size);
while (true)
{
System.out.println("\nQUEUE\n*****\n1.ENQUEUE\n2.DEQUEUE\
n3.DISPLAY\n4.EXIT\nEnter your choice");
intch = sc.nextInt();
switch (ch)
{
case 1:

System.out.println("Enter Element");
int n = sc.nextInt();
obj.enQueue(n);
break;
case 2:

17
System.out.printf("Dequeued element is %d", obj.deQueue());
break;
case 3:
obj.display();
break;
case 4:
System.exit(0);
default:
System.out.println("Wrong option");
}
}
}
}
OUTPUT:
D:\Java\CS3381>javac Queue.java

D:\Java\CS3381>java Queue
Enter queue size
5

QUEUE
*****
1.ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
12
Inserted 12

QUEUE
*****
1.ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
34
Inserted 34

18
QUEUE
*****
1.ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
56
Inserted 56

QUEUE
*****
1.ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
78
Inserted 78

QUEUE
*****
1.ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
3

Front index-> 0
Items ->
12 34 56 78
Rear index-> 3

QUEUE
*****
1.ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
2
Poped element is 12
QUEUE
*****
1.ENQUEUE

19
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
3

Front index-> 1
Items ->
34 56 78
Rear index-> 3

QUEUE
*****
1.ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
4

D:\Java\CS3381>

RESULT:

Thus, the Java program to implement queue data structure using classes and objects has
developed and executed successfully.

20

You might also like