Created
May 2, 2013 22:58
-
-
Save anonymous/5506103 to your computer and use it in GitHub Desktop.
Errors for the Queue.h file are:
Queue.h:52: error: expected unqualified-id before 'using'
Queue.h:52: error: expected initializer before 'using' I've never used the 'using' keyword before, so I'm pretty sure I must have messed that up somehow. Errors in LinkedList.h are redefinition of classes errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace std; | |
#include <iostream> | |
#ifndef LINKEDLIST_H | |
#define LINKEDLIST_H | |
template <class T> | |
struct Node | |
{ | |
T val; | |
Node<T> *next; | |
}; | |
template <class T> | |
class LinkedList | |
{ | |
public: | |
LinkedList(); | |
~LinkedList(); | |
void insertAtBack(T valueToInsert); | |
bool removeFromBack(); | |
void print(); | |
bool isEmpty(); | |
int size(); | |
void clear(); | |
void insertAtFront(T valueToInsert); | |
bool removeFromFront(); | |
T& firstNum(); | |
private: | |
Node<T> *first; | |
Node<T> *last; | |
}; | |
#endif | |
/*****************LINKEDLIST CPP FILE******************/ | |
template <class T> | |
LinkedList<T>::LinkedList() | |
{ | |
first = NULL; | |
last = NULL; | |
} | |
template <class T> | |
LinkedList<T>::~LinkedList() | |
{ | |
Node<T>* temp = first; | |
while(temp != NULL) | |
{ | |
temp = temp->next; | |
delete(first); | |
first = temp; | |
} | |
} | |
template <class T> | |
void LinkedList<T>::insertAtBack(T valueToInsert) | |
{ | |
Node<T>* newNode; | |
newNode->val = valueToInsert; | |
newNode->next = NULL; | |
Node<T>* temp = first; | |
if (temp != NULL) | |
{ | |
while (temp->next != NULL) | |
{ | |
temp = temp->next; | |
} | |
temp->next = newNode; | |
} | |
else | |
{ | |
first = newNode; | |
} | |
} | |
template <class T> | |
bool LinkedList<T>::removeFromBack() | |
{ | |
if (first == NULL && last == NULL) {return false;} | |
if (first == last) | |
{ | |
cout<<"First is equal to Last."<<endl; | |
delete(first); | |
first = last = NULL; | |
return true; | |
} | |
else | |
{ | |
Node<T>* temp = first; | |
int nodeCount = 0; | |
while (temp != NULL) | |
{ | |
nodeCount = nodeCount + 1; | |
temp = temp->next; | |
} | |
Node<T>* temp2 = first; | |
for(int i = 1; i < (nodeCount - 1); i++) | |
{ | |
temp2 = temp2->next; | |
} | |
cout << temp2->val<<endl; | |
delete(temp2->next); | |
last = temp2; | |
last->next = NULL; | |
return true; | |
} | |
} | |
template <class T> | |
void LinkedList<T>::print() | |
{ | |
Node<T>* temp = first; | |
if (temp == NULL) | |
{ | |
cout<<""; | |
} | |
if (temp->next == NULL) | |
{ | |
cout<<temp->val; | |
} | |
else | |
{ | |
while (temp != NULL) | |
{ | |
cout<< temp->val; | |
temp = temp->next; | |
cout<< ","; | |
} | |
} | |
} | |
template <class T> | |
bool LinkedList<T>::isEmpty() | |
{ | |
if (first == NULL && last == NULL) {return true;} | |
else {return false;} | |
} | |
template <class T> | |
int LinkedList<T>::size() | |
{ | |
if (first == NULL && last == NULL) {return 0;} | |
Node<T>* temp = first; | |
int nodeCounter = 0; | |
while (temp != NULL) | |
{ | |
nodeCounter = nodeCounter + 1; | |
temp = temp->next; | |
} | |
return nodeCounter; | |
} | |
template <class T> | |
void LinkedList<T>::clear() | |
{ | |
Node<T>* temp = first; | |
while(temp != NULL) | |
{ | |
temp = temp->next; | |
first = temp; | |
delete(temp); | |
} | |
} | |
template <class T> | |
void LinkedList<T>::insertAtFront(T valueToInsert) | |
{ | |
Node<T>* newNode; | |
newNode->val = valueToInsert; | |
if(first == NULL) | |
{ | |
first = newNode; | |
} | |
else | |
{ | |
newNode->next = first; | |
first = newNode; | |
} | |
} | |
template <class T> | |
bool LinkedList<T>::removeFromFront() | |
{ | |
if (first == NULL && last == NULL) {return false;} | |
else | |
{ | |
Node<T>* temp; | |
temp = first; | |
first = first->next; | |
delete(temp); | |
return true; | |
} | |
} | |
template <class T> | |
T& LinkedList<T>::firstNum() | |
{ | |
return first->val; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace std; | |
#include <iostream> | |
#include "LinkedList.h" | |
#ifndef QUEUE_H | |
#define QUEUE_H | |
template <class T> | |
class Queue: public LinkedList<T> | |
{ | |
public: | |
Queue(); | |
~Queue(); | |
void enqueue(T value); | |
T dequeue(); | |
T& front(); | |
}; | |
#endif | |
/****************QUEUE CPP FILE****************/ | |
template <class T> | |
Queue<T>::Queue(){} | |
template <class T> | |
Queue<T>::~Queue(){} | |
template <class T> | |
void Queue<T>::enqueue(T value) | |
{ | |
insertAtBack(value); | |
} | |
template <class T> | |
T using LinkedList<T>::dequeue() | |
{ | |
if(isEmpty()) | |
{ | |
throw 2; | |
} | |
else | |
{ | |
T firstElmnt = firstNum(); | |
removeFromFront(); | |
return firstElmnt; | |
} | |
} | |
template <class T> | |
T& using LinkedList<T>::front() | |
{ | |
if(isEmpty()) | |
{ | |
throw 3; | |
} | |
else | |
{ | |
return firstNum(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment