0% found this document useful (0 votes)
6 views4 pages

STACK

The document describes two classes, WordPile and Queue, which implement stack and queue data structures respectively. WordPile can hold a maximum of 20 characters and allows adding and removing characters from one end, while Queue can hold up to 100 integers and enables adding from the rear and removing from the front. Both classes include constructors and methods for adding, removing, and displaying elements, with specific error messages for overflow and underflow conditions.

Uploaded by

me.acasa45
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)
6 views4 pages

STACK

The document describes two classes, WordPile and Queue, which implement stack and queue data structures respectively. WordPile can hold a maximum of 20 characters and allows adding and removing characters from one end, while Queue can hold up to 100 integers and enables adding from the rear and removing from the front. Both classes include constructors and methods for adding, removing, and displaying elements, with specific error messages for overflow and underflow conditions.

Uploaded by

me.acasa45
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/ 4

STACK

WordPile is an entity which can hold maximum of 20 characters . Restriction is that characters can
be added or removed from only one end . Some of the members of the class are given below :

Class Name : WordPile

Data Members/ Instance variables :

ch[ ] : character array to hold character elements

capacity : Integer variable to store maximum capacity

top : to point to the index of topmost element

Methods/Member Functions :

WordPile(int cap) : Constructor to initialise data members capacity to cap and


top to -1 and create WordPile.

void pushChar(char v) : adds character to the top of the WordPile if possible


otherwise outputs a message "Word Pile is full".

char popChar() : Returns the deleted character from WordPile if possible .


otherwise returns "//".

a) Specify class WordPile giving details of the constructor and member functions .

The main function and algorithm need not be written .

Solution:
public class WordPile

char ch[] = new char[20];

int capacity, top;

WordPile(int cap)
{

capacity = cap ;

top = -1;

ch = new char[capacity];

void pushChar(char v)

if(top<capacity-1)

ch[++top]=v;

else

System.out.println("WordPile is full");

char popChar()

if(top>=0)

return ch[top--];

else

return('/');

}
QUEUE
Queue is an entity which can hold a maximum of 100 integers. The queue enables the
user to add integers from the rear and remove integers from the front.
Define a class Queue with the following details:
Class name: Queue
Data members/instance variables:
Que[]: array to hold the integer elements
size: stores the size of the array
front: to point the index of the front
rear: to point the index of the rear
Member functions:
Queue(int mm): constructor to initialize the data size = mm, front = 0, rear = 0
void addele(int v): to add integer from the rear if possible else display the message
“Overflow”
int delete(): returns elements from front if present, otherwise displays the message
“Underflow” and returns -9999
void display(): displays the array elements
Specify the class Queue giving details of only the functions void addele(int) and int
delete(). Assume that the other functions have been defined.

The main() function and algorithm need not be written.

import java.util.Scanner;
class Queue{
int que[];
int size;
int front;
int rear;
public Queue(int m){
size = m;
que = new int[size];
front = 0;
rear = 0;
}
public void addele(int v){
if(rear == size)
System.out.println("Overflow");
else{
que[rear++] = v;
System.out.println(v + " PUSHED");
}
}
public int delele(){
if(rear == 0){
System.out.println("Underflow");
return -9999;
}
int v = que[front++];
if(front >= rear){
front = 0;
rear = 0;
}
return v;
}
public void display(){
if(rear == 0)
System.out.println("QUEUE EMPTY");
else{
for(int i = front; i < rear; i++)
System.out.print(que[i] + " ");
System.out.println();
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Queue size: ");
int max = Integer.parseInt(in.nextLine());
if(max > 100)
max = 100;
Queue obj = new Queue(max);
while(true){
System.out.println("1. PUSH ELEMENT");
System.out.println("2. POP ELEMENT");
System.out.println("3. DISPLAY ELEMENTS");
System.out.print("ENTER YOUR CHOICE: ");
int choice = Integer.parseInt(in.nextLine());
switch(choice){
case 1:
System.out.print("Element to be pushed: ");
int v = Integer.parseInt(in.nextLine());
obj.addele(v);
break;
case 2:
v = obj.delele();
if(v != -9999)
System.out.println(v + " POPPED");
break;
case 3:
obj.display();
break;
default:
System.out.println("Bye!");
return;
}
}
}
}

You might also like