-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bc3c161
Showing
15 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.o | ||
*.zip | ||
*.tgz | ||
main | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |