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 implem.
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