In: Computer Science
Write a class for a Stack of characters using a linked list implementation.
// Create Stack Using Linked list
class StackUsingLinkedlist {
// A linked list node
private class Node {
char data; // character data
Node link; // reference variavle Node type
}
// create globle top reference variable
Node top;
// Constructor
StackUsingLinkedlist()
{
this.top = null;
}
// Utility function to add an element x in the stack
public void push(char x) // insert at the beginning
{
// create new node temp and allocate memory
Node temp = new Node();
// check if stack (heap) is full. Then inserting an
// element would lead to stack overflow
if (temp == null) {
System.out.print("\nHeap Overflow");
return;
}
// initialize data into temp data field
temp.data = x;
// put top reference into temp link
temp.link = top;
// update top reference
top = temp;
}
// Utility function to check if the stack is empty or not
public boolean isEmpty()
{
return top == null;
}
// Utility function to return top element in a stack
public int peek()
{
// check for empty stack
if (!isEmpty()) {
return top.data;
}
else {
System.out.println("Stack is empty");
return -1;
}
}
// Utility function to pop top element from the stack
public void pop() // remove at the beginning
{
// check for stack underflow
if (top == null) {
System.out.print("\nStack Underflow");
return;
}
// update the top pointer to point to the next node
top = (top).link;
}
public void display()
{
// check for stack underflow
if (top == null) {
System.out.printf("\nStack Underflow");
exit(1);
}
else {
Node temp = top;
while (temp != null) {
// print node data
System.out.printf("%d->", temp.data);
// assign temp link to temp
temp = temp.link;
}
}
}
}
Write a class for a Queue of characters using a linked list implementation.
// A linked list (LL) node to store a queue entry
class QNode {
char key;
QNode next;
// constructor to create a new linked list node
public QNode(char key)
{
this.key = key;
this.next = null;
}
}
class Queue {
QNode front, rear;
public Queue()
{
this.front = this.rear = null;
}
// Method to add an key to the queue.
void enqueue(int key)
{
// Create a new LL node
QNode temp = new QNode(key);
// If queue is empty, then new node is front and rear both
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
// Add the new node at the end of queue and change rear
this.rear.next = temp;
this.rear = temp;
}
// Method to remove an key from queue.
QNode dequeue()
{
// If queue is empty, return NULL.
if (this.front == null)
return null;
// Store previous front and move front one node ahead
QNode temp = this.front;
this.front = this.front.next;
// If front becomes NULL, then change rear also as NULL
if (this.front == null)
this.rear = null;
return temp;
}
}
Write a class for a Queue of integers using a circular array implementation.
class CircularQueue
{
int maxSize;
int rear;
int front;
int aQueue[];
{
rear = -1;
front = -1;
}
CircularQueue(int maxSize)
{
this.maxSize = maxSize;
this.aQueue = new int[maxSize];
}
void enQueue(int item)
{
if(((rear+1) % maxSize) == front)
{
System.out.println("Queue is Full");
}
else
{
if (rear == front && front == -1)
{
front += 1;
}
rear = (rear+1) % maxSize;
aQueue[rear] = item;
}
}
void deQueue()
{
if(rear == front && rear == -1)
{
System.out.println("Queue is Empty.");
}
else
{
int item = aQueue[front];
if(rear == front)
{
rear = -1;
front = -1;
}
else
{
front = (front + 1) % maxSize;
}
System.out.println(item + " is deQueued from the Queue");
}
}
void display()
{
int tmpfront = front;
if(rear == front && rear == -1)
{
System.out.println("Queue is Empty.");
}
else
{
System.out.println("The element"+ elementOrElements() + "on the Queue are:- ");
for(int i=0; i<maxSize ; i++)
{
if(tmpfront != rear)
{
System.out.println(aQueue[tmpfront]);
tmpfront = (tmpfront + 1) % maxSize;
}
else
{
System.out.println(aQueue[rear]);
break;
}
}
}
}
/* PLEASE UPVOTE */