Skip to content

Instantly share code, notes, and snippets.

@asterwolf
Last active August 29, 2015 14:19
Show Gist options
  • Save asterwolf/c5bf5479d9d6b9c573fd to your computer and use it in GitHub Desktop.
Save asterwolf/c5bf5479d9d6b9c573fd to your computer and use it in GitHub Desktop.
/*
*
* @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;
}
@pathawks
Copy link

I'm turning yet another lab without running it.

Oh, Diego… :rage3:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment