Created
April 4, 2014 20:59
-
-
Save mysteriouspants/9983014 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
#include "linked_list.hpp" | |
int main(int args, const char* argv[]) { | |
linked_list<int> foo; | |
foo.append(1); | |
foo.append(2); | |
foo.insert(3); | |
foo.remove(2); | |
return 0; | |
} |
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
#include "linked_list.hpp" | |
template<class T> | |
linked_list<T>::linked_list() : head(NULL) { } | |
template<class T> | |
linked_list<T>::~linked_list() { | |
node * curr = head; | |
if (curr == NULL) { return; } | |
while (curr->next != NULL) { curr = curr->next; } | |
while (curr != NULL) { | |
curr = curr-> prev; | |
delete curr; | |
} | |
} | |
template<class T> | |
linked_list<T>::node::node() : next(NULL), prev(NULL) { } | |
template<class T> | |
linked_list<T>::node::node(T t, node * prev, node * next) : data(t), next(next), prev(prev) { } | |
template<class T> | |
void linked_list<T>::append(T t) { | |
if (head == NULL) { | |
head = new node(t, NULL, NULL); | |
} else { | |
node * curr = head; | |
while (curr->next != NULL) { curr = curr->next; } | |
curr->next = new node(t, curr, NULL); | |
} | |
} | |
template<class T> | |
void linked_list<T>::insert(T t) { | |
if (head == NULL) { | |
append(t); | |
} else { | |
head = new node(t, NULL, head); | |
head->next->prev = head; | |
} | |
} | |
template<class T> | |
void linked_list<T>::remove(T t) { | |
if (head == NULL) { return; } | |
node * curr = head; | |
while (curr->next != NULL && curr->data != t) { curr = curr-> next; } | |
if (curr->data == t) { | |
if (curr == head) { | |
head = curr->next; | |
head->prev = NULL; | |
delete curr; | |
} else { | |
curr->prev->next = curr->next; | |
if (curr->next != NULL) { curr->next->prev = curr->prev; } | |
delete curr; /* lawl */ | |
} | |
} | |
} |
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
#include <cstddef> | |
#include <memory> | |
#ifndef __LINKED_LIST_HPP__ | |
#define __LINKED_LIST_HPP__ | |
template<class T> | |
class linked_list { | |
private: | |
struct node { | |
T data; | |
node * next; | |
node * prev; | |
node(); | |
node(T data, node * prev,node * next); | |
}; | |
node * head; | |
public: | |
linked_list(); | |
~linked_list(); | |
void append(T); | |
void insert(T); | |
void remove(T); | |
}; | |
#endif |
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
# Chris Miller ([email protected]), CS124 | |
# Copyright (C) 2014 Chris Miller. Aw richts pitten by. | |
# Academic endorsement. This code is not licensed for commercial use. | |
# 20140404, Chapter 17 Programming Challenge 1 | |
CXX=clang++ | |
CXXFLAGS=-std=c++11 -stdlib=libc++ -Wall | |
SOURCES=ch17pc1.cpp linked_list.cpp | |
OBJECTS=$(SOURCES:.cpp=.o) | |
EXECUTABLE=ch17pc1 | |
all: $(SOURCES) $(EXECUTABLE) | |
$(EXECUTABLE): $(OBJECTS) | |
$(CXX) $(OBJECTS) -o $@ | |
.cpp.o: | |
$(CXX) $(CXXFLAGS) -c $< -o $@ | |
clean: | |
rm -fv $(EXECUTABLE) $(OBJECTS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment