Computer Science Lab Work, programming homework help

User Generated

nyrkfnqre

Computer Science

Description

Lab work PLEASE see the files

Unformatted Attachment Preview

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
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Please check the attached file, i modified the code as per requirement. If you want to make more changes or add something else. Please let me know.Thanks

Lab 9
Source Code
#ifndef QUEUE_H
#define QUEUE_H
#include "LinkedList.h"
#include
using namespace std;
template
class Queue : public LinkedList , public Node
{
public:
Queue();
void enqueue(T element);
T dequeue() throw (runtime_error);
int getSize();
private:
LinkedList list;
Node *front, *rear;
int size;
};
template
Queue::Queue()
{
front = rear = NULL;
size = 0;
}
template
void Queue::enqueue(T element)
{
if(front == NULL)
{
front = new Node (element)
rear=front;
size++;
list.addFirst(element)
}
else
{
rear->next = new Node(element);
rear=rear->next;
size++;
list.addLast(element);
}
}

template
T Queue::dequeue() throw (runtime_error)
{
if(rear!=NULL){
Node *temp = front;
front = front->next;
delete temp;
sze--;
}
return list.removeFirst();
}
template
int Queue::getSize()
{
return list.getSize();
}
#endif

LinkedList.h
#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 ...


Anonymous
I was having a hard time with this subject, and this was a great help.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags