演算法資料結構 順序表的實現+操作 及對產生問題的分析,演算法資料結構
線性表的順序儲存是將線性表中的元素存放在一組連續的儲存單元中。使得線上性表中邏輯上相鄰的元素在實體儲存體單元上也是連續的。採用順序儲存的線性表叫做順序表。
線性表的順序儲存結構如下:
模組化設計:
標頭檔 結構體和相應函數的定義,聲明
#ifndef _SEQLIST_H#define _SEQLIST_H#include<iostream>#include<assert.h>//斷言#include<string.h>#include<stdlib.h>using namespace std;#define ElemType int#define SEQLIST_DEFAULT_SIZE 10#define ERROR -1/*結構體 用於存貯順序表基本資料*/typedef struct SeqList{ElemType *base;size_t capacity;//順序表的容量size_t size;}SeqList;int begin();//0int end(SeqList *list);//表尾void menu();//菜單函數void InitSeqList(SeqList *list);//初始化函數bool isempty(SeqList *list);//判斷是否 空bool isfull(SeqList *list);//判斷是否 滿bool push_back(SeqList *list, ElemType x);//尾插函數bool push_front(SeqList *list, ElemType x);//頭插函數bool pop_back(SeqList *list);//尾刪函數bool pop_front(SeqList *list);//頭刪函數bool insert_pos(SeqList *list, ElemType key, ElemType x);//位置插入bool delete_val(SeqList *list, ElemType key);//按值刪除void show(SeqList *list);//顯示函數int find(SeqList *list, ElemType key);//尋找函數//bool delete_val(SeqList *list, ElemType key);bool delete_pos(SeqList *list, ElemType key);//位置刪除int getvalue(SeqList*list, ElemType key);//擷取某位置的值bool modify(SeqList *list, ElemType key, ElemType x);//修改函數void clear(SeqList *list);//清空順序表void destroy(SeqList *list);//銷毀順序表int length(SeqList *list);//返回表長度int next(SeqList *list, ElemType key);//尋找某位置的後繼int pro(SeqList *list, ElemType key);//尋找某位置的前驅void sort(SeqList *list);//排序(冒泡)int insert_val_pos(SeqList *list, ElemType x);//按值插入(返回位置)void resver(SeqList *list);//翻轉函數#endif
#include"SeqList.h"/*各個功能函數*//*菜單*/void menu(){cout << " " << endl;cout << "**********************************" << endl;cout << "* [1] push_back [2] push_front *" << endl;cout << "* [3] show_seqlist[0] quit_system*" << endl;cout << "* [4] pop_back [5] pop_front *" << endl;cout << "* [6] insert_pos [7] insert_val *" << endl;cout << "* [8] delete_pos [9] delete_val *" << endl;cout << "* [10] find [11]getvalue *" << endl;cout << "* [12] modify [13]clear *" << endl;cout << "* [14] destroy [15]sort *" << endl;cout << "* [16] resver [17]length *" << endl;cout << "* [18] next [19]prio *" << endl;cout << "**********************************" << endl;cout << "plese chose:>";}/*初始化順序表*/void InitSeqList(SeqList *list){list->capacity = SEQLIST_DEFAULT_SIZE;list->base = (ElemType*)malloc(sizeof(ElemType)*list->capacity);assert(list->base != NULL);list->size = 0;}int begin(){return 0;}int end(SeqList *list){return list->size;}/*判斷是否為空白*/bool isempty(SeqList *list){return end(list) == 0;}/*判斷是否滿*/bool isfull(SeqList *list){return end(list) >= list->capacity;}/*尾部插入*/bool push_back(SeqList *list, ElemType x){return insert_pos(list, end(list), x);}/*頭部刪除*/bool push_front(SeqList *list, ElemType x){return insert_pos(list, begin(), x);}/*bool push_back(SeqList *list,ElemType x){if(isfull(list))return false;list->base[list->size++] = x;return true;}bool push_front(SeqList *list,ElemType x){if(isfull(list))return false;for(int i=list->size; i>0; --i){list->base[i] = list->base[i-1];}list->base[0] = x;list->size++;return true;}*//*尾部刪除*/bool pop_back(SeqList *list){if (isempty(list))return false;list->size--;//刪除結束 數目減1 return true;}/*頭部插入*/bool pop_front(SeqList *list){if (isempty(list))return false;for (int i = begin(); i<end(list) - 1; ++i){list->base[i] = list->base[i + 1];}list->size--;//刪除結束 數目減1return true;}/*位置插入*/bool insert_pos(SeqList *list, ElemType key, ElemType x){if (isfull(list) || key < begin() || key > end(list))return false;for (int i = end(list); i > key; --i){list->base[i] = list->base[i - 1];}list->base[key] = x;list->size++;return true;}/*按值刪除*/bool delete_val(SeqList *list, ElemType key){int pos = find(list, key);if (pos == -1)return false;delete_pos(list, pos);return true;}/*查詢輸入位置的值*/int find(SeqList *list, ElemType x){if (isempty(list)){cout << "INPUT_ERROR ";return ERROR;}for (int i = begin(); i<end(list); i++){if (list->base[i] == x)return i;}cout << "INPUT_ERROR ";return ERROR;}/*位置刪除*/bool delete_pos(SeqList *list, ElemType key){if (isempty(list) || key < begin() || key > end(list) - 1)return false;for (int i = key; i<end(list) - 1; i++){list->base[i] = list->base[i + 1];}list->size--;return true;}//bool delete_val(SeqList *list, ElemType key)//{// int i = 0;// for(int i=0; i<list->size; ++i)// {// if(list->base[i] == key)// break;// }// if(i >= list->size)// return false;// for(int k=i; k<list->size-1; ++k)// {// list->base[k] = list->base[k+1];// }// list->size--;// return true;//}/*擷取輸入位置的值*/int getvalue(SeqList *list, ElemType key){if (isempty(list) || key < begin() || key > end(list)){cout << "INPUT_ERROR ";return ERROR;}for (int i = begin(); i<end(list); i++){if (key == i)return list->base[i];}cout << "INPUT_ERROR ";return ERROR;}/*修改某位置的值*/bool modify(SeqList *list, ElemType key, ElemType x){if (isempty(list) || key < begin() || key > end(list) - 1)return false;for (int i = begin(); i<end(list); i++){if (key == i)list->base[i] = x;}return true;}/*清空順序表*/void clear(SeqList *list){list->size = 0;}/*銷毀順序表*/void destroy(SeqList *list){if (list->base)free(list->base);list->size = 0;list->capacity = 0;}/*擷取表長*/int length(SeqList *list){if (list->capacity = NULL){cout << "INPUT_ERROR ";return ERROR;}return end(list);}/*返回輸出位置的後繼*/int next(SeqList *list, ElemType key){if (isempty(list) || key < begin() || key >= end(list) - 1){cout << "INPUT_ERROR ";return ERROR;}return list->base[key + 1];}/*返回輸出位置的前驅*/int pro(SeqList *list, ElemType key){if (isempty(list) || key < 1 || key >= end(list)){cout << "INPUT_ERROR ";return ERROR;}return list->base[key - 1];}/*升序排序*/void sort(SeqList *list){for (int i = begin(); i < end(list); i++){for (int j = begin(); j < end(list) - i - 1; j++){if (list->base[j] > list->base[j + 1]){list->base[j] = list->base[j] ^ list->base[j + 1];list->base[j + 1] = list->base[j] ^ list->base[j + 1];list->base[j] = list->base[j] ^ list->base[j + 1];}}}}/*按照值的大小插入(返回位置),再通過 insert_pos 實現插入功能*/int insert_val_pos(SeqList *list, ElemType x)//只返回插入位置{cout << "the changes of SeqList will be incremental ! " << endl;sort(list);for (int i = begin(); i<end(list); ++i){if (x >= list->base[i] && x <= list->base[i + 1])return i + 1;}}/*翻轉順序表*/void resver(SeqList *list){int *top = &list->base [0];int *end = &list->base[list->size-1];for (; top < end; (top++ && end--)){*top = *top ^ *end;*end = *top ^ *end;*top = *top ^ *end;}}/*顯示*/void show(SeqList *list){for (int i = begin(); i< end(list); ++i){cout << list->base[i] << " ";}cout << endl;}
#include<iostream>#include"SeqList.h"using namespace std;int main(){SeqList mylist;InitSeqList(&mylist);int select = 1;ElemType item;int pos = 0;while (select){menu();cin >> select;switch (select){case 1:cout << "please enter data end with -1:>";while (cin >> item, item != -1){push_back(&mylist, item);}break;case 2:cout << "please enter data end with -1:>";while (cin >> item, item != -1){push_front(&mylist, item);}break;case 3:show(&mylist);break;case 4:pop_back(&mylist);break;case 5:pop_front(&mylist);break;case 6:cout << "please enter position(begin 0):>";cin >> pos;cout << "please enter insert data:>";cin >> item;insert_pos(&mylist, pos, item);break;case 7:cout << "please enter insert data:>";cin >> item;insert_pos(&mylist, insert_val_pos(&mylist, item), item);break;case 8:cout << "please enter position(begin 0):>";cin >> pos;delete_pos(&mylist, pos);break;case 9:cout << "please enter delete data:>";cin >> item;delete_val(&mylist, item);break;case 10:cout << "please enter data:>";cin >> item;cout << "the pos is:>" << find(&mylist, item) << endl;break;case 11:cout << "please enter position(begin 0):>";cin >> pos;getvalue(&mylist, pos);cout << "the data of pos is>" << getvalue(&mylist, pos) << endl;break;case 12:cout << "please enter position(begin 0):>";cin >> pos;cout << "please enter the modify data:>";cin >> item;modify(&mylist, pos, item);break;case 13:clear(&mylist);cout << "the SeqList was cleared !"<<endl;break; case 14: destroy(&mylist); cout<<"the SeqList was destroyed !"<<endl; cout << "After destroy ,any operation is invalid!" << endl; cout << "Please quit_system!" << endl; break;case 15:sort(&mylist);cout << "the changes of SeqList will be incremental ! " << endl;break;case 16:cout << "the changes of SeqList will be resver ! " << endl;resver(&mylist);break;case 17:length(&mylist);cout << "the length of SeqList is:>" << length(&mylist) << endl;break;case 18:cout << "please enter position(begin 0):>";cin >> pos;getvalue(&mylist, pos);cout << "the data of pos is>" << getvalue(&mylist, pos) << endl;next(&mylist, pos);cout << "the next of data is>" << next(&mylist, pos) << endl;break;case 19:cout << "please enter position(begin 0):>";cin >> pos;getvalue(&mylist, pos);cout << "the data of pos is>" << getvalue(&mylist, pos) << endl;next(&mylist, pos);cout << "the next of data is>" << pro(&mylist, pos) << endl;break;default:break;return 0;}}}
運行結果如下: