Last active
August 29, 2015 14:19
-
-
Save asterwolf/c5bf5479d9d6b9c573fd to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* | |
* @author: Diego Diaz | |
* Course: COMP B12 | |
* Created on: Apr 23, 2015 | |
* Source File: | |
*/ | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
template <typename T> | |
SortedLinkedList<T>::~SortedLinkedList(){ | |
Node *temp; | |
while(head != 0){ | |
temp = head; | |
head = head-> next; | |
delete temp; | |
} | |
size = 0; | |
} | |
/** | |
* Adds item to the list in the proper place - no duplicates allowed | |
* If item already exists in list, add() silently does nothing | |
* @param item - item to add to list | |
*/ | |
template <typename T> | |
void SortedLinkedList<T>::add(const T item){ | |
Node *newNode = new Node; | |
Node *trailingHead; | |
trailingHead = head; | |
for(Node *n = head->next; n != 0; n = n->next){ | |
if(item < n->element && !exists(item)){ | |
newNode->element = item; | |
trailingHead->next = newNode; | |
newNode->next = n; | |
size++ | |
} | |
trailingHead = trailingHead->next; | |
} | |
/** | |
* Removes all elements from list that match item. Does nothing if item | |
* is not in list | |
* @param item - item to remove from list | |
*/ | |
template <typename T> | |
void SortedLinkedList<T>::remove(const T item){ | |
Node *newNode = new Node; | |
for(Node *n = head; n !=0; n = n->next){ | |
if(n->element == item){ | |
delete n->element; | |
--size; | |
} | |
} | |
} | |
/** | |
* Searches for item in list | |
* @param item - item to search for | |
* @return true if item exists in list, false otherwise | |
*/ | |
template <typename T> | |
bool SortedLinkedList<T>::exists(const T item) const{ | |
for(Node *n = head; n != 0; n = n->next){ | |
if(item == n->element){ | |
return true; | |
} | |
} | |
return false; | |
} |
I'm turning yet another lab without running it.
Oh, Diego…
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I'm nearly done and almost right on, but I don't know the command prompt code to actually run it. So I'm turning yet another lab without running it. It although compiles now..