0% found this document useful (0 votes)
35 views

Program:: Contents of The PACKAGE: Queue Class

The document describes a program that implements queue and stack operations using classes. The Queue and Stack classes contain methods for enqueue, dequeue, push, pop and viewing the contents. The main class allows the user to choose between queue or stack operations and perform actions like adding and removing elements or viewing the contents. Sample outputs of running the program are also shown.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Program:: Contents of The PACKAGE: Queue Class

The document describes a program that implements queue and stack operations using classes. The Queue and Stack classes contain methods for enqueue, dequeue, push, pop and viewing the contents. The main class allows the user to choose between queue or stack operations and perform actions like adding and removing elements or viewing the contents. Sample outputs of running the program are also shown.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Program:

Contents of the PACKAGE :

Queue class
package datastructure;
/**<u>Queue class</u> This class is used to perform enqueue , dequeue operation
and also view the contents of the queue*/
public class queue
{
public int QSIZE=3;
public int FULL=0;
public int EMPTY=1;
public int SOMEDATA=2;
public int front,rear,qstatus;
/**<u>Queuemenu method</u> This method is used to display the operations that
can be performed*/
public void queuemenu()
{
System.out.println("1.Enqueue");
System.out.println("2.Dequeue");
System.out.println("3.View");
System.out.println("4.Exit");
}
/**<u>Enqueue method</u> This method is used to perform enqueue operation */
public void enqueue(int queue[],int data)
{
qstatus=SOMEDATA;
rear+=1;
if(rear==QSIZE)
rear=0;
if((front==-1&&rear==(QSIZE-1)||rear==front))
qstatus=FULL;
queue[rear]=data;
}
/**<u>Dequeue method</u> This method is used to perform dequeue operation */
public int dequeue(int queue[])
{
front++;
if(front==QSIZE)
front=0;
if(front==rear)
qstatus=EMPTY;
else
qstatus=SOMEDATA;
return (queue[front]);
}
/**<u>View method</u> This method is used to display contents of queue */
public void viewQ(int queue[])
{
int i;
if(qstatus==EMPTY)
System.out.println("The queue is empty");
else
{
i=front;
System.out.println("Queue contains...Front-->");
do
{
System.out.println(" "+queue[i=(i+1)%QSIZE]);
}while(i!=rear);
System.out.println("<--Rear");
if(qstatus==FULL)
System.out.println("Queue is full !");
}
}
}

Stack class
package datastructure;
/**<u>Stack class</u> This class is used to perform enqueue , dequeue operation
and also view the contents of the stack*/
public class stack
{
public int top;
public int MAX=4;
public int element;
public stack()
{
top=-1;
}
/**<u>Displaymenu method</u> This method is used to display the operations that
can be performed*/
public void displaymenu()
{
System.out.println("1.Push");
System.out.println("2.Pop");
System.out.println("3.View");
System.out.println("4.Exit");
}
/**<u>Push method</u> This method is used to perform push operation */
public int push(int stk[],int element)
{
if(top<(MAX-1))
{
stk[++top]=element;
return 0;
}
else
return -1;
}
/**<u>Pop method</u> This method is used to perform pop operation */
public int pop(int stk[])
{

if(top>=0)
{
element=stk[top--];
return 0;
}
else
return -1;
}
/**<u>Printstk method</u> This method is used to display contents of queue */
public void printstk(int stk[])
{
int i;
if(top==-1)
System.out.println("Stack is empty");
else
{
System.out.println("The content of the stack is... TOP");
for(i=top;i>=0;i--)
System.out.println("-->"+stk[i]);
if(top==(MAX-1))
System.out.println("Stack is full");
}
}
}
Contents of the MAIN PROGRAM :

import datastructure.*;
import java.io.*;

/**<u>Main class</u> This class is used to perform stack and queue operation
based on the choice given by the user */
class sqmain
{
public static void main(String args[]) throws IOException
{
stack s=new stack();
queue q=new queue();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int choice;
do
{
System.out.println("Implementation of Stack & Queue With user defined Packages");
System.out.println("1.Queue operation");
System.out.println("2.Stack operation");
System.out.println("9.Exit");
System.out.println("Enter your choice:");
choice=Integer.parseInt(br.readLine());
System.out.println(" ");
switch(choice)
{
case 1:
int data,ch,b=1;
int que[]=new int[q.QSIZE];
q.queuemenu();
q.rear=-1;
q.front=-1;
q.qstatus=q.EMPTY;
while(b!=4)
{
System.out.println(" Enter choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
if(q.qstatus==q.FULL)
System.out.println("Overflow on enqueue");
else
{
System.out.println("Enter the element:");
data=Integer.parseInt(br.readLine());
q.enqueue(que,data);
}
break;
case 2:
if(q.qstatus==q.EMPTY)
System.out.println("Underflow on dequeue");
else
{
q.dequeue(que);
}
break;
case 3:
q.viewQ(que);
break;
case 4:
b=4;
break;
}
}
break;
case 2:
int data1,status,a=1,ch1;
int stk[]=new int[s.MAX];
s.displaymenu();
while(a!=4)
{
System.out.println("Enter Choice");
ch1=Integer.parseInt(br.readLine());
switch(ch1)
{
case 1:
System.out.println("Enter the element");
data1=Integer.parseInt(br.readLine());
status=s.push(stk,data1);
if(status==-1)
System.out.println("Overflow of stack");
break;
case 2:
status=s.pop(stk);
if(status==-1)
System.out.println("Underflow of stack");
else
{
System.out.println("The popped value is" +s.element);
}
break;
case 3:
s.printstk(stk);
break;
case 4:
a=4;
break;
}
}
break;
}
}while(choice!=9);
}
}

Output:
Generating documentation for the program
C:\JDK15~1.0\bin>javac sqmain.java

C:\JDK15~1.0\bin>java sqmain
Implementation of Stack & Queue With user defined Packages
1.Queue operation
2.Stack operation
9.Exit
Enter your choice:
1
1.Enqueue
2.Dequeue
3.View
4.Exit
Enter choice:
1
Enter the element:
10
Enter choice:
1
Enter the element:
11
Enter choice:
1
Enter the element:
12
Enter choice:
3
Queue contains...Front-->
10
11
12
<--Rear
Queue is full !
Enter choice:
2
Enter choice:
3
Queue contains...Front-->
11
12
<--Rear
Enter choice:
4
Implementation of Stack & Queue With user defined Packages
1.Queue operation
2.Stack operation
9.Exit
Enter your choice:
2

1.Push
2.Pop
3.View
4.Exit
Enter Choice
1
Enter the element
10
Enter Choice
1
Enter the element
11
Enter Choice
1
Enter the element
12
Enter Choice
3
The content of the stack is... TOP
-->12
-->11
-->10
Enter Choice
2
The popped value is12
Enter Choice
2
The popped value is11
Enter Choice
3
The content of the stack is... TOP
-->10
Enter Choice
4
Implementation of Stack & Queue With user defined Packages
1.Queue operation
2.Stack operation
9.Exit
Enter your choice:
9

C:\JDK15~1.0\bin

You might also like