Created
October 3, 2014 20:26
-
-
Save NimaBoscarino/ae8be77aa410bbda16b6 to your computer and use it in GitHub Desktop.
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
// | |
// LinkedList.cpp | |
// Assignment2 | |
// | |
// Created by Nima Boscarino on 2014-09-29. | |
// Copyright (c) 2014 NimaBoscarino. All rights reserved. | |
// | |
#ifdef __Assignment2__LinkedList__ | |
template <class T> | |
LinkedList<T>::LinkedList() | |
{ | |
front = NULL; | |
end = NULL; | |
size = 0; | |
} | |
template <class T> | |
LinkedList<T>::LinkedList(const LinkedList& ll) | |
{ | |
//copy constructor | |
} | |
template <class T> | |
LinkedList<T>::~LinkedList() | |
{ | |
while (!IsEmpty()) RemoveAt(0); | |
} | |
template <class T> | |
LinkedList<T>& LinkedList<T>::operator= (const LinkedList<T>& ll) | |
{ | |
//LOL FIGURE THIS OUT | |
} | |
template <class T> | |
int LinkedList<T>::Size() const | |
{ | |
return size; | |
} | |
template <class T> | |
bool LinkedList<T>::IsEmpty() const | |
{ | |
return (size == 0); | |
} | |
template <class T> | |
bool LinkedList<T>::Contains(T item) const | |
{ | |
Node<T>* runningPointer = front; | |
while (runningPointer != NULL) | |
{ | |
if (runningPointer->data == item) | |
{ | |
return true; | |
} | |
runningPointer = runningPointer->next; | |
} | |
return false; | |
} | |
template <class T> | |
T LinkedList<T>::ElementAt(int p) const //should throw an error with invalid index | |
{ | |
// check to see if this is good | |
Node<T>* out = front; | |
while (p > 0) | |
{ | |
out = out->next; | |
p--; | |
} | |
return out->data; | |
} | |
template <class T> | |
void LinkedList<T>::InsertAt(T item, int p) //need to put in exception handling | |
{ | |
Node<T>* itemNode = new Node<T>(item); | |
if (size == 0) //List is empty | |
{ | |
front = itemNode; | |
end = itemNode; | |
} | |
else if (p == size) //insert at end | |
{ | |
itemNode->previous = end; | |
end->next = itemNode; | |
end = itemNode; | |
} | |
else if (p == 0) //insert at front | |
{ | |
itemNode->next = front; | |
front->previous = itemNode; | |
front = itemNode; | |
} | |
else //every other case | |
{ | |
Node<T>* nodeOfInterest = NodeAt(p); //to prevent running NodeAt(p) for every single line here | |
nodeOfInterest->previous->next = itemNode; | |
itemNode->previous = nodeOfInterest->previous; | |
itemNode->next = nodeOfInterest; | |
nodeOfInterest->previous = itemNode; | |
} | |
size++; //increase, since there's a new item in the list | |
} | |
template <class T> | |
T LinkedList<T>::RemoveAt(int p) //needs exception handling | |
{ | |
Node<T>* itemNode = NodeAt(p); | |
T dataOutput = itemNode->data; | |
if (size == 1) //List will become empty | |
{ | |
front = NULL; | |
end = NULL; | |
} | |
else if (p == size) //remove from end | |
{ | |
end = itemNode->previous; | |
end->next = NULL; | |
} | |
else if (p == 0) //insert at front | |
{ | |
front->previous = itemNode; | |
itemNode->next = front; | |
front = itemNode; | |
} | |
else //every other case | |
{ | |
(itemNode->previous)->next = (itemNode->next); | |
(itemNode->next)->previous = (itemNode->previous); | |
} | |
size--; //decrease, since there is one less item in the list | |
delete itemNode; | |
return dataOutput; | |
} | |
template <class T> | |
Node<T>* LinkedList<T>::NodeAt(int p) | |
{ | |
Node<T>* out = front; | |
while (p > 0) | |
{ | |
out = out->next; | |
p--; | |
} | |
return out; | |
} | |
#endif /* __Assignment2__LinkedList__ */ | |
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
// | |
// LinkedList.h | |
// Assignment2 | |
// | |
// Created by Nima Boscarino on 2014-09-29. | |
// Copyright (c) 2014 NimaBoscarino. All rights reserved. | |
// | |
#ifndef __Assignment2__LinkedList__ | |
#define __Assignment2__LinkedList__ | |
#include <iostream> | |
template <class T> | |
class Node { | |
public: | |
T data; | |
Node<T>* previous = NULL; | |
Node<T>* next = NULL; | |
Node(T _data) | |
{ | |
data = _data; | |
} | |
}; | |
template <class T> | |
class LinkedList { | |
public: | |
LinkedList(); | |
LinkedList(const LinkedList& ll); | |
~LinkedList(); | |
LinkedList& operator= (const LinkedList& ll); | |
int Size() const; | |
bool IsEmpty() const; | |
bool Contains(T item) const; | |
T ElementAt(int p) const; | |
void InsertAt(T item, int p); | |
T RemoveAt(int p); | |
private: | |
Node<T>* front; | |
Node<T>* end; | |
int size; | |
Node<T>* NodeAt(int p); | |
}; | |
#include "LinkedList.cpp" | |
#endif /* defined(__Assignment2__LinkedList__) */ |
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
// | |
// main.cpp | |
// Assignment2 | |
// | |
// Created by Nima Boscarino on 2014-09-29. | |
// Copyright (c) 2014 NimaBoscarino. All rights reserved. | |
// | |
#include <iostream> | |
#include "LinkedList.h" | |
using namespace std; | |
int main(int argc, const char * argv[]) | |
{ | |
cout << "HELLO\n"; | |
LinkedList<string> list; | |
list.InsertAt("Element1", 0); | |
list.InsertAt("NIMA", 0); | |
if (list.Contains("NIMA")) { | |
cout << "IT WORKED" << endl; | |
} | |
cout << list.Size(); | |
cout << "END" << endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment