|
| 1 | + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | + # .---. .----------- |
| 3 | + # / \ __ / ------ |
| 4 | + # / / \( )/ ----- (`-') _ _(`-') <-. (`-')_ |
| 5 | + # ////// '\/ ` --- ( OO).-/( (OO ).-> .-> \( OO) ) .-> |
| 6 | + # //// / // : : --- (,------. \ .'_ (`-')----. ,--./ ,--/ ,--.' ,-. |
| 7 | + # // / / / `\/ '-- | .---' '`'-..__)( OO).-. ' | \ | | (`-')'.' / |
| 8 | + # // //..\\ (| '--. | | ' |( _) | | | | . '| |)(OO \ / |
| 9 | + # ============UU====UU==== | .--' | | / : \| |)| | | |\ | | / /) |
| 10 | + # '//||\\` | `---. | '-' / ' '-' ' | | \ | `-/ /` |
| 11 | + # ''`` `------' `------' `-----' `--' `--' `--' |
| 12 | + # ######################################################################################################## |
| 13 | + # |
| 14 | + # Author: edony - edonyzpc@gmail.com |
| 15 | + # |
| 16 | + # twitter : @edonyzpc |
| 17 | + # |
| 18 | + # Last modified: 2014-11-28 15:25 |
| 19 | + # |
| 20 | + # Filename: linklist.h |
| 21 | + # |
| 22 | + # Description: All Rights Are Reserved |
| 23 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 24 | +#ifndef __LINKLIST_H__ |
| 25 | +#define __LINKLIST_H__ |
| 26 | +#include <stdlib.h> |
| 27 | +namespace myalg |
| 28 | +{ |
| 29 | + template <typename T = int> |
| 30 | + //define the list member |
| 31 | + struct l_mem |
| 32 | + { |
| 33 | + T mem_data; |
| 34 | + int position; |
| 35 | + l_mem *next; |
| 36 | + }; |
| 37 | + |
| 38 | + template <typename T1 = int> |
| 39 | + //define the linklist class |
| 40 | + class linklist |
| 41 | + { |
| 42 | + private: |
| 43 | + //sieze of the list |
| 44 | + int size; |
| 45 | + |
| 46 | + //pointer pointer to member of the list |
| 47 | + l_mem<T1> *head; |
| 48 | + l_mem<T1> *tail; |
| 49 | + |
| 50 | + public: |
| 51 | + //default constructor |
| 52 | + linklist() |
| 53 | + { |
| 54 | + this->size = 0; |
| 55 | + this->head = new l_mem<T1>; |
| 56 | + head->position = 0; |
| 57 | + this->tail = new l_mem<T1>; |
| 58 | + head->position = this->size; |
| 59 | + } |
| 60 | + |
| 61 | + //copy constructor |
| 62 | + linklist(const linklist& other_list) |
| 63 | + { |
| 64 | + this->size += other_list.getsize(); |
| 65 | + this->head = new l_mem<T1>; |
| 66 | + //this->head = (l_mem*)malloc(sizeof *head); |
| 67 | + *head = *(other_list.head); |
| 68 | + this->tail = new l_mem<T1>; |
| 69 | + //this->tail = (l_mem*)malloc(sizeof *tail); |
| 70 | + *tail = *(other_list.tail); |
| 71 | + this->size = other_list.getsize(); |
| 72 | + } |
| 73 | + |
| 74 | + //destructor |
| 75 | + ~linklist() |
| 76 | + { |
| 77 | + delete head; |
| 78 | + //free(head); |
| 79 | + delete tail; |
| 80 | + //free(head); |
| 81 | + } |
| 82 | + |
| 83 | + //insert the next member after the member ins_pos, |
| 84 | + //-1 for the failing to insert and 0 for successing to insert |
| 85 | + int linklist_ins_next(l_mem<T1> *ins_pos, T1 ins_data) |
| 86 | + { |
| 87 | + l_mem<T1> *ins = new l_mem<T1>; |
| 88 | + if(ins == NULL) return -1; |
| 89 | + ins->mem_data = ins_data; |
| 90 | + |
| 91 | + ins->mem_data = ins_data; |
| 92 | + if(ins_pos == NULL)//insert at the head for default |
| 93 | + { |
| 94 | + if(this->getsize() == 0) |
| 95 | + { |
| 96 | + this->tail = ins; |
| 97 | + } |
| 98 | + ins->next = this->head; |
| 99 | + this->head = ins; |
| 100 | + ins->position = this->head + 1; |
| 101 | + this->size++; |
| 102 | + this->tail = this->size; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + ins->next = ins_pos->next; |
| 107 | + ins_pos->next = ins; |
| 108 | + if(ins->next == NULL) |
| 109 | + { |
| 110 | + this->tail = ins; |
| 111 | + } |
| 112 | + ins->position = ins_pos->position + 1; |
| 113 | + this->size++; |
| 114 | + this->tail = this->size; |
| 115 | + } |
| 116 | + return 0; |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + //remove the member |
| 121 | + int rm_next(l_mem<T1> *rm_pos) |
| 122 | + { |
| 123 | + if(rm_pos == NULL || rm_pos->next ==NULL) return -1;//error removement |
| 124 | + |
| 125 | + rm_pos->next = rm_pos->next->next; |
| 126 | + this->size--; |
| 127 | + l_mem<T1> * tmp = new l_mem<T1>; |
| 128 | + tmp = rm_pos->next; |
| 129 | + while(tmp != NULL) |
| 130 | + { |
| 131 | + tmp->position--; |
| 132 | + tmp = tmp->next; |
| 133 | + } |
| 134 | + delete tmp; |
| 135 | + return 0;//finish removement |
| 136 | + } |
| 137 | + |
| 138 | + //find the mem |
| 139 | + int find_mem(T1 find_data, l_mem<T1> * pop_mem) |
| 140 | + { |
| 141 | + l_mem<T1> * tmp = new l_mem<T1>; |
| 142 | + if(tmp == NULL) return -1; |
| 143 | + tmp = this->head->next; |
| 144 | + while(tmp != NULL) |
| 145 | + { |
| 146 | + if(tmp->mem_data != find_data) |
| 147 | + { |
| 148 | + tmp = tmp->next; |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + pop_mem = tmp; |
| 153 | + return 0; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + inline int getsize(){return size;} |
| 159 | + }; |
| 160 | +} |
| 161 | +#endif |
| 162 | + |
0 commit comments