From 2c9b9a5a7b57163de6364b566fb7b6462bb8dc16 Mon Sep 17 00:00:00 2001 From: zedarider Date: Fri, 19 Feb 2016 15:09:45 +0000 Subject: Updated the game This is the final version of the 2048 game using pointers --- Algorithms/BubbleSort.cpp | 15 ++++ Algorithms/LinkedLists | Bin 0 -> 11320 bytes Algorithms/LinkedLists.cpp | 188 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 Algorithms/BubbleSort.cpp create mode 100644 Algorithms/LinkedLists create mode 100644 Algorithms/LinkedLists.cpp (limited to 'Algorithms') diff --git a/Algorithms/BubbleSort.cpp b/Algorithms/BubbleSort.cpp new file mode 100644 index 0000000..587b507 --- /dev/null +++ b/Algorithms/BubbleSort.cpp @@ -0,0 +1,15 @@ +/* +* @Author: yannherklotz +* @Date: 2016-02-08 23:26:59 +* @Last Modified by: yannherklotz +* @Last Modified time: 2016-02-08 23:26:59 +*/ + +#include + +using namespace std; + +int main(){ + + return 0; +} \ No newline at end of file diff --git a/Algorithms/LinkedLists b/Algorithms/LinkedLists new file mode 100644 index 0000000..21e8c09 Binary files /dev/null and b/Algorithms/LinkedLists differ diff --git a/Algorithms/LinkedLists.cpp b/Algorithms/LinkedLists.cpp new file mode 100644 index 0000000..360714e --- /dev/null +++ b/Algorithms/LinkedLists.cpp @@ -0,0 +1,188 @@ +/* +* @Author: yannherklotz +* @Date: 2016-02-08 22:20:52 +* @Last Modified by: yannherklotz +* @Last Modified time: 2016-02-09 12:43:15 +*/ + +#include + +using namespace std; + +typedef int list_t; + +struct linkNode { + list_t val; + linkNode* next; +}; + +void addel(list_t, linkNode*&); +void deallocate(linkNode*&); +void printListOpposite(linkNode*); +void printListCorrect(linkNode*); +int listLength(linkNode*); +int deleteItem(linkNode*&, list_t); +linkNode* concatenateList(linkNode*, linkNode*); + +int main(){ + linkNode* listA = NULL; + linkNode* listB = NULL; + linkNode* listC = NULL; + + int n; + list_t el; + + cout << "How many elements: "; + cin >> n; + + for(int i = 0; i < n; ++i) { + cout << "Enter element: "; + cin >> el; + + addel(el, listA); + } + printListOpposite(listA); + cout << endl; + printListCorrect(listA); + cout << "List Count: " << listLength(listA) << endl; + cout << "which element to remove? "; + cin >> n; + deleteItem(listA, n); + printListCorrect(listA); + + cout << "How many elements in list B: "; + cin >> n; + + for(int i = 0; i < n; ++i) { + cout << "Enter element: "; + cin >> el; + + addel(el, listB); + } + + printListCorrect(listB); + listC = concatenateList(listA, listB); + cout << "Concatenated list:" << endl; + printListCorrect(listC); + cout << "delete from a: "; + cin >> n; + deleteItem(listA, n); + cout << "delete from b: "; + cin >> n; + deleteItem(listB, n); + cout << endl << "A" << endl; + printListCorrect(listA); + cout << "B" << endl; + printListCorrect(listB); + cout << "A+B" << endl; + printListCorrect(listC); + + deallocate(listA); + deallocate(listB); + deallocate(listC); + return 0; +} + +void addel(list_t el, linkNode* &ln) { + linkNode* tmp = new linkNode; + tmp->val = el; + tmp->next = ln; + ln = tmp; +} + +void deallocate(linkNode* &ln) { + while(ln != NULL) { + linkNode* nextln = ln->next; + delete ln; + ln = nextln; + } +} + +void printListOpposite(linkNode* ln) { + while(ln != NULL) { + cout << ln->val << endl; + ln = ln->next; + } +} + +void printListCorrect(linkNode* ln) { + if(ln != NULL) { + printListCorrect(ln->next); + cout << ln->val << endl; + } +} + +int listLength(linkNode* ln) { + int count = 0; + while(ln != NULL) { + ++count; + ln = ln->next; + } + return count; +} + +int findItem(linkNode* ln, list_t el) { + int count = 0; + while(ln != NULL) { + if(ln->val == el) { + return count; + } + ++count; + } + return(-1); +} + +int deleteItem(linkNode* &ln, list_t el) { + linkNode* sp = ln; + linkNode* fp = ln->next; + + if(sp->val == el) { + ln = ln->next; + delete sp; + } else { + while(fp != NULL) { + if(fp->val == el) { + sp->next = fp->next; + delete fp; + return 1; + } + fp = fp->next; + sp = sp->next; + } + } + return 0; +} + +linkNode* concatenateList(linkNode* fl, linkNode* sl) { + linkNode* tmpsl = NULL; + linkNode* tmpfl = NULL; + + while(fl != NULL) { + addel(fl->val, tmpfl); + fl = fl->next; + } + while(sl != NULL) { + addel(sl->val, tmpsl); + sl = sl->next; + } + + linkNode* tmp = tmpsl; + while(tmp->next != NULL) { + tmp = tmp->next; + } + tmp->next = tmpfl; + return tmpsl; +} + +void reverseOrder(listNode* &ln) { + linkNode* sp = ln; + linkNode* tmp = ln; + ln = ln->next; + tmp->next = NULL; + while(ln->next != NULL) { + tmp = ln->next; + ln->next = sp; + sp = ln; + ln = tmp; + } +} \ No newline at end of file -- cgit