SlideShare a Scribd company logo
-JAVA-
provide a test class that do the required
-you may add methods you need in the SLL-
//************************** SLL.java *********************************
// a generic singly linked list class
public class SLL{
private class SLLNode {
private T info;
private SLLNode next;
public SLLNode() {
this(null,null);
}
public SLLNode(T el) {
this(el,null);
}
public SLLNode(T el, SLLNode ptr) {
info = el; next = ptr;
}
}
protected SLLNode head, tail;
public SLL() {
head = tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void addToHead(T el) {
head = new SLLNode(el,head);
if (tail == null)
tail = head;
}
public void addToTail(T el) {
if (!isEmpty()) {
tail.next = new SLLNode(el);
tail = tail.next;
}
else head = tail = new SLLNode(el);
}
public T deleteFromHead() { // delete the head and return its info;
if (isEmpty())
return null;
T el = head.info;
if (head == tail) // if only one node on the list;
head = tail = null;
else head = head.next;
return el;
}
public T deleteFromTail() { // delete the tail and return its info;
if (isEmpty())
return null;
T el = tail.info;
if (head == tail) // if only one node in the list;
head = tail = null;
else { // if more than one node in the list,
SLLNode tmp; // find the predecessor of tail;
for (tmp = head; tmp.next != tail; tmp = tmp.next);
tail = tmp; // the predecessor of tail becomes tail;
tail.next = null;
}
return el;
}
public void delete(T el) { // delete the node with an element el;
if (!isEmpty())
if (head == tail && el.equals(head.info)) // if only one
head = tail = null; // node on the list;
else if (el.equals(head.info)) // if more than one node on the list;
head = head.next; // and el is in the head node;
else { // if more than one node in the list
SLLNode pred, tmp;// and el is in a nonhead node;
for (pred = head, tmp = head.next;
tmp != null && !tmp.info.equals(el);
pred = pred.next, tmp = tmp.next);
if (tmp != null) { // if el was found;
pred.next = tmp.next;
if (tmp == tail) // if el is in the last node;
tail = pred;
}
}
}
@Override
public String toString() {
if(head == null)
return "[ ]";
String str = "[ ";
SLLNode tmp = head;
while(tmp != null){
str += tmp.info + " ";
tmp = tmp.next;
}
return str+"]";
}
public boolean contains(T el) {
if(head == null)
return false;
SLLNode tmp = head;
while(tmp != null){
if(tmp.info.equals(el))
return true;
tmp = tmp.next;
}
return false;
}
public int size(){
if(head == null)
return 0;
int count = 0;
SLLNode p = head;
while(p != null) {
count++;
p = p.next;
}
return count;
}
//
// Please write the methods of Task02 here:
//
public void insertBefore(int index, T newElem) throws Exception {
//throws exception if the list is empty
if(isEmpty()){
throw new Exception("list is empty");
}
//inserts
if(index < size()){
if (index == 0){
SLLNode newnode = new SLLNode<>(newElem, head);
head = newnode;
}
else{
SLLNode temp = head;
SLLNode newnode = new SLLNode<>(newElem, null);
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
newnode.next = temp.next;
temp.next = newnode;
}
}
//throws exception for invalid index
else {
throw new Exception("index is not valid");
}
}
public T delete(int index) throws Exception{
//throws exception if the list is empty
if (isEmpty()) {
throw new Exception("list is empty");
}
//removes
if (index < size()) {
if (index == 0) {
return deleteFromHead();
}
else if (index == size() - 1) {
return deleteFromTail();
}
SLLNode temp = head;
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
T elm = temp.next.info;
temp.next = temp.next.next;
return elm;
}
//invalid index
throw new Exception("index is not valid");
}
public void insertAfterSecondOccurrence(T e1, T e2) throws Exception {
//throws exception if the list is empty
if (isEmpty()) {
throw new Exception("list is empty");
}
//inserts
int pos = -1;
int counter = 0;
SLLNode temp = head;
for (int i = 0; i < size(); i++) {
if (temp.info == e2) {
counter++;
}
if (counter == 2) {
pos = i;
break;
}
temp = temp.next;
}
if (pos != -1) {
if (pos + 1 == size()) {
addToTail(e1);
} else {
insertBefore(pos + 1, e1);
}
}
//the list has no second occurrence of e2
if (pos == -1){
throw new Exception("list has no second occurrence of " + e2);
}
}
} Part I: Programming (50 points) Use the singly linked list class introduced in the lab to
implement integers of unlimited size. Each node of the list should store one digit of the integer.
You are to implement the addition, subtraction and multiplication operations. Please note that
any additional data structure to be used in solving this problem has to be taken from the ones
introduced in the ICS 202 lab. In addition, provide a test class that will do the following: It will
keep asking the user to enter two integers, choose one of the operations (addition, subtraction,
multiplication) and then display the result. The program will stop upon entering the "#"
character (without the double quotes).

More Related Content

PDF
Using Java programming language solve the followingSLL CLASS.pdf
PDF
Solve using Java programming language- ----------------------------.pdf
PDF
Submit1) Java Files2) Doc file with the following contents.pdf
PDF
Solve using java and using this Singly linked list classpublic cl.pdf
PDF
Use the singly linked list class introduced in the lab to implement .pdf
PDF
public class SLLT { protected SLLNodeT head, tail; pub.pdf
DOCX
Linked lists
PDF
Help explain the code with line comments public class CompletedLis.pdf
Using Java programming language solve the followingSLL CLASS.pdf
Solve using Java programming language- ----------------------------.pdf
Submit1) Java Files2) Doc file with the following contents.pdf
Solve using java and using this Singly linked list classpublic cl.pdf
Use the singly linked list class introduced in the lab to implement .pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
Linked lists
Help explain the code with line comments public class CompletedLis.pdf

Similar to -JAVA-provide a test class that do the required -you may add met.pdf (20)

PDF
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
PDF
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
PDF
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
PDF
mainpublic class AssignmentThree {    public static void ma.pdf
PDF
please help me in C++Objective Create a singly linked list of num.pdf
PDF
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
PDF
#include sstream #include linkylist.h #include iostream.pdf
PPT
PDF
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
PDF
Data Structures in C++I am really new to C++, so links are really .pdf
PDF
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
PDF
#includeiostream struct node {    char value;    struct no.pdf
PDF
137 Lab-2.2.pdf
PPTX
DSA(1).pptx
DOCX
Linked Stack program.docx
PPTX
linked list.pptx
PPTX
Ll.pptx
PDF
The LinkedList1 class implements a Linked list. class.pdf
PDF
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
please help me in C++Objective Create a singly linked list of num.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
#include sstream #include linkylist.h #include iostream.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
Data Structures in C++I am really new to C++, so links are really .pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
#includeiostream struct node {    char value;    struct no.pdf
137 Lab-2.2.pdf
DSA(1).pptx
Linked Stack program.docx
linked list.pptx
Ll.pptx
The LinkedList1 class implements a Linked list. class.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Ad

More from alphawheels007 (20)

PDF
1) �Cu�l de las AD es falsa con respecto a las infecciones por virus.pdf
PDF
1) La selecci�n sexual elige el fenotipo que le dar� a la descendenc.pdf
PDF
1) Which of the following terms is not associated with price discrim.pdf
PDF
1) What is the role of the senior leaders in the hospital How shoul.pdf
PDF
1) What percentage of all sampled fish have a flattened body shape A.pdf
PDF
1) Use Link State algorithm to determine the shortest path from node.pdf
PDF
1) Un �rea fue destruida recientemente por un furioso incendio fores.pdf
PDF
1) Statements detailing a companys expectations for its employees .pdf
PDF
1) Select the correct order for the steps of corporate social respon.pdf
PDF
1) Las especies con tiempos de generaci�n m�s largos deben tener .pdf
PDF
1) Seg�n Henri Fayol, los planes efectivos deben tener las cualidade.pdf
PDF
1) Run the systemctl status httpd command to show that your web se.pdf
PDF
1) La siguiente oraci�n tiene errores, podr�a ser prolija, entrecort.pdf
PDF
1) In the study of an outbreak of an infectious disease, plotting an.pdf
PDF
1) Install and run an Apache secure web server on your Linux OS. 2) .pdf
PDF
1) La expresi�n g�nica diferencial ocurre en cada una de las siguien.pdf
PDF
1) El objetivo del inventario de seguridad es a. reemplace las uni.pdf
PDF
1) Explicar qu� es la gesti�n de la capacidad y por qu� es estrat�gi.pdf
PDF
1) En una poblaci�n que est� en equilibrio de Hardy-Weinberg, hay do.pdf
PDF
1) Eres un estudiante de MPH en pr�cticas en una iglesia local. Part.pdf
1) �Cu�l de las AD es falsa con respecto a las infecciones por virus.pdf
1) La selecci�n sexual elige el fenotipo que le dar� a la descendenc.pdf
1) Which of the following terms is not associated with price discrim.pdf
1) What is the role of the senior leaders in the hospital How shoul.pdf
1) What percentage of all sampled fish have a flattened body shape A.pdf
1) Use Link State algorithm to determine the shortest path from node.pdf
1) Un �rea fue destruida recientemente por un furioso incendio fores.pdf
1) Statements detailing a companys expectations for its employees .pdf
1) Select the correct order for the steps of corporate social respon.pdf
1) Las especies con tiempos de generaci�n m�s largos deben tener .pdf
1) Seg�n Henri Fayol, los planes efectivos deben tener las cualidade.pdf
1) Run the systemctl status httpd command to show that your web se.pdf
1) La siguiente oraci�n tiene errores, podr�a ser prolija, entrecort.pdf
1) In the study of an outbreak of an infectious disease, plotting an.pdf
1) Install and run an Apache secure web server on your Linux OS. 2) .pdf
1) La expresi�n g�nica diferencial ocurre en cada una de las siguien.pdf
1) El objetivo del inventario de seguridad es a. reemplace las uni.pdf
1) Explicar qu� es la gesti�n de la capacidad y por qu� es estrat�gi.pdf
1) En una poblaci�n que est� en equilibrio de Hardy-Weinberg, hay do.pdf
1) Eres un estudiante de MPH en pr�cticas en una iglesia local. Part.pdf
Ad

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Cell Types and Its function , kingdom of life
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial disease of the cardiovascular and lymphatic systems
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Abdominal Access Techniques with Prof. Dr. R K Mishra
A systematic review of self-coping strategies used by university students to ...
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
GDM (1) (1).pptx small presentation for students
Computing-Curriculum for Schools in Ghana
Cell Types and Its function , kingdom of life
202450812 BayCHI UCSC-SV 20250812 v17.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?

-JAVA-provide a test class that do the required -you may add met.pdf

  • 1. -JAVA- provide a test class that do the required -you may add methods you need in the SLL- //************************** SLL.java ********************************* // a generic singly linked list class public class SLL{ private class SLLNode { private T info; private SLLNode next; public SLLNode() { this(null,null); } public SLLNode(T el) { this(el,null); } public SLLNode(T el, SLLNode ptr) { info = el; next = ptr; } } protected SLLNode head, tail; public SLL() { head = tail = null; } public boolean isEmpty() { return head == null; } public void addToHead(T el) { head = new SLLNode(el,head); if (tail == null) tail = head; } public void addToTail(T el) { if (!isEmpty()) { tail.next = new SLLNode(el); tail = tail.next;
  • 2. } else head = tail = new SLLNode(el); } public T deleteFromHead() { // delete the head and return its info; if (isEmpty()) return null; T el = head.info; if (head == tail) // if only one node on the list; head = tail = null; else head = head.next; return el; } public T deleteFromTail() { // delete the tail and return its info; if (isEmpty()) return null; T el = tail.info; if (head == tail) // if only one node in the list; head = tail = null; else { // if more than one node in the list, SLLNode tmp; // find the predecessor of tail; for (tmp = head; tmp.next != tail; tmp = tmp.next); tail = tmp; // the predecessor of tail becomes tail; tail.next = null; } return el; } public void delete(T el) { // delete the node with an element el; if (!isEmpty()) if (head == tail && el.equals(head.info)) // if only one head = tail = null; // node on the list; else if (el.equals(head.info)) // if more than one node on the list; head = head.next; // and el is in the head node; else { // if more than one node in the list SLLNode pred, tmp;// and el is in a nonhead node; for (pred = head, tmp = head.next; tmp != null && !tmp.info.equals(el);
  • 3. pred = pred.next, tmp = tmp.next); if (tmp != null) { // if el was found; pred.next = tmp.next; if (tmp == tail) // if el is in the last node; tail = pred; } } } @Override public String toString() { if(head == null) return "[ ]"; String str = "[ "; SLLNode tmp = head; while(tmp != null){ str += tmp.info + " "; tmp = tmp.next; } return str+"]"; } public boolean contains(T el) { if(head == null) return false; SLLNode tmp = head; while(tmp != null){ if(tmp.info.equals(el)) return true; tmp = tmp.next; } return false; } public int size(){ if(head == null)
  • 4. return 0; int count = 0; SLLNode p = head; while(p != null) { count++; p = p.next; } return count; } // // Please write the methods of Task02 here: // public void insertBefore(int index, T newElem) throws Exception { //throws exception if the list is empty if(isEmpty()){ throw new Exception("list is empty"); } //inserts if(index < size()){ if (index == 0){ SLLNode newnode = new SLLNode<>(newElem, head); head = newnode; } else{ SLLNode temp = head; SLLNode newnode = new SLLNode<>(newElem, null); for (int i = 0; i < index - 1; i++) { temp = temp.next; } newnode.next = temp.next; temp.next = newnode; }
  • 5. } //throws exception for invalid index else { throw new Exception("index is not valid"); } } public T delete(int index) throws Exception{ //throws exception if the list is empty if (isEmpty()) { throw new Exception("list is empty"); } //removes if (index < size()) { if (index == 0) { return deleteFromHead(); } else if (index == size() - 1) { return deleteFromTail(); } SLLNode temp = head; for (int i = 0; i < index - 1; i++) { temp = temp.next; } T elm = temp.next.info; temp.next = temp.next.next; return elm; } //invalid index throw new Exception("index is not valid"); } public void insertAfterSecondOccurrence(T e1, T e2) throws Exception {
  • 6. //throws exception if the list is empty if (isEmpty()) { throw new Exception("list is empty"); } //inserts int pos = -1; int counter = 0; SLLNode temp = head; for (int i = 0; i < size(); i++) { if (temp.info == e2) { counter++; } if (counter == 2) { pos = i; break; } temp = temp.next; } if (pos != -1) { if (pos + 1 == size()) { addToTail(e1); } else { insertBefore(pos + 1, e1); } } //the list has no second occurrence of e2 if (pos == -1){ throw new Exception("list has no second occurrence of " + e2); } } } Part I: Programming (50 points) Use the singly linked list class introduced in the lab to implement integers of unlimited size. Each node of the list should store one digit of the integer. You are to implement the addition, subtraction and multiplication operations. Please note that any additional data structure to be used in solving this problem has to be taken from the ones introduced in the ICS 202 lab. In addition, provide a test class that will do the following: It will
  • 7. keep asking the user to enter two integers, choose one of the operations (addition, subtraction, multiplication) and then display the result. The program will stop upon entering the "#" character (without the double quotes).