Skip to content

[C++] Challenge 8 (Unreviewed) #371

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions challenge_8/cpp/manuel/Makefile
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions challenge_8/cpp/manuel/README.md
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 47 additions & 0 deletions challenge_8/cpp/manuel/src/LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <string>
#include <map>

#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<Node *, Node *> 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;
}
}
18 changes: 18 additions & 0 deletions challenge_8/cpp/manuel/src/include/LinkedList.h
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions challenge_8/cpp/manuel/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>

#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;
}