Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please modify the program (Add/Remove any method) from the existing program: /* Class Node */ class Node { protected int data; protected Node link; /*

Please modify the program (Add/Remove any method) from the existing program:

/* Class Node */

class Node

{

protected int data;

protected Node link;

/* Constructor */

public Node()

{

link = null;

data = 0;

}

/* Constructor */

public Node(int d,Node n)

{

data = d;

link = n;

}

/* Function to set link to next Node */

public void setLink(Node n)

{

link = n;

}

/* Function to set data to current Node */

public void setData(int d)

{

data = d;

}

/* Function to get link to next node */

public Node getLink()

{

return link;

}

/* Function to get data from current Node

*/

public int getData()

{

return data;

}

}

/* Class linkedList */

class linkedList

{

protected Node start;

protected Node end ;

public int size ;

/* Constructor */

public linkedList()

{

start = null;

end = null;

size = 0;

}

/* Function to check if list is empty */

public boolean isEmpty()

{

return start == null;

}

/* Function to get size of list */

public int getSize()

{

return size;

}

/* Function to insert an element at

beginning */

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;

}

}

/* Function to insert an element at end */

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

{

if (i == pos)

{

Node tmp = ptr.getLink() ;

ptr.setLink(nptr);

nptr.setLink(tmp);

break;

}

ptr = ptr.getLink();

}

size++ ;

}

/* Function to delete an element at

position */

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

{

if (i == pos)

{

Node tmp = ptr.getLink();

tmp = tmp.getLink();

ptr.setLink(tmp);

break;

}

ptr = ptr.getLink();

}

size-- ;

}

/* Function to display elements */

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()+ " ");

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

image text in transcribedimage text in transcribed

J SinglyLinkedList.java 1 finkedList.java > Zs SinglyLinkedList > main(String) lass SinglyLinkedList / t java. util.scanner; - class SinglyLinkedList un | Debug dblic static void main(String[] args) Scanner scan = new Scanner (System.in); / creating object of class linkedList / linkedList list = new linkedList () ; System.out.println(x: "Singly Linked List Test n ); char ch; / Perform list operations / do \{ System.out.println(x: " Singly Linked List Operations "); System.out.println( x: " 1 . insert at begining"); System.out.println(x: "2. insert at end"); System.out.println(x: "3. insert at position"); System.out.println(x: "4. delete at position"); System.out.println(x: "5. check empty"); System.out.println(x: "6. get size"); int choice = scan. nextInt () ; switch (choice) \{ case 1 : System.out.println(x: "Enter integer element to insert"); list.insertAtStart ( scan. nextInt() ); break; case 2 : System.out.println(x: "Enter integer element to insert"); list.insertAtEnd( scan. nextInt() ); break; case 3 : System.out.println(x: "Enter integer element to insert"); int num = scan. nextInt( ); System.out.println(x: "Enter position"); int pos = scan. nextInt(); if (pos list.getsize ( ) ) System.out. println(x: "Invalid position ); else list.insertAtPos(num, pos); break; case 4 : System.out.println(x: "Enter position"); int p= scan. nextInt () ; Java >J SinglyLinkedList.java > \&s singlyLinkedList > main(String[)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

a cognitive reaction to the anticipation of future misfortune.

Answered: 1 week ago

Question

Determine miller indices of plane A Z a/2 X a/2 a/2 Y

Answered: 1 week ago