SlideShare a Scribd company logo
Submit:
1) Java Files
2) Doc file with the following contents:
//************************** 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;
}
}
public class SLL_Driver {
public static void main(String[] args) {
SLL myList = new SLL();
String[] cityNames = {"Jubail", "Riyadh", "Abha", "Dammam", "Taif"};
for(int i = 0; i < cityNames.length; i++)
myList.addToHead(cityNames[i]);
System.out.println("The list is: " + myList);
System.out.println("It is " + myList.contains("Dammam") + " that the list contains
Dammam.");
System.out.println("It is " + myList.contains("Jeddah") + " that the list contains Jeddah.");
}
}
// Java program to implement a queue using an array
public class QueueAsArray {
private int front, rear, capacity;
private T[] queue;
public QueueAsArray(int capacity) {
front = rear = -1;
this.capacity = capacity;
queue = (T[]) new Object[capacity];
}
public boolean isEmpty(){
return front == -1;
}
public boolean isFull(){
return rear == capacity - 1;
}
// function to insert an element at the rear of the queue
public void enqueue(T data) {
if (isFull())
throw new UnsupportedOperationException("Queue is full!");
if(isEmpty())
front++;
rear++;
queue[rear] = data;
}
public T dequeue() {
if (isEmpty())
throw new UnsupportedOperationException("Queue is empty!");
T temp = queue[front];
if (rear == 0) {
rear = front = -1;
}
else{
for(int i = 0; i <= rear - 1; i++) {
queue[i] = queue[i + 1];
}
rear--;
}
return temp;
}
public boolean search(T e){
if (isEmpty())
throw new UnsupportedOperationException("Queue is empty!");
for(int i = 0; i <= rear; i++)
if(e.equals(queue[i]))
return true;
return false;
}
public String toString() {
if (isEmpty())
throw new UnsupportedOperationException("Queue is empty!");
String str = "";
for (int i = 0; i <= rear; i++) {
str = str + queue[i] + " ";
}
return str;
}
public T peek() {
if (isEmpty())
throw new UnsupportedOperationException("Queue is empty!");
return queue[front];
}
} King Fahd University of Petroleum & Minerals College of Computer Science and
Engineering Information and Computer Science Department ICS 202 - Data Structures
Recursion Objectives The objective of this lab is to design and implement recursive programs.
Outcomes After completing this Lab, students are expected to: Design recursive solutions. -
Implement recursive methods. Lab Tasks 1. (a) Comment the iterative public boolean
contains(Element e) method of the given SLL class then implement it as a recursive method. Use
appropriate helper method in your solution. (c) Use the given test program to test your recursive
methods. Sample program run: The list is: [ Taif Dammam Abha Riyadh Jubail ] It is true that
the list contains Dammam. It is false that the list contains Jeddah. 2. (a) Comment the iterative
public T dequeue () method of the given class QueueAsArray then implement it as a recursive
method. Use an appropriate helper method in your solution. (b) Write a test program to test the
recursive dequeue method. Sample program run: The queue is: 6020403070 First dequeued
element is: 60 Second dequeued element is: 20 After two node deletion the queue is: 403070
Element at queue front is: 4

More Related Content

PDF
Solve using Java programming language- ----------------------------.pdf
PDF
-JAVA-provide a test class that do the required -you may add met.pdf
PDF
Using Java programming language solve the followingSLL CLASS.pdf
PDF
Solve using java and using this Singly linked list classpublic cl.pdf
PDF
public class SLLT { protected SLLNodeT head, tail; pub.pdf
PDF
Help explain the code with line comments public class CompletedLis.pdf
PDF
Use the singly linked list class introduced in the lab to implement .pdf
PDF
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
Solve using Java programming language- ----------------------------.pdf
-JAVA-provide a test class that do the required -you may add met.pdf
Using Java programming language solve the followingSLL CLASS.pdf
Solve using java and using this Singly linked list classpublic cl.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
Help explain the code with line comments public class CompletedLis.pdf
Use the singly linked list class introduced in the lab to implement .pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf

Similar to Submit1) Java Files2) Doc file with the following contents.pdf (20)

DOCX
DS Code (CWH).docx
PDF
137 Lab-2.2.pdf
PDF
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
PPT
PDF
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
PDF
#includeiostream struct node {    char value;    struct no.pdf
PDF
Lab-2.2 717822E504.pdf
PDF
#include sstream #include linkylist.h #include iostream.pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
PDF
#include iostream #include cstring #include vector #i.pdf
PDF
mainpublic class AssignmentThree {    public static void ma.pdf
DOCX
Table.java Huffman code frequency tableimport java.io.;im.docx
PPTX
Data Structures and Agorithm: DS 09 Queue.pptx
DOCX
Linked lists
PDF
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
PPTX
Data Structures and Agorithm: DS 06 Stack.pptx
PPT
Link list part 2
PDF
Scala vs Java 8 in a Java 8 World
PDF
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
DS Code (CWH).docx
137 Lab-2.2.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
#includeiostream struct node {    char value;    struct no.pdf
Lab-2.2 717822E504.pdf
#include sstream #include linkylist.h #include iostream.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
#include iostream #include cstring #include vector #i.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
Table.java Huffman code frequency tableimport java.io.;im.docx
Data Structures and Agorithm: DS 09 Queue.pptx
Linked lists
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
Data Structures and Agorithm: DS 06 Stack.pptx
Link list part 2
Scala vs Java 8 in a Java 8 World
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf

More from akaluza07 (9)

PDF
Suppose a division of Washington Instruments Incorporated that sells.pdf
PDF
Use the ER Diagram that you created in your Homework Assignment ER .pdf
PDF
Use the ER Diagram that you created in your Homework Assignment ER.pdf
PDF
This is to test a balanced tree. I need help testing an unbalanced t.pdf
PDF
This includes the following � Setup MongoDB in the cloud � Gen.pdf
PDF
The Xerox Alto42 Imagine the value of cornering the technological ma.pdf
PDF
The java code works, I just need it to display the results as in t.pdf
PDF
The first assignment is about assessing the feasibility of the proje.pdf
PDF
Starter code provided below answer should be in C code please.Star.pdf
Suppose a division of Washington Instruments Incorporated that sells.pdf
Use the ER Diagram that you created in your Homework Assignment ER .pdf
Use the ER Diagram that you created in your Homework Assignment ER.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdf
This includes the following � Setup MongoDB in the cloud � Gen.pdf
The Xerox Alto42 Imagine the value of cornering the technological ma.pdf
The java code works, I just need it to display the results as in t.pdf
The first assignment is about assessing the feasibility of the proje.pdf
Starter code provided below answer should be in C code please.Star.pdf

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Classroom Observation Tools for Teachers
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
GDM (1) (1).pptx small presentation for students
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
master seminar digital applications in india
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Classroom Observation Tools for Teachers
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
GDM (1) (1).pptx small presentation for students
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
master seminar digital applications in india
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O5-L3 Freight Transport Ops (International) V1.pdf
VCE English Exam - Section C Student Revision Booklet
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3

Submit1) Java Files2) Doc file with the following contents.pdf

  • 1. Submit: 1) Java Files 2) Doc file with the following contents: //************************** 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; }
  • 2. 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;
  • 3. 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;
  • 4. } 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; } } public class SLL_Driver { public static void main(String[] args) { SLL myList = new SLL(); String[] cityNames = {"Jubail", "Riyadh", "Abha", "Dammam", "Taif"}; for(int i = 0; i < cityNames.length; i++) myList.addToHead(cityNames[i]); System.out.println("The list is: " + myList); System.out.println("It is " + myList.contains("Dammam") + " that the list contains Dammam."); System.out.println("It is " + myList.contains("Jeddah") + " that the list contains Jeddah."); } } // Java program to implement a queue using an array public class QueueAsArray { private int front, rear, capacity; private T[] queue;
  • 5. public QueueAsArray(int capacity) { front = rear = -1; this.capacity = capacity; queue = (T[]) new Object[capacity]; } public boolean isEmpty(){ return front == -1; } public boolean isFull(){ return rear == capacity - 1; } // function to insert an element at the rear of the queue public void enqueue(T data) { if (isFull()) throw new UnsupportedOperationException("Queue is full!"); if(isEmpty()) front++; rear++; queue[rear] = data; } public T dequeue() { if (isEmpty()) throw new UnsupportedOperationException("Queue is empty!"); T temp = queue[front]; if (rear == 0) { rear = front = -1; } else{ for(int i = 0; i <= rear - 1; i++) { queue[i] = queue[i + 1]; }
  • 6. rear--; } return temp; } public boolean search(T e){ if (isEmpty()) throw new UnsupportedOperationException("Queue is empty!"); for(int i = 0; i <= rear; i++) if(e.equals(queue[i])) return true; return false; } public String toString() { if (isEmpty()) throw new UnsupportedOperationException("Queue is empty!"); String str = ""; for (int i = 0; i <= rear; i++) { str = str + queue[i] + " "; } return str; } public T peek() { if (isEmpty()) throw new UnsupportedOperationException("Queue is empty!"); return queue[front]; } } King Fahd University of Petroleum & Minerals College of Computer Science and Engineering Information and Computer Science Department ICS 202 - Data Structures
  • 7. Recursion Objectives The objective of this lab is to design and implement recursive programs. Outcomes After completing this Lab, students are expected to: Design recursive solutions. - Implement recursive methods. Lab Tasks 1. (a) Comment the iterative public boolean contains(Element e) method of the given SLL class then implement it as a recursive method. Use appropriate helper method in your solution. (c) Use the given test program to test your recursive methods. Sample program run: The list is: [ Taif Dammam Abha Riyadh Jubail ] It is true that the list contains Dammam. It is false that the list contains Jeddah. 2. (a) Comment the iterative public T dequeue () method of the given class QueueAsArray then implement it as a recursive method. Use an appropriate helper method in your solution. (b) Write a test program to test the recursive dequeue method. Sample program run: The queue is: 6020403070 First dequeued element is: 60 Second dequeued element is: 20 After two node deletion the queue is: 403070 Element at queue front is: 4