Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Writing a Java Program Please include comments and screenshots. Mod-7-Assignment

ID: 3856701 • Letter: W

Question

Writing a Java Program Please include comments and screenshots.
Mod-7-Assignments-Ch20-Lists, Stacks, Queues, etc.-Submit Files Hide Submission Folder Information Submission Folder Mod 7 Assignments-Ch20-Lists, Stacks, Queues,etc Instructions Mod 7 Assignments Chapter 20: Li programming Exercises: 20.11-(Match grouping symbols) a Java program continues various pairs of grouping symbols, such as ists, stacks, Que and Priority Queues .Parentheses: (and) Braces: (and) Brackets: [and) Note that the grouping symbols cannot overlaps. For example. (a(b) is illegal. Write a program to check whether a Java source-code file has correct pairs of grouping symbols

Explanation / Answer

LIST Program..

import java.util.Scanner;

class Node

{

protected int data;

protected Node link;

public Node()

{

link = null;

data = 0;

}

public Node(int d,Node n)

{

data = d;

link = n;

}

  

public void setLink(Node n)

{

link = n;

}

public void setData(int d)

{

data = d;

}

  

public Node getLink()

{

return link;

}

  

public int getData()

{

return data;

}

}

class linkedList

{

protected Node start;

protected Node end ;

public int size ;

  

public linkedList()

{

start = null;

end = null;

size = 0;

}

  

public boolean isEmpty()

{

return start == null;

}

  

public int getSize()

{

return size;

}

  

public void insertAtStart(int val)

{

Node nptr = new Node(val, null);

size++ ;

if(start == null)

{

start = nptr;

end = start;

}

else

{

nptr.setLink(start);

start = nptr;

}

}

  

public void insertAtEnd(int val)

{

Node nptr = new Node(val,null);

size++ ;

if(start == null)

{

start = nptr;

end = start;

}

else

{

end.setLink(nptr);

end = nptr;

}

}

/* Function to insert an element at position */

public void insertAtPos(int val , int pos)

{

Node nptr = new Node(val, null);

Node ptr = start;

pos = pos - 1 ;

for (int i = 1; i < size; i++)

{

if (i == pos)

{

Node tmp = ptr.getLink() ;

ptr.setLink(nptr);

nptr.setLink(tmp);

break;

}

ptr = ptr.getLink();

}

size++ ;

}

  

public void deleteAtPos(int pos)

{

if (pos == 1)

{

start = start.getLink();

size--;

return ;

}

if (pos == size)

{

Node s = start;

Node t = start;

while (s != end)

{

t = s;

s = s.getLink();

}

end = t;

end.setLink(null);

size --;

return;

}

Node ptr = start;

pos = pos - 1 ;

for (int i = 1; i < size - 1; i++)

{

if (i == pos)

{

Node tmp = ptr.getLink();

tmp = tmp.getLink();

ptr.setLink(tmp);

break;

}

ptr = ptr.getLink();

}

size-- ;

}

  

public void display()

{

System.out.print(" Singly Linked List = ");

if (size == 0)

{

System.out.print("empty ");

return;

}

if (start.getLink() == null)

{

System.out.println(start.getData() );

return;

}

Node ptr = start;

System.out.print(start.getData()+ "->");

ptr = start.getLink();

while (ptr.getLink() != null)

{

System.out.print(ptr.getData()+ "->");

ptr = ptr.getLink();

}

System.out.print(ptr.getData()+ " ");

}

}

public class SinglyLinkedList

{

public static void main(String[] args)

{   

Scanner scan = new Scanner(System.in);

  

linkedList list = new linkedList();

System.out.println("Singly Linked List Test ");

char ch;

  

do

{

System.out.println(" Singly Linked List Operations ");

System.out.println("1. insert at begining");

System.out.println("2. insert at end");

System.out.println("3. insert at position");

System.out.println("4. delete at position");

System.out.println("5. check empty");

System.out.println("6. get size");

int choice = scan.nextInt();

switch (choice)

{

case 1 :

System.out.println("Enter integer element to insert");

list.insertAtStart( scan.nextInt() );   

break;

case 2 :

System.out.println("Enter integer element to insert");

list.insertAtEnd( scan.nextInt() );   

break;   

case 3 :

System.out.println("Enter integer element to insert");

int num = scan.nextInt() ;

System.out.println("Enter position");

int pos = scan.nextInt() ;

if (pos <= 1 || pos > list.getSize() )

System.out.println("Invalid position ");

else

list.insertAtPos(num, pos);

break;

case 4 :

System.out.println("Enter position");

int p = scan.nextInt() ;

if (p < 1 || p > list.getSize() )

System.out.println("Invalid position ");

else

list.deleteAtPos(p);

break;

case 5 :

System.out.println("Empty status = "+ list.isEmpty());

break;   

case 6 :

System.out.println("Size = "+ list.getSize() +" ");

break;   

default :

System.out.println("Wrong Entry ");

break;   

}

  

list.display();

System.out.println(" Do you want to continue (Type y or n) ");

ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');   

}

}

STACK Example program

public abstract class myStack<E> implements Stack<E>{

private final E s[];

int t=0;

public myStack() {

this.s = (E[]) new Object[100];

}

@Override

public int size(){

return t;

}

@Override

public boolean isEmpty(){

switch(size()){

case 0:

return true;

}

return false;

}

@Override

public E top() {

if(isEmpty())

throw new EmptyStackException();

return s[t-1];

}

@Override

public void push(E element) {

if(isEmpty())

s[0]= element;

else

s[t]= element;

t++;

}

@Override

public E pop() {

E x;

if(isEmpty())

throw new EmptyStackException();

else{

x = s[t-1];

s[t-1] = null;

t--;

}

return x;

}

}

public class StackDemo {

private static final int capacity = 3;

int arr[] = new int[capacity];

int top = -1;

  

public void push(int pushedElement) {

if (top < capacity - 1) {

top++;

arr[top] = pushedElement;

System.out.println("Element " + pushedElement

+ " is pushed to Stack !");

printElements();

} else {

System.out.println("Stack Overflow !");

}

}

  

public void pop() {

if (top >= 0) {

top--;

System.out.println("Pop operation done !");

} else {

System.out.println("Stack Underflow !");

}

}

  

public void printElements() {

if (top >= 0) {

System.out.println("Elements in stack :");

for (int i = 0; i <= top; i++) {

System.out.println(arr[i]);

}

}

}

  

public static void main(String[] args) {

StackDemo stackDemo = new StackDemo();

  

stackDemo.pop();

stackDemo.push(23);

stackDemo.push(2);

stackDemo.push(73);

stackDemo.push(21);

stackDemo.pop();

stackDemo.pop();

stackDemo.pop();

stackDemo.pop();

}

  

}

Program for QUEUES

package com.java2novice.ds.queue;

public class QueueImpl {

private int capacity;

int queueArr[];

int front = 0;

int rear = -1;

int currentSize = 0;

public QueueImpl(int queueSize){

this.capacity = queueSize;

queueArr = new int[this.capacity];

}

public void enqueue(int item) {

if (isQueueFull()) {

System.out.println("Overflow ! Unable to add element: "+item);

} else {

rear++;

if(rear == capacity-1){

rear = 0;

}

queueArr[rear] = item;

currentSize++;

System.out.println("Element " + item+ " is pushed to Queue !");

}

}

public void dequeue() {

if (isQueueEmpty()) {

System.out.println("Underflow ! Unable to remove element from Queue");

} else {

front++;

if(front == capacity-1){

System.out.println("Pop operation done ! removed: "+queueArr[front-1]);

front = 0;

} else {

System.out.println("Pop operation done ! removed: "+queueArr[front-1]);

}

currentSize--;

}

}

public boolean isQueueFull(){

boolean status = false;

if (currentSize == capacity){

status = true;

}

return status;

}

public boolean isQueueEmpty(){

boolean status = false;

if (currentSize == 0){

status = true;

}

return status;

}

public static void main(String a[]){

QueueImpl queue = new QueueImpl(4);

queue.enqueue(4);

queue.dequeue();

queue.enqueue(56);

queue.enqueue(2);

queue.enqueue(67);

queue.dequeue();

queue.dequeue();

queue.enqueue(24);

queue.dequeue();

queue.enqueue(98);

queue.enqueue(45);

queue.enqueue(23);

queue.enqueue(435);

}

}