adding cs235
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#include "Factory.h"
|
||||
#include "linkedlist.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 linkedlist();//Modify this line
|
||||
}
|
||||
@@ -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();
|
||||
};
|
||||
//=======================================================================================
|
||||
@@ -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;
|
||||
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=Factory.o linkedlist.o node.o ../ignoreme.a
|
||||
EXE=main
|
||||
all: libpwnd.so $(EXE)
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
main.o: main.cpp Factory.o linkedlist.o node.o libpwnd.so ../ignoreme.a
|
||||
g++ -Wall -g -o main Factory.o linkedlist.o ../ignoreme.a
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
linkedlist.o: linkedlist.cpp linkedlist.h
|
||||
node.o: node.cpp node.h
|
||||
test: linkedlist.o node.o test.cpp
|
||||
$(CXX) $(CXXFLAGS) linkedlist.o node.o test.cpp -o $@
|
||||
|
||||
run: main
|
||||
LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. ./main
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
@rm -vf *.1
|
||||
@rm -vf *.0
|
||||
|
||||
drun: main
|
||||
LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. gdb ./main
|
||||
|
||||
debug: test
|
||||
LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. gdb ./test
|
||||
|
||||
valgrind: $(EXE)
|
||||
LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
||||
|
||||
libpwnd.so: pwnd.c
|
||||
gcc -shared -o libpwnd.so.1.0 pwnd.c
|
||||
ln -fs libpwnd.so.1.0 libpwnd.so.1
|
||||
ln -fs libpwnd.so.1 libpwnd.so
|
||||
@@ -0,0 +1,158 @@
|
||||
#include "linkedlist.h"
|
||||
|
||||
linkedlist::linkedlist(): head(NULL) {}
|
||||
linkedlist::~linkedlist(){clear();}
|
||||
|
||||
bool linkedlist::is_it_zero(int value) {
|
||||
if(value < 0)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
bool linkedlist::has_a(int value) {
|
||||
// cout << "has_a(" << value << ")" << endl;
|
||||
node* node_ptr = head;
|
||||
if(head == NULL)
|
||||
return false;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->value == value)
|
||||
return true;
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void linkedlist::insertHead(int value) {
|
||||
// cout << "inserthead(" << value << ")" << endl;
|
||||
// cout << *this << endl;
|
||||
if(!is_it_zero(value))
|
||||
return;
|
||||
if(has_a(value))
|
||||
return;
|
||||
head = new node(value, head);
|
||||
// cout << *this << endl << endl;
|
||||
}
|
||||
|
||||
void linkedlist::insertTail(int value) {
|
||||
// cout << "insertTail(" << value << ")" << endl;
|
||||
// cout << *this << endl;
|
||||
if(!is_it_zero(value))
|
||||
return;
|
||||
if(has_a(value))
|
||||
return;
|
||||
if(head == NULL) {
|
||||
insertHead(value);
|
||||
return;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->next == NULL) {
|
||||
node_ptr->next = new node(value, NULL);
|
||||
break;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
// cout << *this << endl << endl;
|
||||
}
|
||||
|
||||
void linkedlist::insertAfter(int value, int insertionNode) {
|
||||
// cout << "insertAfter(" << value << ", insertionNode" << insertionNode << ")" << endl;
|
||||
// cout << *this << endl;
|
||||
if(value < 0 || insertionNode < 0) {
|
||||
//cout << "first check" << endl;
|
||||
return;
|
||||
}
|
||||
if(has_a(value) || !has_a(insertionNode)) {
|
||||
//cout << "failing" << endl;
|
||||
return;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->value == insertionNode) {
|
||||
node* temp = new node(value, NULL);
|
||||
temp->next = node_ptr->next;
|
||||
node_ptr->next = temp;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
// cout << *this << endl << endl;
|
||||
}
|
||||
|
||||
void linkedlist::remove(int value) {
|
||||
// cout << "remove(" << value << ")" << endl;
|
||||
// cout << *this << endl;
|
||||
if(!is_it_zero(value))
|
||||
return;
|
||||
if(!has_a(value))
|
||||
return;
|
||||
node* node_ptr = head;
|
||||
if(node_ptr->value == value) {
|
||||
//cout << "first" << endl;
|
||||
node* ptr = head;
|
||||
head = head->next;
|
||||
delete ptr;
|
||||
return;
|
||||
}
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->next == NULL)
|
||||
return;
|
||||
if(node_ptr->next->value == value) {
|
||||
node* ptr = node_ptr->next;
|
||||
node_ptr->next = node_ptr->next->next;
|
||||
delete ptr;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
// cout << *this << endl << endl;
|
||||
}
|
||||
|
||||
void linkedlist::clear() {
|
||||
// cout << "clear()" << endl;
|
||||
node* node_ptr = head;
|
||||
while(head) {
|
||||
node_ptr = head;
|
||||
head = head->next;
|
||||
delete node_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
int linkedlist::at(int index) {
|
||||
// cout << *this << endl;
|
||||
// cout << "at(" << index << ")" << endl;
|
||||
if(index < 0) {
|
||||
//cout << "is it zero" << endl;
|
||||
return -1;
|
||||
}
|
||||
if(size() <= index) {
|
||||
// cout << "this is the index=" << index << " size(" << size() << ")"<<endl;
|
||||
return -1;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
for(int i = 0; i < index; i++){
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
if(head == NULL)
|
||||
return -1;
|
||||
return node_ptr->value;
|
||||
}
|
||||
|
||||
int linkedlist::size() {
|
||||
int size_of_list = 0;
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
size_of_list++;
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
// cout << "size(" << size_of_list << ")" << endl;
|
||||
return size_of_list;
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, linkedlist & f) {
|
||||
node* node_ptr = f.head;
|
||||
while(node_ptr != NULL) {
|
||||
os << node_ptr->value << ",";
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef __LINKEDLIST_H__
|
||||
#define __LINKEDLIST_H__
|
||||
#include <iostream>
|
||||
#include "LinkedListInterface.h"
|
||||
#include "node.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class linkedlist : public LinkedListInterface {
|
||||
public:
|
||||
linkedlist();
|
||||
~linkedlist();
|
||||
node* head;
|
||||
bool has_a(int value);
|
||||
bool is_it_zero(int value);
|
||||
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();
|
||||
friend ostream & operator<<(ostream & os, linkedlist & f);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include "linkedlist.h"
|
||||
#include "node.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
node* head = new node(0, head);
|
||||
node* tom = new node(1, NULL);
|
||||
node* dick = new node(2, NULL);
|
||||
head->next = tom;
|
||||
tom->next = dick;
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
cout << node_ptr->value;
|
||||
if(node_ptr != NULL) {
|
||||
cout << "====>";
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
cout << endl;
|
||||
cout << dick->next << endl;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "node.h"
|
||||
|
||||
node::node(const int value, node* next) : value(value), next(next) {}
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef __NODE_H__
|
||||
#define __NODE_H__
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
public:
|
||||
node(const int, node*);
|
||||
int value;
|
||||
node* next;
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
#include "linkedlist.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
linkedlist l;
|
||||
l.insertTail(69);
|
||||
l.insertHead(28);
|
||||
l.insertHead(1);
|
||||
l.insertHead(2);
|
||||
l.insertHead(4);
|
||||
l.insertHead(30);
|
||||
l.insertHead(15);
|
||||
l.insertTail(68);
|
||||
l.insertHead(9);
|
||||
l.insertHead(0);
|
||||
l.insertHead(7);
|
||||
l.insertHead(42);
|
||||
l.insertHead(3);
|
||||
cout << l << endl;
|
||||
l.insertAfter(13,30);
|
||||
cout << l << endl;
|
||||
l.remove(13);
|
||||
cout << endl << l << endl << "removed 13" << endl;;
|
||||
l.remove(4);
|
||||
cout << endl << l << endl << "removed 4" << endl;;
|
||||
l.remove(68);
|
||||
cout << endl << l << endl << "removed 68" << endl;;
|
||||
l.remove(3);
|
||||
cout << endl << endl;
|
||||
cout << endl << l << endl << "removed 3" << endl;;
|
||||
l.remove(0);
|
||||
cout << endl << l << endl << "removed 9" << endl;;
|
||||
l.remove(69);
|
||||
cout << endl << l << endl << "removed 69" << endl;;
|
||||
}
|
||||
Reference in New Issue
Block a user