diff --git a/challenge_8/cpp/manuel/Makefile b/challenge_8/cpp/manuel/Makefile new file mode 100644 index 000000000..aa263e535 --- /dev/null +++ b/challenge_8/cpp/manuel/Makefile @@ -0,0 +1,28 @@ +SRC := $(wildcard src/*.cpp) +HEADERS := $(wildcard $(addprefix src/include/, *.h)) + +OBJDIR := obj +BINDIR := bin +OBJECTS := $(addprefix $(OBJDIR)/, $(notdir src/ $(SRC:.cpp=.o))) + +EXE := solution.exe +CC := g++ +CFLAGS := -Wall -c -std=c++11 +LFLAGS := -Wall + +$(EXE) : $(BINDIR) $(OBJDIR) $(OBJECTS) + @$(CC) $(LFLAGS) -o $(BINDIR)/$@ $(OBJECTS) + +obj/%.o : src/%.cpp $(HEADERS) + @$(CC) $(CFLAGS) -o $@ $< + +.PHONY : clean $(BINDIR) $(OBJDIR) + +clean : + @rm -rf $(BINDIR) $(OBJDIR) + +$(BINDIR) : + @mkdir -p $(BINDIR) + +$(OBJDIR) : + @mkdir -p $(OBJDIR) diff --git a/challenge_8/cpp/manuel/README.md b/challenge_8/cpp/manuel/README.md new file mode 100644 index 000000000..fe5874818 --- /dev/null +++ b/challenge_8/cpp/manuel/README.md @@ -0,0 +1,28 @@ +# Random Pointer Linked List +###### C++11 @manuel + +### 1. Approch to Solving the problem + +I assume that I am given a linked list that is prebuilt and the random +attribute always points to a random node, so I do not verify whether the +connections match. + +To make copies I map every node in the given list with a copy. + +I then iterate through my linked list and assign every attribute in +the copy to the corresponding copy of the given node. + +To test this I make a simple linked list containing all the letters +alphabet and make a copy of it to verify. + +### 2. How to compile and run this code + +``` +make +make clean +``` + +### 3. How this program works + +This program does not take any input data and prints out +the tests I run in the main method. diff --git a/challenge_8/cpp/manuel/src/LinkedList.cpp b/challenge_8/cpp/manuel/src/LinkedList.cpp new file mode 100644 index 000000000..3a8105554 --- /dev/null +++ b/challenge_8/cpp/manuel/src/LinkedList.cpp @@ -0,0 +1,47 @@ +#include +#include + +#include "include/LinkedList.h" + +LinkedList::Node * LinkedList::copy(Node *root) { + // Assumes passed in node is prebuilt + // linked list and copies the data on it + // onto a new linked list + + std::map copies; // Maps old nodes to their copy + + Node *temp = root; // Pointer to root node + + // Allocate new memory for copy + while(temp != NULL) { + copies[temp] = new Node(); + temp = temp->next; + } + + // Reset list + temp = root; + Node *copy; + + // Copy data + while(temp->next != NULL) { + copy = copies[temp]; + copy->next = copies[temp->next]; + copy->random = copies[temp->random]; + copy->data = temp->data + "'"; + temp = temp->next; + } + return copies[root]; +} + +void LinkedList::clean(Node *root) { + // Frees the used memory by nodes + Node *temp = root->next; + + while(temp != NULL) { + root->next = temp->next; + temp->next = NULL; + temp->random = NULL; + delete temp; + temp = root->next; + } +} diff --git a/challenge_8/cpp/manuel/src/include/LinkedList.h b/challenge_8/cpp/manuel/src/include/LinkedList.h new file mode 100644 index 000000000..77bd4485b --- /dev/null +++ b/challenge_8/cpp/manuel/src/include/LinkedList.h @@ -0,0 +1,18 @@ +#ifndef LINKEDLIST_H +#define LINKEDLIST_H + +class LinkedList { + + public: + + struct Node { + std::string data; + Node *next; + Node *random; + }; + + static Node * copy(Node *); + static void clean(Node *); +}; + +#endif diff --git a/challenge_8/cpp/manuel/src/main.cpp b/challenge_8/cpp/manuel/src/main.cpp new file mode 100644 index 000000000..49cd4e198 --- /dev/null +++ b/challenge_8/cpp/manuel/src/main.cpp @@ -0,0 +1,41 @@ +#include + +#include "include/LinkedList.h" + +int main(int argc, char **argv) { + LinkedList::Node *list = new LinkedList::Node(); + + // Build dummy list + LinkedList::Node *temp = list; + for(char letter = 'A'; letter <= 'Z'; letter++) { + temp->data = letter; + temp->next = new LinkedList::Node(); + temp = temp->next; + } + + // Iterate through list + temp = list; + while(temp != NULL) { + std::cout << temp->data << " "; + temp = temp->next; + } + std::cout << std::endl; + + // Make copy + LinkedList::Node *copy = LinkedList::copy(list); + + // Iterate through copy + temp = copy; + while(temp != NULL) { + std::cout << temp->data << " "; + temp = temp->next; + } + std::cout << std::endl; + + LinkedList::clean(list); // deallocate memory + LinkedList::clean(copy); + delete copy; + delete list; + return 0; +} +