Question: a. Provide an implementation for a findIndex method in the SList.java class public int findIndex (int elem) that returns the position of a specific element
a. Provide an implementation for a findIndex method in the SList.java class
public int findIndex (int elem)
that returns the position of a specific element if it is a member in the list, otherwise it will return (-1).
b. Provide an implementation for the removeLast method in the SList.java class
public int removeLast ()
that allows the user to remove and return the last element 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 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"; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
