Computer Science Lab work.

Anonymous
timer Asked: Nov 9th, 2016

Question description

I browse the files.

Lab 9 We have seen in class how to model and implement a queue dynamically using a Linked List in a composition relation. Implement the class Queue that uses Linked List as an inheritance relation. Note: You have as attachment the code of the Linked List and Queue using Linked List as composition. You just need to modify it.
#ifndef LINKEDLIST_H #define LINKEDLIST_H #include using namespace std; template class Node { public: T element; // Element contained in the node Node *next; // Pointer to the next node Node() // No-arg constructor { next = NULL; } Node(T element) // Constructor { this->element = element; next = NULL; } }; template class LinkedList { public: LinkedList(); void addFirst(T element); void addLast(T element); T getFirst(); T getLast(); T removeFirst() throw (runtime_error); T removeLast(); void add(T element); void add(int index, T element); void clear(); bool contains(T element); T get(int index); int indexOf(T element); bool isEmpty(); int lastIndexOf(T element); bool remove(T element); int getSize(); T remove(int index); T set(int index, T element); private: Node *head, *tail; int size; }; template LinkedList::LinkedList() { head = tail = NULL; size = 0; } template void LinkedList::addFirst(T element) { Node *newNode = new Node(element); newNode->next = head; head = newNode; size++; if (tail == NULL) tail = head; } template void LinkedList::addLast(T element) { if (tail == NULL) { head = tail = new Node(element); } else { tail->next = new Node(element); tail = tail->next; } size++; } template T LinkedList::getFirst() { if (size == 0) throw runtime_error("Index out of range"); else return head->element; } template T LinkedList::getLast() { if (size == 0) throw runtime_error("Index out of range"); else return tail->element; } template T LinkedList::removeFirst() throw (runtime_error) { if (size == 0) throw runtime_error("No elements in the list"); else { Node *temp = head; head = head->next; size--; T element = temp->element; delete temp; return element; } } template T LinkedList::removeLast() { if (size == 0) throw runtime_error("No elements in the list"); else if (size == 1) { Node *temp = head; head = tail = NULL; size = 0; T element = temp->element; delete temp; return element; } else { Node *current = head; for (int i = 0; i < size - 2; i++) current = current->next; Node *temp = tail; tail = current; tail->next = NULL; size--; T element = temp->element; delete temp; return element; } } template void LinkedList::add(T element) { addLast(element); } template void LinkedList::add(int index, T element) { if (index == 0) addFirst(element); else if (index >= size) addLast(element); else { Node *current = head; for (int i = 1; i < index; i++) current = current->next; Node *temp = current->next; current->next = new Node(element); (current->next)->next = temp; size++; } } template void clear() { while (head != NULL) { Node *temp = head; delete temp; head = head->next; } tail = NULL; } template T LinkedList::get(int index) { if (index < 0 || index > size - 1) throw runtime_error("Index out of range"); Node *current = head; for (int i = 0; i < index; i++) current = current->next; return current->element; } template int LinkedList::indexOf(T element) { // Implement it in this exercise Node *current = head; for (int i = 0; i < size; i++) { if (current->element == element) return i; current = current->next; } return -1; } template bool LinkedList::isEmpty() { return head == NULL; } template int LinkedList::getSize() { return size; } template T LinkedList::remove(int index) { if (index < 0 || index >= size) throw runtime_error("Index out of range"); else if (index == 0) return removeFirst(); else if (index == size - 1) return removeLast(); else { Node *previous = head; for (int i = 1; i < index; i++) { previous = previous->next; } Node *current = previous->next; previous->next = current->next; size--; T element = current->element; delete current; return element; } } template bool LinkedList::remove(T element) { Node *previous = head; Node *current; if (head != NULL) { if (element == head->element) { head = head->next; size--; return true; } else { current = head->next; } } else return false; for (int i = 0; i < size - 1; i++) { if (element == current->element) { previous->next = current->next; // Remove the current element size--; return true; } else { previous = current; current = current->next; } } return false; } template int LinkedList::lastIndexOf(T element) { int lastIndex = -1; Node *current = head; for (int i = 0; i < size; i++) { if (current->element == element) lastIndex = i; current = current->next; } return lastIndex; } template bool LinkedList::contains(T element) { // Implement it in this exercise Node *current = head; for (int i = 0; i < size; i++) { if (current->element == element) return true; current = current->next; } return false; } template T LinkedList::set(int index, T element) { if (index < 0 || index >= size) throw runtime_error("Index out of range"); Node *current = head; for (int i = 0; i < index; i++) { current = current->next; } T oldElement = current->element; current->element = element; return oldElement; } #endif
#ifndef QUEUE_H #define QUEUE_H #include "LinkedList.h" #include using namespace std; template class Queue { public: Queue(); void enqueue(T element); T dequeue() throw (runtime_error); int getSize(); private: LinkedList list; }; template Queue::Queue() { } template void Queue::enqueue(T element) { list.addLast(element); } template T Queue::dequeue() throw (runtime_error) { return list.removeFirst(); } template int Queue::getSize() { return list.getSize(); } #endif

Tutor Answer

(Top Tutor) Studypool Tutor
School: UC Berkeley
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags
Study Guides

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors