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

Overview For this project you will implement a basic linked and a sorted linked

ID: 3672361 • Letter: O

Question

Overview

For this project you will implement a basic linked and a sorted linked list. Notice that no public tests are part of this project as all tests are either release or secret. You must implement this project by yourself.

Objectives

This project will allow you practice linked lists and testing.

Grading

(20%) Release Tests

(70%) Secret Tests

(10%) Style

Code Distribution

The project's code distribution is available by checking out the project named LinkedListsProject. The code distribution provides you with the following:

listClasses package Where classes implementing linked lists will appear.

tests package Where you should place your student tests.

Specifications

You are expected to implement two classes: BasicLinkedList and SortedLinkedList.The javadoc describing what you need to implement can be found at Project Javadoc.

The linked list in our project has a head and tail reference. The next link of the last node points to null.

Methods addToEnd(), getLast(), retrieveLastElement() rely (and must use) the tail reference.

The head and tail must be set correctly when list operations update the list.

Your code must be efficient. For example, you will lose credit if you perform an unnecessary list traversal(s) during the implementation of a method.

If you see in the submit server the error "missing_component", the problem is that you are missing a required class. You can also get this error if you are not defining a generic class correctly. For example, your SortedLinkedList class is not being defined as expected. It is recommended that you try submitting your project immediately (if you have not done so yet) so you can identify this problem (your code may work in Eclipse but not in the submit server).

Requirements

You may not use recursion in this project.

For this project you are not required to write student tests, but you are strongly encourage to do so. If you have a problem with your code and need assistance during office hours, you need to have a student test(s) that illustrates the problem you are experiencing. See Student Tests for information regarding the implementation of student tests.

The iterator you define may NOT be used to implement other list operations (e.g., add). The iterator is for users that want to iterate over the list.

You need to implement a singly-linked list that relies on a head and tail reference.

See Style Guidelines for information regarding style.

Sample Driver

The following driver and associated output provide an idea of the classes you need to implement.

Explanation / Answer

import java.util.Scanner;

class nodddd
{
protected int data;
protected nodddd link;

public nodddd()
{
link = null;
data = 0;
}
public nodddd(int d,nodddd n)
{
data = d;
link = n;
}
public void setLink(nodddd n)
{
link = n;
}
public void setData(int d)
{
data = d;
}
public nodddd getLink()
{
return link;
}
public int getData()
{
return data;
}
}

/* Class linkedList */
class linkedList
{
protected nodddd start;
protected nodddd 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 begining */
public void insertAtStart(int val)
{
nodddd nptr = new nodddd(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)
{
nodddd nptr = new nodddd(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)
{
nodddd nptr = new nodddd(val, null);
nodddd ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
nodddd 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)
{
nodddd s = start;
nodddd t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
nodddd ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
nodddd 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;
}
nodddd 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 LinkedList
{
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.Beginign insert");
System.out.println("2.Ending insert");
System.out.println("3.Position insert");
System.out.println("4. Delete at position");
System.out.println("5. IsEmpty check");
System.out.println("6.To know 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;   
}
/* Display List */
list.display();
System.out.println(" Want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');   
}
}