adding cs235

This commit is contained in:
dm
2016-04-06 20:46:10 -07:00
parent 8e52ce1982
commit cf99ec6565
178 changed files with 13079 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
#include "Factory.h"
#include "ll.h"
//You may add #include statements here
/*
You will MODIFY THIS DOCUMENT.
*/
//=======================================================================================
/*
getLinkedList()
Creates and returns an object whose class extends LinkedListInterface.
This should be an object of a class you have created.
Example: If you made a class called "LinkedList", you might say, "return new LinkedList();".
*/
LinkedListInterface * Factory::getLinkedList()
{
return new ll();//Modify this line
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include "LinkedListInterface.h"
using namespace std;
/*
WARNING: It is expressly forbidden to modify any part of this document, including its name
*/
//=======================================================================================
/*
getLinkedList()
Creates and returns an object whose class extends LinkedListInterface.
This should be an object of a class you have created.
Example: If you made a class called "LinkedList", you might say, "return new LinkedList();".
*/
class Factory
{
public:
static LinkedListInterface * getLinkedList();
};
//=======================================================================================
+80
View File
@@ -0,0 +1,80 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include <string>
using namespace std;
class LinkedListInterface
{
public:
LinkedListInterface(void){};
virtual ~LinkedListInterface(void){};
/*
insertHead
A node with the given value should be inserted at the beginning of the list.
Only non-negative values should be added to the list. Do not allow
duplicate values in the list.
*/
virtual void insertHead(int value) = 0;
/*
insertTail
A node with the given value should be inserted at the end of the list.
Only non-negative values should be added to the list. Do not allow
duplicate values in the list.
*/
virtual void insertTail(int value) = 0;
/*
insertAfter
A node with the given value should be inserted immediately after the
node whose value is equal to insertionNode.
A node should only be added if the node whose value is equal to
insertionNode is in the list. Only non-negative values should be
added to the list. Do not allow duplicate values in the list.
*/
virtual void insertAfter(int value, int insertionNode) = 0;
/*
remove
The node with the given value should be removed from the list.
The list may or may not include a node with the given value.
*/
virtual void remove(int value) = 0;
/*
clear
Remove all nodes from the list.
*/
virtual void clear() = 0;
/*
at
Returns the value of the node at the given index. The list begins at
index 0.
If the given index is out of range of the list, return -1;
*/
virtual int at(int index) = 0;
/*
size
Returns the number of nodes in the list.
*/
virtual int size() = 0;
};
+27
View File
@@ -0,0 +1,27 @@
CXXFLAGS=-Wall -g
all: test main
test: test.cc Factory.o ll.o
main: pwnd.o Factory.o ../ignoreme.a ll.o
$(CXX) $(CXXFLAGS) -o $@ $^
# these deps need to be set up to allow for appropriate recompilation
Factory.o: Factory.h Factory.cpp
ll.o: ll.h ll.cc
pwnd.o: pwnd.c
clean:
@rm -vf *.o main test core libpwnd* vgcore.*
run: main
./main
gdb: test
gdb ./test
rtest: test
./test
valgrind: main
valgrind --tool=memcheck --leak-check=yes --max-stackframe=5000000 \
--show-reachable=yes --suppressions=../To\ Students/DONT_DELETE.supp ./main
+158
View File
@@ -0,0 +1,158 @@
#include "ll.h"
ll::ll(): _size(0), head(NULL), tail(NULL) {
}
ll::~ll() {
clear();
}
void ll::insertHead(int value) {
if(value < 0 or find(value)) {
return;
}
if(head == NULL) {
assert(tail == NULL);
assert(_size == 0);
head = new node(value);
tail = head;
}
else {
node * tmp = head;
head = new node(value);
head->next = tmp;
}
_size++;
}
void ll::insertTail(int value) {
if(value < 0 or find(value)) {
return;
}
if(tail == NULL) {
assert(head == NULL);
assert(_size == 0);
tail = new node(value);
head = tail;
}
else {
node * tmp = tail;
tmp->next = new node(value);
tail = tmp->next;
}
_size++;
}
void ll::insertAfter(int value, int insertionNode) {
if(value < 0 or find(value)) {
return;
}
node * inlist = find(insertionNode);
if(not inlist) {
return;
}
node * toinsert = new node(value);
if(inlist == tail) {
tail = toinsert;
}
toinsert->next = inlist->next;
inlist->next = toinsert;
_size++;
}
void ll::remove(int value) {
if(not find(value)) {
// should deal with empty list, and not found
return;
}
else if(head == tail) {
delete head;
head = tail = NULL;
_size--;
}
else {
node * cur = head;
if(head->value == value) {
head = head->next;
delete cur;
_size--;
}
else {
while(cur) {
if(cur->next == NULL) {
// removing end ...
break;
}
if(cur->next->value == value) {
node * to_remove = cur->next;
if(to_remove == tail) {
tail = cur;
}
cur->next = to_remove->next;
delete to_remove;
_size--;
}
cur = cur->next;
}
}
}
}
void ll::clear() {
node * tmp = head;
while(head) {
tmp = head;
head = head->next;
delete tmp;
_size--;
}
head = tail = NULL;
assert(_size == 0);
}
int ll::at(int index) {
if (index >= _size or index < 0) {
return -1;
}
node * tmp = head;
for(int i = 0; i < index; i++) {
tmp = tmp->next;
}
if(tmp == NULL) {
return -1;
}
else {
return tmp->value;
}
}
int ll::size() {
return _size;
}
node * ll::find(int value) {
node * tmp = head;
while(tmp) {
if(tmp->value == value) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
ostream & operator<<(ostream & os, const node & n) {
os << n.value;
return os;
}
ostream & operator<<(ostream & os, const ll & l) {
os << "ll {(" << l._size << ") ";
node * cur = l.head;
while (cur) {
os << *cur << ", ";
cur = cur->next;
}
os << "}";
return os;
}
+42
View File
@@ -0,0 +1,42 @@
#ifndef __LL_H__
#define __LL_H__
#include <iostream>
#include <cassert>
#include "Factory.h"
using namespace std;
struct node {
int value;
node * next;
node(int value): value(value), next(NULL) {};
friend ostream & operator<<(ostream &, const node &);
};
class ll : public LinkedListInterface
{
private:
int _size;
public:
ll();
~ll();
node * head;
node * tail;
void insertHead(int value);
void insertTail(int value);
void insertAfter(int value, int insertionNode);
void remove(int value);
void clear();
int at(int index);
int size();
node * find(int value);
friend ostream & operator<<(ostream & os, const ll & i);
};
#endif
+5
View File
@@ -0,0 +1,5 @@
#include <unistd.h>
int usleep(useconds_t usec) {
return 0;
}
+174
View File
@@ -0,0 +1,174 @@
#include <iostream>
#include <cassert>
using namespace std;
#include "Factory.h"
#include "ll.h"
int main() {
LinkedListInterface * lli = Factory::getLinkedList();
ll * myll = (ll*)lli;
myll->insertHead(8);
myll->insertHead(5);
myll->insertHead(3);
myll->insertHead(2);
myll->insertHead(1);
// should be duplicate
myll->insertHead(1);
myll->insertHead(0);
assert(myll->size() == 6);
assert(myll->head->value == 0);
assert(myll->tail->value == 8);
assert(myll->at(-8) == -1);
assert(myll->at(3238) == -1);
assert(myll->at(3) == 3);
delete myll;
{
ll ll2 = ll();
ll2.insertTail(5);
ll2.insertTail(6);
assert(ll2.size() == 2);
assert(ll2.head->value == 5);
assert(ll2.tail->value == 6);
}
{
// test for negative insertion
ll ll4 = ll();
ll4.insertHead(-1);
ll4.insertTail(-1);
assert(ll4.size() == 0);
}
{
// test for duplicate insertion
ll ll3 = ll();
ll3.insertTail(1);
ll3.insertTail(2);
ll3.insertTail(3);
ll3.insertTail(4);
ll3.insertTail(4);
ll3.insertTail(4);
ll3.insertTail(5);
assert(ll3.size() == 5);
}
{
// test find
ll ll5 = ll();
ll5.insertHead(0);
ll5.insertTail(1);
ll5.insertTail(2);
assert(ll5.find(2));
assert(ll5.find(200) == NULL);
}
{
// test insertAfter
ll ll6 = ll();
ll6.insertHead(0);
ll6.insertTail(2);
ll6.insertTail(3);
assert(ll6.size() == 3);
ll6.insertAfter(1, 0);
assert(ll6.size() == 4);
ll6.insertAfter(1, 23);
assert(ll6.size() == 4);
}
{
ll ll7 = ll();
ll7.insertAfter(1, 0);
}
{
// test deletion
ll ll8 = ll();
ll8.insertTail(1);
ll8.insertTail(2);
ll8.insertTail(3);
ll8.insertTail(4);
ll8.insertTail(5);
ll8.remove(4);
assert(ll8.find(4) == NULL);
assert(ll8.size() == 4);
assert(ll8.at(0) == 1);
assert(ll8.at(1) == 2);
assert(ll8.at(2) == 3);
assert(ll8.at(3) == 5);
assert(ll8.at(4) == -1);
assert(ll8.at(5) == -1);
}
{
ll ll9 = ll();
ll9.remove(4);
assert(ll9.size() == 0);
assert(ll9.at(1) == -1);
}
{
ll ll10 = ll();
ll10.insertTail(4);
assert(ll10.size() == 1);
ll10.remove(4);
assert(ll10.size() == 0);
}
{
ll ll11 = ll();
ll11.insertHead(4);
assert(ll11.size() == 1);
assert(ll11.at(0) == 4);
ll11.remove(4);
assert(ll11.size() == 0);
}
{
ll ll12 = ll();
ll12.insertTail(1);
ll12.insertTail(9);
assert(ll12.size() == 2);
ll12.remove(1);
assert(ll12.size() == 1);
}
{
ll ll13 = ll();
ll13.insertTail(9);
ll13.insertTail(3);
assert(ll13.size() == 2);
ll13.insertTail(16);
assert(ll13.size() == 3);
}
{
ll ll_14 = ll();
ll_14.insertHead(9);
ll_14.insertHead(3);
assert(ll_14.size() == 2);
ll_14.insertTail(16);
assert(ll_14.size() == 3);
}
{
// test enforced by assert in clear
ll l = ll();
l.insertHead(16);
l.insertTail(32);
l.insertTail(38);
l.remove(38);
l.insertTail(9);
}
{
// test enforced by asserts in ll
ll l = ll();
l.insertHead(16);
l.insertAfter(14, 16);
l.insertTail(17);
}
cout << "You passed" << endl;
}