Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon0 committed Nov 10, 2017
0 parents commit bc3c161
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.o
*.zip
*.tgz
main

58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
CXXFLAGS= --std=c++11
LDLIBS=
CC=g++

PROGRAM=main
OBJS=\
listAdd.o\
listDelete.o\
listDeleteSoft.o\
main.o\
merge.o\
mergeSort.o\
newList.o\
newNode.o\
newRecord.o\
printList.o

ARCHIVE=\
header.h\
listAdd.cc\
listDelete.cc\
listDeleteSoft.cc\
main.cc\
Makefile\
merge.cc\
mergeSort.cc\
newList.cc\
newNode.cc\
newRecord.cc\
printList.cc\
valgrind.txt


all: $(PROGRAM)

$(PROGRAM): $(PROGRAM).o $(OBJS)
$(PROGRAM).o:

clean:
$(RM) -v $(PROGRAM) *.o

test: $(PROGRAM)
./$(PROGRAM)

valgrind: $(PROGRAM)
valgrind --leak-check=full ./$(PROGRAM)

debug: CXXFLAGS += -g
debug: clean $(PROGRAM)

tar: mergesort.tgz
mergesort.tgz: $(ARCHIVE)
tar czvf $@ $^

zip: mergesort.zip
mergesort.zip: $(ARCHIVE)
zip $@ $^

Empty file added README.md
Empty file.
38 changes: 38 additions & 0 deletions header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef HEADER_H
#define HEADER_H
#include<cstddef>

struct node;
struct list;
struct record;

struct list {
int length;
node* first;
node* last;
};

struct node {
list* head;
node* next;
node* prev;
record* data;
};

struct record {
int value;
};

list* newList();
node* newNode(list*, record*);
record* newRecord(int);
void listAdd(list*, node*);
void listDelete(list*);
void listDeleteSoft(list*);
void printList(list*);

list* mergeSort(list*);
list* merge(list*, list*);

#endif

15 changes: 15 additions & 0 deletions listAdd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "header.h"

void listAdd(list* l, node* n) {
if(l->length == 0){
l->first = l->last = n;
l->length = 1;
}
else{
n->prev = l->last;
l->last->next = n;
l->last = n;
l->length++;
}
}

14 changes: 14 additions & 0 deletions listDelete.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "header.h"

void listDelete(list* l){
node* n = l->first;
while(n != NULL){
node* d = n;
n = n->next;
delete d->data;
delete d;
}

delete l;
}

14 changes: 14 additions & 0 deletions listDeleteSoft.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "header.h"

void listDeleteSoft(list* l){
node* n = l->first;
while(n != NULL){
node* d = n;
n = n->next;
delete d;
}

delete l;
}


21 changes: 21 additions & 0 deletions main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "header.h"

int main(){
int vals[] = {958, 257, 186, 164, 947, 341, 235, 320, 903, 632, 374};
list* l = newList();

for(int v: vals){
listAdd(l, newNode(l, newRecord(v)));
}

printList(l);

l = mergeSort(l);

printList(l);

listDelete(l);

return 0;
}

30 changes: 30 additions & 0 deletions merge.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "header.h"

list* merge(list* left, list* right) {
node* l = left->first;
node* r = right->first;
list* result = newList();

while(l != NULL && r != NULL){
if(l->data->value <= r->data->value){
listAdd(result, newNode(result, l->data));
l = l->next;
}
else{
listAdd(result, newNode(result, r->data));
r = r->next;
}
}

while(l != NULL){
listAdd(result, newNode(result, l->data));
l = l->next;
}
while(r != NULL){
listAdd(result, newNode(result, r->data));
r = r->next;
}

return result;
}

34 changes: 34 additions & 0 deletions mergeSort.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "header.h"

list* mergeSort(list* l){
if(l->length <= 1){
return l;
}

list* left = newList();
list* right = newList();

{
node* n = l->first;
int i = 0;
while(n != NULL){
if(i < l->length/2){
listAdd(left, newNode(left, n->data));
}
else{
listAdd(right, newNode(right, n->data));
}
n = n->next;
i++;
}
}

left = mergeSort(left);
right = mergeSort(right);
list* result = merge(left, right);
listDeleteSoft(left);
listDeleteSoft(right);
listDeleteSoft(l);
return result;
}

10 changes: 10 additions & 0 deletions newList.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "header.h"

list* newList() {
list* l = new list;
l->length = 0;
l->first = NULL;
l->last = NULL;
return l;
}

11 changes: 11 additions & 0 deletions newNode.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "header.h"

node* newNode(list* l, record* r){
node* n = new node;
n->data = r;
n->head = l;
n->next = NULL;
n->prev = NULL;
return n;
}

8 changes: 8 additions & 0 deletions newRecord.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "header.h"

record* newRecord(int v) {
record* r = new record;
r->value = v;
return r;
}

19 changes: 19 additions & 0 deletions printList.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "header.h"
#include <iostream>

void printList(list* l){
node* n = l->first;

if(n != NULL){
std::cout << n->data->value;
n = n->next;

while(n != NULL){
std::cout << ", " << n->data->value;
n = n->next;
}
std::cout << std::endl;

}
}

16 changes: 16 additions & 0 deletions valgrind.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
==4393== Memcheck, a memory error detector
==4393== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4393== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==4393== Command: ./main
==4393==
958, 257, 186, 164, 947, 341, 235, 320, 903, 632, 374
164, 186, 235, 257, 320, 341, 374, 632, 903, 947, 958
==4393==
==4393== HEAP SUMMARY:
==4393== in use at exit: 0 bytes in 0 blocks
==4393== total heap usage: 131 allocs, 131 frees, 3,636 bytes allocated
==4393==
==4393== All heap blocks were freed -- no leaks are possible
==4393==
==4393== For counts of detected and suppressed errors, rerun with: -v
==4393== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

0 comments on commit bc3c161

Please sign in to comment.