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;
}
@asterwolf
Copy link
Author

Revision 2: changes while loop from function exists to for loop. Removed else statement as it was only moving pointer n to the next node, but now with a for loop, the update shall do it already.

@asterwolf
Copy link
Author

Revision 3: Made more changes to my for loop in the exists function (little problems i forgot to update).

Attempted to write the remove function and ran into problems (will pick up in the morning).

Made a few changes to the add function, realized that it wouldn't add item if the item was greater than every element of the linked list the way I had it set up before and so I added another if statement after the for loop to check one more time if the item is in linked list. If it isn't at this point, it was probably larger than every element of the linked list and will be added now.

size is increment every time the add function is called, but what if nothing is added to the linked list because the item already exists in the linked list?

Will attempt the destructor in the morning. (must release memory of the old linked list after all elements are copied over to the new linked list.

@Angel-Rojas
Copy link

Your line #9 and #10 aren't needed i believe, mendoza's main already has them implemented. The changes i made make your program run finally without crashing... but still incorrect output.
-Angel

@asterwolf
Copy link
Author

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..

@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