Question: 3. Fill in the code to perform the tasks specified by the comments in the SListTester.java class. (1 point each) a. Create an Object of

3. Fill in the code to perform the tasks specified by the comments in the SListTester.java class. (1 point each)

a. Create an Object of the SList class.

b. Insert the following integer numbers in the list using the addFirst method.

{2, 3, 4, 7, 9}

c. Print the elements in the linked list.

d. Insert the following integer numbers in the list using the addLast method.

{15, 7, 12, 8, 10}

e. Print the elements in the linked list.

f. Remove the first element from your linked list.

g. Print the elements in the linked list.

h. Print the number of elements in the linked list (size).

i. Print the index (position) of the first occurrence of (7) using the findIndex method from 1.a

j. Print the index (position) of the first occurrence of (25) using the findIndex method from 1.a

k. Remove the last element from your linked list.

l. Print the elements in the linked list.

SList.java :

public class SList implements LinkedList { private Node head; private int size;

public SList() { head = null; size = 0; }

public int getSize() { return size; }

public boolean isEmpty() { if (size == 0) return true; return false; }

public void addFirst(int n) { Node newNode = new Node(n, null); newNode.setNext(head); head = newNode; size++; }

public void addLast(int n) { Node newNode = new Node(n, null); Node last = head;

while (last.getNext() != null) { last = last.getNext(); } last.setNext(newNode); size++; }

public int removeFirst() { Node x = head; if(head != null) { head = head.getNext(); size--; } return x.getElement(); }

public int findIndex(int num) { int nIndex = 0; Node nd = head; while(nd != null) { if(num == nd.getElement()) { return nIndex; } nd = nd.getNext(); nIndex++; } return -1; // Element is not present in the linkedlist }

public int removeLast(int n); { Node nd = head; Node previousNode = head; Node tail = head; // gets the last node while(tail.getNext() != null) { tail = tail.getNext(); } // gets last element int nlastElement = tail.getElement(); //gets second to last node while(previousNode.getNext() != tail) { nd = nd.getNext(); } // sets the tail as the second to last node and decreases size tail = previousNode.getNext(); size--; return nlastElement; }

public String toString() { if (!isEmpty()) { String s = "["; Node x = head; while (x != null) { s += x.getElement(); if(x.getNext() != null) s += ", "; x = x.getNext(); } s += "]"; return s; } return "Sorry, the list is empty"; } }

SListTester.java :

public class SListTester

{

public static void main (String [] args)

{

//Create an Object of the SList class

//Insert the following elements in the list using addFirst

// 2, 3, 4, 7, 9

//Print the elements in the linked list

//Insert the following elements in the list using addLast

// 15, 7, 12, 8, 10

//Print the elements in the linked list

//Remove the first element from your linked list

//Print the elements in the linked list

//Print the number of elements in the linked list (Size)

//Print the index (position) of the first occurrence of (7) using the findIndex method

//Print the index (position) of the first occurrence of (25) using the findIndex method

//Remove the last element from your linked list

//Print the elements in the linked list

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!