0% found this document useful (0 votes)
5 views2 pages

Java Array-Based Word Queue Implementation

The document presents a Java implementation of a queue data structure using an array, specifically designed to handle characters. It includes methods for adding, removing, and printing characters in a FIFO manner, along with a user interface for interaction. The main method allows users to perform operations on the WordQueue through a simple console menu.

Uploaded by

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

Java Array-Based Word Queue Implementation

The document presents a Java implementation of a queue data structure using an array, specifically designed to handle characters. It includes methods for adding, removing, and printing characters in a FIFO manner, along with a user interface for interaction. The main method allows users to perform operations on the WordQueue through a simple console menu.

Uploaded by

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

//QUEUE using an Array

import [Link].*;

public class WordQueue {


char ch[];
int size,front,rear;
public WordQueue(int cap) {
ch=new char[cap];
size=cap;
front=rear=-1;
}
void addChar(char v) {
if(rear == size-1)
[Link]("WordQueue is full....");
else if(rear == -1) {
front=rear=0;
ch[rear]=v;
[Link](v+" added to WordQueue");
}
else {
ch[++rear]=v;
[Link](v+" added to WordQueue");
}
}
char removeChar() {
char pop='\\';
if(front == -1)
[Link]("WordQueue is empty....");
else if(front == rear) {
pop=ch[front];
front=rear=-1;
}
else
pop=ch[front++];

return pop;
}
void printWordQueue() {
if(front == -1)
[Link]("WordQueue is empty....");
else {
int i;
for(i=front;i<rear;i++) {
[Link](ch[i]+" -> ");
}
[Link](ch[i]+" !!!");
}
}
public static void main(String arg[]) {
Scanner inp=new Scanner([Link]);
WordQueue obj=new WordQueue(5);
char choice='y', chr;
int opt;

do {
[Link]("***** FIFO DATA STRUCTURE -> QUEUE *****");
[Link]("1. Add Chracter");
[Link]("2. Remove Chracter");
[Link]("3. Print WordQueue");
[Link]("\nEnter your choice : ");
opt=[Link]();

switch(opt) {
case 1:
[Link]("Enter a Character : ");
chr=[Link]().charAt(0);
[Link](chr);
break;
case 2:
[Link]([Link]()+" is popped from
WordQueue");
break;
case 3:
[Link]();
break;
default:
[Link]("Invalid Choice! Try again...");
}
[Link]("\nDo you want to continue (y/n) : ");
choice=[Link]().charAt(0);
}while(choice!='n');
}
}

You might also like