I need to create a singly linked list derived from a UML sheet that has a very s
ID: 3682472 • Letter: I
Question
I need to create a singly linked list derived from a UML sheet that has a very specific layout i need to follow. Here's the skeleton ive gotten so far, i need help, please.
import java.util.*;
import java.io.*;
public class SLinkList
{
private class SNode
{
private SNode next;
private int data;
}//end class SNode
private SNode head, tail, newNode, current;
private int size;
public void insert(int i)
}
public void delete(int i)
{
}
public boolean isEmpty()
}
public int getSize()
{
}
public void print()
{
}
}
my problem is i have no idea where to start adding in my parameters here's the assignment i was givin:
CSC 161 Single-Linked List Implementation Given the following Class Diagram: Write the code that defines the class. Write the code for an application program that creates and tests an object of this class type Build a menu with calls to each of the methods of the object. (Use the following variable/method/parameter names.) SLinkList AVA SNode next : SNode data : int : SNode : SNode head tail newNode SNode current: SNode slze : int + insert( 1 : 1nt ) : void |insert method must build an ascending ordered list delete ( 1 : int ) : void + isEmpty() : boolean + getSize( ) : int + print( ) : void delete method deletes the first occurrence of i from the sorted list, or prints "Value not Found" when required Print "Empty List" when the print method is called when the List is empty Assian Meat #1 in e tonstantute an object to test 'ecth to instantiate li ke list. OUwin« selections* (ae and C)qurt and Maln sun InkeExplanation / Answer
SinglyLinkedList_Sorted.java
/*
* Java Program to Implement Sorted Singly Linked List
*/
import java.util.Scanner;
/* 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 SingleLinkedList
{
protected Node data;
public int size;
public SingleLinkedList()
{
data=null;
size = 0;
}
/* Function to check size of list */
public int getSize()
{
return size;
}
/* Function to insert an element */
public void insert(int val)
{
Node nptr, ptr, tmp = null;
nptr = new Node(val, null);
boolean ins = false;
if (data == null)
data = nptr;
else if (val <= data.getData())
{
nptr.setLink(data);
data = nptr;
}
else
{
tmp = data;
ptr = data.getLink();
while(ptr != null)
{
if (val >= tmp.getData() && val <= ptr.getData())
{
tmp.setLink(nptr);
nptr.setLink(ptr);
ins = true;
break;
}
else
{
tmp = ptr;
ptr = ptr.getLink();
}
}
if (ins == false)
{
tmp.setLink(nptr);
}
}
size++;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
data = data.getLink();
size--;
return ;
}
if (pos == size)
{
Node ptr = data;
for (int i = 1; i < size - 1; i++)
ptr = ptr.getLink();
ptr.setLink(null);
size --;
return;
}
Node ptr = data;
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-- ;
}
/* Function to display elements */
public int display()
{
System.out.print("Sorted Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return 0;
}
if (data.getLink() == null)
{
System.out.println(data.getData() );
return data.getData();
}
Node ptr = data;
System.out.print(data.getData()+ "->");
ptr = data.getLink();
while (ptr.getLink() != null) {
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ " ");
return data.getData();
}
}
public class SinglyLinkedList_Sorted
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of SingleLinkedList */
SingleLinkedList list = new SingleLinkedList();
System.out.println("Sorted Singly Linked List Test ");
char ch;
/* Perform list operations */
do
{
System.out.println(" Sorted Singly Linked List Operations ");
System.out.println("1. Add");
System.out.println("2. Delete");
System.out.println("3.Print");
System.out.println("4.Quit");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insert( scan.nextInt() );
break;
case 2 :
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 3 :
System.out.println("Display element in ascending order = "+ list.display());
break;
case 4 :
System.exit(1);
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* Display List */
list.display();
System.out.println(" Do you want to continue (Type S or s to continue or press any key to QUIT) ");
ch = scan.next().charAt(0);
} while (ch == 'S'|| ch == 's');
}
}
output
run:
Sorted Singly Linked List Test
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
1
Enter integer element to insert
12
Sorted Singly Linked List = 12
Do you want to continue (Type S or s to continue or press any key to QUIT)
s
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
1
Enter integer element to insert
23
Sorted Singly Linked List = 12->23
Do you want to continue (Type S or s to continue or press any key to QUIT)
s
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
1
Enter integer element to insert
54
Sorted Singly Linked List = 12->23->54
Do you want to continue (Type S or s to continue or press any key to QUIT)
s
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
1
Enter integer element to insert
1
Sorted Singly Linked List = 1->12->23->54
Do you want to continue (Type S or s to continue or press any key to QUIT)
s
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
3
Sorted Singly Linked List = 1->12->23->54
Display element in ascending order = 1
Sorted Singly Linked List = 1->12->23->54
Do you want to continue (Type S or s to continue or press any key to QUIT)
s
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
2
Enter position
1
Sorted Singly Linked List = 12->23->54
Do you want to continue (Type S or s to continue or press any key to QUIT)
s
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
1
Enter integer element to insert
23
Sorted Singly Linked List = 12->23->23->54
Do you want to continue (Type S or s to continue or press any key to QUIT)
s
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
2
Enter position
3
Sorted Singly Linked List = 12->23->54
Do you want to continue (Type S or s to continue or press any key to QUIT)
s
Sorted Singly Linked List Operations
1. Add
2. Delete
3.Print
4.Quit
3
Sorted Singly Linked List = 12->23->54
Display element in ascending order = 12
Sorted Singly Linked List = 12->23->54
Do you want to continue (Type S or s to continue or press any key to QUIT)
b
BUILD SUCCESSFUL (total time: 1 minute 2 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.