[Link].
: 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:
[Link]
import [Link];
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())
[Link]("Overflow");
else
st[++top] = element;
}
public intpop()
{
if(isEmpty())
{
[Link]("UnderFlow");
return (-1);
}
return (st[top--]);
}
public void printStack()
{
[Link]("Stack Elements:");
for (int i = top; i>=0; i--)
10
[Link](st[i]);
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter stack size");
int size=[Link]();
Stack obj = new Stack(size);
while (true)
{
[Link]("\nSTACK\n*****\[Link]\[Link]
\[Link]\[Link]\nEnter your choice");
intch = [Link]();
switch (ch)
{
case 1:
[Link]("Enter Element");
int n = [Link]();
[Link](n);
break;
case 2:
[Link]("Poped element is %d", [Link]());
break;
case 3:
[Link]();
break;
case 4:
[Link](0);
default:
[Link]("Wrong option");
}
}
}
}
OUTPUT:
D:\Java\CS3381>java Stack
Enter stack size
5
STACK
*****
[Link]
2. POP
11
3. Display
[Link]
Enter your choice
1
Enter Element
12
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
1
Enter Element
34
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
1
Enter Element
56
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
1
Enter Element
78
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
3
Stack Elements:
78
12
56
34
12
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
2
Poped element is 78
STACK
*****
[Link]
2. POP
3. Display
[Link]
Enter your choice
3
Stack Elements:
56
34
12
STACK
*****
[Link]
2. POP
3. Display
[Link]
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
[Link].: 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:
[Link]
import [Link];
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())
{
[Link]("Queue is full");
15
}
else
{
if (front == -1)
front = 0;
rear++;
items[rear] = element;
[Link]("Inserted " + element);
}
}
intdeQueue()
{
int element;
if (isEmpty())
{
[Link]("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())
{
[Link]("Empty Queue");
}
else
{
[Link]("\nFront index-> " + front);
[Link]("Items -> ");
for (i = front; i<= rear; i++)
[Link](items[i] + " ");
[Link]("\nRear index-> " + rear);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter queue size");
int size=[Link]();
Queue obj = new Queue(size);
while (true)
{
[Link]("\nQUEUE\n*****\[Link]\[Link]\
[Link]\[Link]\nEnter your choice");
intch = [Link]();
switch (ch)
{
case 1:
[Link]("Enter Element");
int n = [Link]();
[Link](n);
break;
case 2:
17
[Link]("Dequeued element is %d", [Link]());
break;
case 3:
[Link]();
break;
case 4:
[Link](0);
default:
[Link]("Wrong option");
}
}
}
}
OUTPUT:
D:\Java\CS3381>javac [Link]
D:\Java\CS3381>java Queue
Enter queue size
5
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
12
Inserted 12
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
34
Inserted 34
18
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
56
Inserted 56
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
1
Enter Element
78
Inserted 78
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
3
Front index-> 0
Items ->
12 34 56 78
Rear index-> 3
QUEUE
*****
[Link]
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
2
Poped element is 12
QUEUE
*****
[Link]
19
2. DEQUEUE
3. DISPLAY
4. EXIT
Enter your choice
3
Front index-> 1
Items ->
34 56 78
Rear index-> 3
QUEUE
*****
[Link]
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