GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Data-Structure-Algorithm/DynamicArray.cpp at main · aruncode49/Data-Structure-Algorithm Expand file tree/ Copy pathDynamicArray.cppMore file actions 193 lines (175 loc) · 4.18 KB / Copy pathDynamicArray.cppFile metadata and controls193 lines (175 loc) · 4.18 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 #include<stdio.h> #include<iostream> using namespace std; class DynArray { private: int capacity; int lastIndex; int *p; protected: void doubleArray(); void halfArray(); public: DynArray(int); // parametrised constructor DynArray(DynArray &); // copy constructor for deep copy DynArray& operator=(DynArray&); // copy assignment operator for deep copy bool is_empty(); // is empty function for checking empty bool is_full(); // is full function for checking full void append(int); // append function for adding element at last index void insert(int,int); // insert function for inserting element at particular index void edit(int,int); // edit function for edit element at particular index void del(int); // delete function for deleting element at particular index int getItem(int); // get item function for get element at particular index int count(); // count function for counts the number of elements present inside array int getElementIndex(int); // for getting index at particular index ~DynArray(); // destructor }; void DynArray::doubleArray() { int *temp; temp = new int[capacity*2]; for(int i = 0; i < lastIndex; i++) temp[i] = p[i]; delete []p; p = temp; capacity *= 2; } void DynArray::halfArray() { int *temp; temp = new int[capacity/2]; for(int i = 0; i < lastIndex; i++) temp[i] = p[i]; delete []p; p = temp; capacity /=2; } DynArray& DynArray::operator=(DynArray& arr) { capacity = arr.capacity; lastIndex = arr.lastIndex; if(p != NULL) delete []p; p = new int[capacity]; for(int i = 0; i < lastIndex; i++) p[i] = arr.p[i]; return (*this); } DynArray::DynArray(DynArray &arr) { capacity = arr.capacity; lastIndex = arr.lastIndex; if(p != NULL) delete []p; p = new int[capacity]; for(int i = 0; i < lastIndex; i++) p[i] = arr.p[i]; } DynArray::DynArray(int) { p=NULL; } DynArray::DynArray (int size) { capacity = size; lastIndex = -1; if(p != NULL) delete []p; p = new int[capacity]; } bool DynArray::is_empty() { return (lastIndex == -1); } bool DynArray::is_full() { return (lastIndex+1 == capacity); } void DynArray::append(int data) { if(is_full()) doubleArray(); lastIndex++; p[lastIndex]=data; } void DynArray::insert(int index, int data) { int i; try{ if(index<0 || index>lastIndex+1) throw 1; if(is_full()) doubleArray(); for(i = lastIndex; i>=index; i--) p[i+1] = p[i]; p[index] = data; lastIndex++; } catch(int e){ if(e==1) cout<<"Invalid Index"; } } void DynArray::edit(int index, int data) { try{ if(index<0 || index > lastIndex+1) throw 1; p[index] = data; } catch(int e) { if(e==1) cout<<"Invalid Index"; } } void DynArray::del(int index) { int i; try{ if(index < 0 || index > lastIndex) throw 1; for(i = index; i < lastIndex; i++) p[i] = p[i+1]; lastIndex--; if(capacity > 1 && lastIndex+1 < capacity/2) halfArray(); } catch(int e){ if(e==1) cout<<"Invalid Index"; } } int DynArray::getItem(int index) { try{ if(index < 0 || index > lastIndex) throw 1; return p[index]; } catch(int e){ if(e==1) cout<<"Invalid Index"; } return -1; } int DynArray::count() { return lastIndex+1; } int DynArray::getElementIndex(int element) { int i; for(i=0; i<=lastIndex; i++) { if(p[i] == element) return i; else return -1; } } DynArray::~DynArray() { delete []p; } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |