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

Subject: Computer Science Programming In Java Topic: Link List Subject: I need s

ID: 3603858 • Letter: S

Question

Subject: Computer Science Programming In Java

Topic: Link List

Subject: I need some help with creating simple link list Programming that does the following:

-Public getElementNumPosition(int Pos)

   -this method gets the element that is at the given position in the linked list program.

   -if no element is at element, return -1;

-Public void AddPositionE(int Pos, E object)

   -adds an object using the the index positioning of the linked list program

-Public void AddingElement(E object)

-adds object to the linked list,

-Public double getPosition(E object)

   -this method will get the position of a given number

-Public E deleteElementPosition(int Pos)

   -this method will delete an element in linked list given a position number.

   -if no number at that position, return a -1;

-Public boolean deleteElem(E object)

   -this method will delete an element chosen if it’s in the linked list (will only delete 1 number if there is duplicates.)

   -if element chosen for deleting is not in linked list, return a -1;

-Public int sizeOfList()

   -returns the number of elements in list


_________________________________________________________________________________________

I currently have this:



Public class LinkL<E>

{

class Node

{

  E data;

  Node next;

  Node(E d)

  {

      data = d;

      next = null;

  }

  Node(E d, Node n)

  {

      data = d;

      next = n;

  }

}

private Node head, tail;

private int size;

public int sizeOfList()

{

  return size;

}

Public getElementNumPosition(int Pos)

{

   //not sure

}

public void AddingElement(E obj)

{

  if(head == null)

      head = tail = new Node(obj);

  else

  {

      tail.next = new Node(obj, null);

      tail = tail.next;

  }

  size++;

}

Public void AddPositionE(int Pos, E object)

{

//not sure yet

}

Public double getPosition(E object)

{

//

}

Public E deleteElementPosition(int Pos)

{

//

}


Public boolean deleteElem(E object)

{

//

}

//This will be my main method to run tests

Public static void main (String[] args)

{

LinkL<Integer> prog = new LinkL<Integer>();

   System.out.println(“This is where I will run my tests”);

   prog.AddingElement(-5);

   prog.AddingElement(19);

   prog.AddingElement(100);

   //This should look add my elements to links

   //should print: -5, 19, 100

System.out.println(prog); //will print elements

   //and so on …

}

}

Elements in link list: 5, 9, 100,3 Getting element at position 0: -5 Adding an element using its object number and position to list: 0, 7 7, -5, 9, 100, 3 Getting position number given an object number: 3 3 is at position 4 Deleting element given a position number: 4 7, -5, 9, 100 Deleting element given the element number: 100 7, -5,9 Returning size of linked list 3

Explanation / Answer

Hi, Please find the below code.

public class Linkl<E> {

class Node {

E data;

Node next;

Node(E d) {

data = d;

next = null;

}

Node(E d, Node n) {

data = d;

next = n;

}

}

private Node head, tail;

private int size;

public int sizeOfList() {

return size;

}

public int getElementNumPosition(int index) {

if(index>=size){

return -1;

}

Node current = goTo(index);

return (int) current.data;

}

// Returns a reference to the node object representing the index'th element

// in the list. Used as a helper by many of the public methods.

private Node goTo(int index) {

Node current = head;

for (int i = 0; i < index; i++) {

current = current.next;

}

return current;

}

public void addingElement(E obj) {

if (head == null)

head = tail = new Node(obj);

else {

tail.next = new Node(obj, null);

tail = tail.next;

}

size++;

}

// Inserts the given value into this list at the specified index.

// Precondition: 0 <= index <= size

// Throws a NullPointerException if index > size.

public void addPositionE(int index, E value) {

if (index == 0) {

// insert at the front

head = new Node(value, head);

size++;

} else {

// insert in middle/end; walk to node before the one to insert

Node current = goTo(index - 1);

Node newNode = new Node(value, current.next);

current.next = newNode;

size++;

// shorter version of the above code

// current.next = new ListNode(value, current.next);

}

}

// Returns the index of the first occurrence of the given value in the list,

// or -1 if the value is not found in the list.

public int getPosition(E value) {

int index = 0;

Node current = head;

while (current != null) {

if (current.data == value) {

return index;

}

index++;

current = current.next;

}

return -1;

}

public void deleteElementPosition(int index) {

if (index == 0) {

// removing from the front

head = head.next;

size--;

} else {

Node current = goTo(index - 1);

current.next = current.next.next; // deletes the node

size--;

}

}

public boolean deleteElem(E value) {

Node current = head;

while (current != null) {

if (current.next.data == value) {

current.next = current.next.next;

size--;

return true;

}

current = current.next;

}

return false;

}

public String toString() {

if (head == null) {

return "[]";

} else {

String result = "[" + head.data;

Node current = head.next;

while (current != null) {

result += ", " + current.data;

current = current.next;

}

result += "]";

return result;

}

}

// This will be my main method to run tests

public static void main(String[] args) {

Linkl<Integer> prog = new Linkl<Integer>();

System.out.println("This is where I will run my tests");

prog.addingElement(-5);

prog.addingElement(19);

prog.addingElement(100);

prog.addingElement(3);

// This should look add my elements to links

// should print: -5, 19, 100

System.out.println("Elements in the link list: "+prog.toString()); // will print elements

System.out.println("Getting element at position 0: "+prog.getElementNumPosition(4));

//Adding element at given position

prog.addPositionE(0,7);

System.out.println("Adding an element using its object number and positon to list:0,7 "+prog.toString());

//Position of element

System.out.println("Getting position number given and object number: 3 is at position "+prog.getPosition(3));

//Delete element at given index

prog.deleteElementPosition(4);

System.out.println("Delete element given a position number 4: "+prog.toString());

//Delete given Element

prog.deleteElem(100);

System.out.println("Delete element given the element number 100: "+prog.toString());

System.out.println("Size of Linked List: "+prog.size);

}

}

Thanks