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

Seeking comments for this program that would explain each methods implementation

ID: 3859634 • Letter: S

Question

Seeking comments for this program that would explain each methods implementation. See program below.

//import packages

import java.util.Scanner;

//define class

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);

   }

}

Explanation / Answer

//import packages
import java.util.Scanner;

//define class
class Node

{

protected int data; //declaring variable

protected Node link;

public Node() //non parameterised constructor

{

link = null;

data = 0;

}

public Node(int d,Node n) //parameterised constructor

{

data = d;

link = n;

}

  

public void setLink(Node n) //setlink Function have a object of the class as parameter

{

link = n;

}

public void setData(int d) //setData function having one parameter of int type

{

data = d;

}

  

public Node getLink() //getLink function have class as return type

{

return link;

}

  

public int getData() //getdata function of int as return type.

{

return data;

}

}

class linkedList //linkedlist class

{

protected Node start; //variable of node class type

protected Node end ;

public int size ;

  

public linkedList() //non para constructor

{

start = null;

end = null;

size = 0;

}

  

public boolean isEmpty() // isEmpty function of boolean return type

{

return start == null;

}

  

public int getSize() //getSize function of int as return type

{

return size;

}

  

public void insertAtStart(int val) //insertAtStart function having one int parameter

{

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) //insertAtEnd function having one int parameter

{

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) //insertAtPos function
//having 2 parameters where val is the data and pos is the position
{

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) //deleteAtPos function having one int parameter
// pos stores the position where the value is to be //deleted
{

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() //display function to print

{

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 //SinglyLinkedList class

{

public static void main(String[] args)

{   

Scanner scan = new Scanner(System.in);



linkedList list = new linkedList(); //creating object of the linkedList class

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

char ch;



do
  
{ //creating the menu for the User
  
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 : //calling insertAtStart function
  
System.out.println("Enter integer element to insert");
  
list.insertAtStart( scan.nextInt() );
  
break;

case 2 : //calling insertAtEnd function
  
System.out.println("Enter integer element to insert");
  
list.insertAtEnd( scan.nextInt() );   
  
break;   

case 3 : //calling insertAtPos function
  
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 : //calling deleteAtPos function
  
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 : //status of list
  
System.out.println("Empty status = "+ list.isEmpty());
  
break;   

case 6 : //size of the list
  
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() { //constructor of myStack

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

}

@Override

public int size(){ //size function Override the base function

return t;

}

@Override

public boolean isEmpty(){ //isEmpty function Override the base function

switch(size()){
  
case 0:

return true;
  
}

return false;

}

@Override

public E top() { //top function

if(isEmpty())
  
throw new EmptyStackException();

return s[t-1];

}

@Override

public void push(E element) { //push function

if(isEmpty())
  
s[0]= element;
  
else
  
s[t]= element;

t++;

}

@Override

public E pop() { //pop function

E x;

if(isEmpty())
  
throw new EmptyStackException();
  
else{
  
x = s[t-1];
  
s[t-1] = null;
  
t--;
  
}

return x;

}

}

public class StackDemo { //StackDemo class

private static final int capacity = 3;

int arr[] = new int[capacity];

int top = -1;

  

public void push(int pushedElement) { //push function

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() { //pop function

if (top >= 0) {
  
top--;
  
System.out.println("Pop operation done !");
  
}
else {
  
System.out.println("Stack Underflow !");
  
}

}

  

public void printElements() { //printElements function

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) { //main function

StackDemo stackDemo = new StackDemo(); //object of StackDemo class
  

stackDemo.pop(); //calling the function with the help of obejct

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 { //QueueImpl function

private int capacity;

int queueArr[];

int front = 0;

int rear = -1;

int currentSize = 0;

public QueueImpl(int queueSize){ //parameterised constructor

this.capacity = queueSize;

queueArr = new int[this.capacity];

}

public void enqueue(int item) { //enqueue function have one int as parameter

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() { //dequeue function

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(){ //isQueueFull function

boolean status = false;

if (currentSize == capacity){
  
status = true;
  
}

return status;

}

public boolean isQueueEmpty(){ //isQueueEmpty function

boolean status = false;

if (currentSize == 0){
  
status = true;
  
}

return status;

}

public static void main(String a[]){ //main function

QueueImpl queue = new QueueImpl(4); //creating object of QueueImpl class

queue.enqueue(4); //calling the function with the help of function

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);

}

}