线性表的顺序存储是将线性表中的元素存放在一组连续的存储单元中。使得在线性表中逻辑上相邻的元素在物理存储单元上也是连续的。采用顺序存储的线性表叫做顺序表。
线性表的顺序存储结构如下:
模块化设计:
头文件 结构体和相应函数的定义,声明
#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();//0 int 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--; //删除结束 数目减1 return 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; } } }
运行结果如下:
时间: 2024-10-10 00:42:29