Question

In: Computer Science

JAVA Write a class for a Stack of characters using a linked list implementation. Write a...

  • JAVA
  • Write a class for a Stack of characters using a linked list implementation.
  • Write a class for a Queue of characters using a linked list implementation.
  • Write a class for a Queue of integers using a circular array implementation.

Solutions

Expert Solution

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 */


Related Solutions

write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
(a) Write a stack class that is based on a linked list. It can be just...
(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing. (b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing. (c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks, fix the race condition. For reference, see Section 7.3.1 of SGG book.(Section 7.3.1 is about mutex and semaphores it does explain how to implement I'm just having a hard time finding the race condition within the code) /* * Stack containing race conditions */ #include #include #include typedef int value_t; //...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in an array. NOT in linked List. Implement an array-based Linked List in your language. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE...
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT