I am very new to Java and have been assigned to implement a few methods:
1. add (T newValue) ---> add an entry to a sorted list requires that you find the right position the new entry belongs (ascending order).
ex:
SortedListClass mylist = new SortedListClass();
mylist.add(7);
mylist.add(2);
mylist.add(5);
The list should by in this order:
2 | 5 | 7 |
2. remove( T target) ---> To be able to remove a particular entry you need to find it first.
3. Print the items in the reverse order.
So far my code looks like this:
public class SortedListClass <T> implements ListClassInterface<T>{
private ListNode head;
private int size;
public void ListClass(){
head = null;
size = 0;
}
}
public class ListNode <T>{
private T item;
private ListNode<T> next;
ListNode(T item, ListNode next ){
this.item = item;
this.next = next;
}
ListNode(T item){
this(item, null);
}
public void setItem(T item){
this.item = item;
}
public T getItem(){
return item;
}
public void setNext(ListNode n){
next = n;
}
public ListNode getNext(){
return next;
}
}
You can create the Sorted Linked list by following these steps
package linkedList;
/* 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 {
protected Node start;
public int size;
public linkedList() {
start = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty() {
return start == null;
}
/* 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 (start == null)
start = nptr;
else if (val <= start.getData()) {
nptr.setLink(start);
start = nptr;
} else {
tmp = start;
ptr = start.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) {
start = start.getLink();
size--;
return;
}
if (pos == size) {
Node ptr = start;
for (int i = 1; i < size - 1; i++)
ptr = ptr.getLink();
ptr.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--;
}
/* Function to display elements */
public void display() {
System.out.println("Sorted Singly Linked List = ");
if (size == 0) {
System.out.print("empty\n");
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() + "\n");
}
public void printReverse() {
if (start == null) {
System.out.println("null");
} else {
printReverseRecursive(start);
}
}
private void printReverseRecursive(Node node) {
if (node == null) {
return;
}
printReverseRecursive(node.link);
System.out.print(node.getData() + " ");
}
}
?
package linkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
linkedList list = new linkedList();
System.out.println("Sorted Singly Linked List Test\n");
char ch;
/* Perform list operations */
do {
System.out.println("\nSorted Singly Linked List Operations\n");
System.out.println("1. insert");
System.out.println("2. delete at position");
System.out.println("3. check empty");
System.out.println("4. get size");
System.out.println("5: Print in reverse");
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\n");
else
list.deleteAtPos(p);
break;
case 3:
System.out.println("Empty status = " + list.isEmpty() + "\n");
break;
case 4:
System.out.println("Size = " + list.getSize() + " \n");
break;
case 5:
list.printReverse();
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
/* Display List */
list.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y' || ch == 'y');
}
}?
When I executed the above Program in my Java IDE(Eclipse) and got the following series of output, pasting images in order
That's it, You are done, please upvote and mark it as the answer, if it helps, thanks
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly