Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // linkedlist.h
- // Forritunarverkefni 5
- //
- // Created by Nokkvi Karlsson on 2/21/18.
- // Copyright © 2018 Nokkvi Karlsson. All rights reserved.
- //
- #include "queue.h"
- #include "stack.h"
- #include "node.h"
- #ifndef linkedlist_h
- #define linkedlist_h
- class EmptyException { };
- template <class T>
- class LinkedList : public Stack<T>, public Queue<T>
- {
- public:
- LinkedList(){
- head = NULL;
- tail = NULL;
- }
- virtual ~LinkedList(){
- Node<T> *tmp = head;
- while(head != NULL) {
- head = head->next;
- delete tmp;
- tmp = head;
- }
- head = NULL;
- tail = NULL;
- }
- void headInsert(T data){
- head = new Node<T>(data, head);
- if(tail == NULL) {
- tail = head;
- }
- }
- void tailInsert(T data) {
- if(head == NULL) {
- headInsert(data);
- }
- else {
- Node<T> *node = new Node<T>(data, NULL);
- tail->next = node;
- tail = node;
- }
- }
- T headRemove() {
- if(head == NULL) {
- throw EmptyException();
- }
- T data = head->data;
- Node<T> *tmpNode = head;
- head = head->next;
- if(head == NULL) {
- tail = head;
- }
- delete tmpNode;
- return data;
- }
- void push(T data) {
- headInsert(data);
- }
- T pop() {
- return headRemove();
- }
- void add(T data) {
- tailInsert(data);
- }
- T remove() {
- return headRemove();
- }
- void print(ostream& outs) const {
- Node<T>* currNode = head;
- while(currNode != NULL) {
- outs << currNode->data << " ";
- currNode = currNode->next;
- }
- }
- private:
- Node<T>* head;
- Node<T>* tail;
- int itemCount;
- };
- #endif /* linkedlist_h */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement