数据结构之线性表
1.线性表的顺序表示
1 //线性表的顺序表示(c++) 2 #include <iostream> 3 using namespace std; 4 const int list_size = 100; 5 const int ex_size = 10; 6 typedef struct{ 7 int *h,size; 8 }sqlist; 9 void init(sqlist &l){//初始化线性表 10 l.h = new int[list_size + 1]; 11 *l.h = 0; 12 l.size = list_size; 13 } 14 void ex_list(sqlist &l){//增大线性表容量 15 l.size += ex_size; 16 int *p = new int[l.size]; 17 for(int i = 1; i <= *l.h; ++i) p[i] = l.h[i]; 18 delete [] l.h; 19 l.h = p; 20 } 21 bool empty(sqlist &l){//判断线性表是否为空 22 if(!(*l.h)) return true; 23 return false; 24 } 25 void destroy(sqlist &l){//销毁线性表(其实就是释放空间。。) 26 if(l.h != NULL) delete []l.h; 27 l.h = NULL; 28 l.size = 0; 29 } 30 void clear(sqlist &l){//清空线性表 31 *l.h = 0; 32 } 33 void display(sqlist &l){//打印线性表 34 if(empty(l)) cout << "The linear table is empty!" << endl; 35 else{ 36 for(int *p = l.h + 1; p <= l.h + *l.h; ++p) 37 cout << *p << " "; 38 cout << endl; 39 } 40 } 41 void merge_list(int *a, int l, int m, int r, int *t){//归并排序step2 42 int i = l, j = m + 1, x = m, y = r, k = 0; 43 while (i <= x && j <= y) 44 if (a[i] < a[j]) t[k++] = a[i++]; 45 else t[k++] = a[j++]; 46 while (i <= x) t[k++] = a[i++]; 47 while (j <= y) t[k++] = a[j++]; 48 for (i = 0; i < k; ++i) 49 a[l + i] = t[i]; 50 } 51 void merge_sort(int *a, int l, int r,int *t){//归并排序step1 52 if (l >= r) return; 53 int mid = (l + r) / 2; 54 merge_sort(a, l, mid, t); 55 merge_sort(a, mid + 1, r, t); 56 merge_list(a, l, mid, r, t); 57 } 58 void sort(sqlist &l){//归并排序 59 int *t = new int[l.size]; 60 merge_sort(l.h, 1, *l.h, t); 61 delete []t; 62 } 63 void merge(sqlist &l1,sqlist &l2,sqlist &l3){//合并线性表1和线性表2到线性表3 64 if(l3.size <= *l1.h + *l2.h){ 65 delete []l3.h; 66 l3.size = *l1.h + *l2.h + ex_size; 67 l3.h = new int[l3.size + 1]; 68 } 69 *l3.h = *l1.h + *l2.h; 70 int *i = l1.h + 1, *j = l2.h + 1, *k = l3.h + 1; 71 while(i <= l1.h + *l1.h && j <= l2.h + *l2.h) 72 if(*i < *j) *k++ = *i++; 73 else *k++ = *j++; 74 while(i <= l1.h + *l1.h) *k++ = *i++; 75 while(j <= l2.h + *l2.h) *k++ = *j++; 76 } 77 bool insert(sqlist &l, int i, int x){//插入操作 78 if(i < 1 || i > *l.h + 1) return false; 79 if(*l.h + 1 >= l.size) ex_list(l); 80 for(int *j = l.h + *l.h + 1; j > l.h + i; --j) *j = *(j-1); 81 *(l.h + i) = x; 82 ++(*l.h); 83 return true; 84 } 85 bool add(sqlist &l, int x){ 86 return insert(l, *l.h + 1, x); 87 } 88 bool del(sqlist &l,int i){//删除操作 89 if(i < 1 || i > *l.h) return false; 90 for(int *j = l.h + i + 1; j <= l.h + *l.h; ++j) *(j-1) = *j; 91 --(*l.h); 92 return true; 93 } 94 int *get(sqlist &l,int i){//返回第i个元素 95 if(i < 1 || i > *l.h) return NULL; 96 return l.h + i; 97 } 98 int *locate(sqlist &l,int i){//定位i的位置,以指针形式返回 99 for(int *p = l.h + 1; p <= l.h + *l.h; ++p) 100 if(*p == i) return p; 101 return NULL; 102 } 103 int *binary_locate(sqlist &l,int i){//二分定位,只能对已排序的线性表进行定位 104 int x = 1, y = *l.h; 105 while(x <= y){ 106 int *p = l.h + (x+y)/2; 107 if(*p == i) return p; 108 else if(*p < i) x = (x+y)/2 + 1; 109 else y = (x+y)/2 - 1; 110 } 111 return NULL; 112 } 113 int *prior(sqlist &l, int x){//返回x的前驱的指针 114 int *p = locate(l,x); 115 if(p == l.h + 1) p = NULL; 116 if(p != NULL) --p; return p; 117 } 118 int *binary_prior(sqlist &l, int x){//对于有序线性表可二分进行找前驱 119 int *p = binary_locate(l,x); 120 if(p == l.h + 1) p = NULL; 121 if(p != NULL) --p; return p; 122 } 123 int *next(sqlist &l, int x){//返回x的后继的指针 124 int *p = locate(l,x); 125 if(p == l.h + *l.h) p = NULL; 126 if(p != NULL) ++p; return p; 127 } 128 int *binary_next(sqlist &l, int x){//与前驱同理 129 int *p = binary_locate(l,x); 130 if(p == l.h + *l.h) p = NULL; 131 if(p != NULL) ++p; return p; 132 } 133 //以下为方便输出的函数。。。主要是控制格式和输出数据 134 void print_star(int x){ 135 cout << "*****************************" << endl; 136 if(x) cout << endl << endl; 137 } 138 void print_inf(sqlist &l,int i){ 139 cout << "线性表 " << i << " :长度为:" << *l.h << " 容量为:" << l.size << endl; 140 } 141 void print_list(sqlist &a,sqlist &b,sqlist &c){ 142 cout << endl; 143 cout << "-------线性表 1 数据-------" << endl; display(a); 144 cout << "-------线性表 2 数据-------" << endl; display(b); 145 cout << "-------线性表 3 数据-------" << endl; display(c); 146 cout << endl; 147 } 148 void print_information(sqlist &a,sqlist &b,sqlist &c){ 149 print_list(a,b,c); print_inf(a,1); print_inf(b,2); print_inf(c,3);cout << endl; 150 } 151 int main(){ 152 cout << endl; print_star(0); 153 cout << "-------建立顺序线性表-------" << endl; 154 sqlist a,b,c; init(a); init(b); init(c); 155 print_information(a,b,c); 156 cout << "-------线性表建立成功-------" << endl; 157 print_star(1); print_star(0); 158 cout << "-------数据插入与删除-------" << endl << endl; 159 cout << "-------数据的插入操作-------" << endl; 160 for(int i = 1; i < 11; ++i) 161 insert(a,i,11-i); 162 for(int i = 1; i < 11; ++i) 163 insert(b,i,i); 164 print_information(a,b,c); 165 cout << "-------数据插入成功!-------" << endl << endl; 166 cout << "-------数据的删除操作-------" << endl; 167 del(a,1); del(b,1); del(c,1); 168 print_information(a,b,c); 169 cout << "-------数据删除成功!-------" << endl; 170 print_star(1); print_star(0); 171 cout << "-------数据的相关查询-------" << endl; 172 cout << "线性表1中第1个元素为:" << get(a,1) << endl; 173 cout << "线性表1中第一个6是第 " << locate(a,6) - a.h << " 个元素" << endl; 174 cout << "线性表1中5的前驱为:" << *prior(a,5) << endl; 175 cout << "线性表1中5的后继为:" << *next(a,5) << endl; 176 cout << "-------数据查询完毕!-------" << endl; 177 print_star(1); print_star(0); 178 cout << "-------数据的归并排序-------" << endl; 179 cout << "初始数据:" << endl; 180 print_information(a,b,c); 181 sort(a); sort(b); 182 cout << "排序后数据:" << endl; 183 print_information(a,b,c); 184 cout << "排序后可以使用二分查找进行定位,如下:" << endl; 185 cout << "线性表1中第一个6是第 " << binary_locate(a,6) - a.h << " 个元素" << endl; 186 cout << "-------数据排序完毕!-------" << endl; 187 print_star(1); print_star(0); 188 cout << "-------多组数据的合并-------" << endl; 189 merge(a,b,c); 190 print_information(a,b,c); 191 cout << "-------数据合并成功!-------" << endl; 192 print_star(1); print_star(0); 193 cout << "-------数据清空和销毁-------" << endl; 194 cout << "初始数据:" << endl; 195 print_information(a,b,c); 196 clear(a); clear(b); clear(c); 197 cout << "清空后:" << endl; 198 print_information(a,b,c); 199 destroy(a); destroy(b); destroy(c); 200 cout << "销毁后:" << endl; 201 print_information(a,b,c); 202 print_star(0); 203 return 0; 204 }
2.动态链表
1 //动态链表(简单版。。。) 2 #include <cstdio> 3 #include <iostream> 4 using namespace std; 5 const int inf = 0x3f3f3f3f; 6 typedef struct node{ 7 int x; 8 struct node *next; 9 }node, *head; 10 head init(int x = 0){//初始化链表 11 head h = new node; 12 h->x = x; 13 h->next = NULL; 14 return h; 15 } 16 head tail(head &h){//返回尾部元素的头指针 17 head p = h; 18 while(p->next != NULL) p = p->next; 19 return p; 20 } 21 bool empty(head &h){//判断是否为空 22 if(h->x) return false; 23 return true; 24 } 25 int length(head &h){//返回链表长度 26 return h->x; 27 } 28 bool insert(head &h, int i, int x){//在i处插入x 29 if(i < 1 || i > h->x + 1) return false; 30 head p = h, t = init(x); 31 for(int j = 1; j < i; ++j) p = p->next; 32 t->next = p->next; 33 p->next = t; 34 ++h->x; 35 return true; 36 } 37 head insert(head &h, head &p, int x){//在p处插入x,输入多个数据时用 38 p->next = init(x); ++h->x; 39 return p->next; 40 } 41 bool add(head &h, int x){//尾部插入x 42 return insert(h, h->x + 1, x); 43 } 44 bool del(head &h, int i){//删除第i个元素 45 if(i < 1 || i > h->x) return false; 46 head t, p = h; 47 for(int j = 1; j < i; ++j) p = p->next; 48 t = p->next->next; 49 delete p->next; 50 p->next = t; 51 --h->x; 52 return true; 53 } 54 int *get(head &h, int i){//返回第i个元素 55 head p = h; 56 if(i < 1 || i > h->x) return NULL; 57 for(int j = 0; j < i; ++j) p = p->next; 58 return &(p->x); 59 } 60 void display(head &h){//display(h)打印长度+链表,display(h->next)打印链表 61 if(empty(h)) return; 62 head p = h; 63 while(p != NULL){ 64 cout << p->x << " "; 65 p = p->next; 66 } 67 cout << endl; 68 } 69 void input(head &h, int n){//在h链表中连续加入n个数 70 if(n < 1) return; 71 head p = tail(h); 72 while(n--){ 73 int x; 74 cin >> x; 75 p = insert(h,p,x); 76 } 77 } 78 void merge(head &h1, head &h2){//合并两个有序链表 79 h1->x += h2->x; 80 head p = h1, x = h1->next, y = h2->next; 81 while(x && y){ 82 if(x->x < y->x) { p->next = x; x = x->next; p = x; } 83 else { p->next = y; y = y->next; p = y; } 84 } 85 p->next = x ? x : y; 86 delete h2; 87 } 88 void destroy(head &h){//毁灭链表 89 if(!h) return; 90 destroy(h->next); 91 delete h; 92 h = NULL; 93 } 94 int main(){ 95 head h = init(); 96 add(h,1); add(h,2); add(h,3); display(h); 97 del(h,1); display(h); cout << *get(h,1) << endl; 98 input(h,3); display(h); 99 head t = init(); add(h,4); 100 merge(h,t); display(h); 101 return 0; 102 }