Sequential storage of linear tables is the placement of elements in a linear table in a contiguous set of storage cells. So that logically adjacent elements in a linear table are also contiguous on the physical storage unit. Linear tables with sequential storage are called sequential tables.
The sequential storage structure of the linear table is as follows:
Modular Design:
header file struct and corresponding function definition, declaration
#ifndef _seqlist_h#define _seqlist_h#include<iostream> #include <assert.h>//assertion #include<string.h> #include <stdlib.h>using namespace std; #define Elemtype int#define seqlist_default_size 10#define error-1/* structural body For storage Order table basic information */typedef struct Seqlist{elemtype *base;size_t capacity;//sequential table capacity size_t size;} Seqlist;int begin ();//0int End (Seqlist *list);//Footer void menu ();//Menu function void Initseqlist (seqlist *list);//Initialize function bool IsEmpty (seqlist *list);//Determine whether the empty bool Isfull (seqlist *list);//Determine whether full bool Push_back (seqlist *list, Elemtype x);//tail interpolation function bool Push_front (seqlist *list, Elemtype x);//head interpolation function bool Pop_back (seqlist *list);//tail-delete function bool Pop_front (seqlist *list);// Header Delete function bool Insert_pos (seqlist *list, Elemtype Key, Elemtype x);//Position insert bool Delete_val (seqlist *list, Elemtype key);// Delete void show by value (Seqlist *list);//display function int find (seqlist *list, Elemtype key);//Find function//bool delete_val (seqlist *list, Elemtype key); bool Delete_pos (Seqlist *list, Elemtype key);//Position Delete int GetValue (seqlist*list, Elemtype key);//ReceivedTake the value of a position bool modify (seqlist *list, Elemtype Key, Elemtype x);//Modify function void Clear (seqlist *list);//empty Order table void Destroy (Seqlist * list);//Destroy Order table int Length (seqlist *list);//return table length int next (seqlist *list, Elemtype key);//Find the successor Int Pro (Seqlist *list, in a location) Elemtype key);//Find the precursor void sort (seqlist *list) for a position;//Sort (bubbling) int insert_val_pos (seqlist *list, Elemtype x);// Insert by value (return position) void Resver (Seqlist *list);//Flip function #endif
#include "SeqList.h"/* function functions *//* menus */void menu () {cout << "" << endl;cout << "************************ "<< endl;cout <<" * [1] push_back [2] Push_front * "<< endl;cout <<" * [3] Show_seqli St[0] quit_system* "<< endl;cout <<" * [4] pop_back [5] Pop_front * "<< endl;cout <<" * [6] in Sert_pos [7] insert_val * "<< endl;cout <<" * [8] delete_pos [9] delete_val * "<< endl;cout <<" * [Ten] Find [11]getvalue * << endl;cout << "* [] Modify [13]clear *" << endl;cout &L t;< "* [] destroy [15]sort *" << endl;cout << "* [+] resver [17]length *" << Endl ; cout << "* [] next [19]prio *" << endl;cout << "**********************************" <& Lt Endl;cout << "Plese chose:>";} /* Initialization Order table */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;} /* Determine if empty */bool isempty (seqlist *list) {return end (list) = = 0;} /* Determine if full */bool isfull (seqlist *list) {return end (list) >= list->capacity;} /* Trailing insert */bool push_back (seqlist *list, Elemtype x) {return insert_pos (list, end (list), x);} /* Header Delete */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;} *//* Tail Delete */bool pop_back (seqlist *list) {if (IsEmpty (list)) return false;list->size--;//Delete end number minus 1 return true; /* Header Insert */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--;//Delete End number minus 1return true;} /* location Insert */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;} /* Delete by value */bool delete_val (seqlist *list, elemtype key) {int pos = find (list, key); if (pos = =-1) return False;delete_pos (list, POS); return true;} /* Query the value of the input location */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;} /* location Delete */bool delete_pos (seqlist *list, Elemtype key) {if (IsEmpty (list) | | Key < BEGIN () | | key > End (list)-1) retur n false;for (int i = key; I<end (list)-1; i++) {List->base[i] = list->base[i + 1];} List->size--;return true;} boolDelete_val (seqlist *list, elemtype key)//{//int i = 0;//for (int i=0; i<list->size; ++i)//{//if (l Ist->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;//}/* Gets the value of the input position */i NT 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;} /* Modify the value of a location */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;} /* Empty Order Table */void Clear (seqlist *list) {list->size = 0;} /* Destroy Order table */void destroy (Seqlist *list) {if (list->base) FRee (list->base); list->size = 0;list->capacity = 0;} /* Get table length */int length (seqlist *list) {if (list->capacity = NULL) {cout << "Input_error"; return ERROR;} Return end (list);} /* Returns the output location of the successor */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];} /* Returns the output location of the precursor */int Pro (Seqlist *list, Elemtype key) {if (IsEmpty (list) | | Key < 1 | | Key >= End (list) {cout << "I Nput_error "; return ERROR;} return list->base[key-1];} /* Ascending sort */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 (return position) by the size of the value, and then implement the Insert function via Insert_pos */int insert_val_pos (seqlist *list, elemtyPE x)//Only return insert position {cout << "The changes of seqlist would 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;}} /* Flip Order table */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;}} /* Displays */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 dat A end with-1:> ";" while (CIN >> item, Item! =-1) {push_back (&mylist, item);} Break;case 2:cout << "Please enter data end with-1:>" and while (CIN >> item, Item! =-1) {Push_front (&myli St, 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 &G T;> Item;insert_pos (&mylist, POS, item) break;case 7:cout << "Please enter insert data:>"; Cin >> it Em;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); Case 10:cout << "Please enter data:>"; Cin >> item;cout << "The pos is:>" << Find (&mylis T, item) << endl;break;case 11:cout << "Please enter position (begin 0):>"; Cin >> Pos;getvalue (&m Ylist, POS); cout << "The data of POS is>" << getvalue (&mylist, POS) << endl;break;case 12:cout &L t;< "Please enter position (begin 0):>", cin >> pos;cout << "Please enter the Modify data:>"; Cin >&G T 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, no operation is invalid!" << Endl; cout << "Please quit_system!" << Endl; Break;case 15:sort (&mylist) cout << "THe changes of Seqlist'll be incremental! "<< endl;break;case 16:cout <<" The changes of Seqlist would be resver! "<< endl;resver (&mylist); b Reak;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 (Begi N 0):> "; Cin >> Pos;getvalue (&mylist, POS); cout <<" The data of POS is> "<< GetValue (&mylis T, POS) << endl;next (&mylist, POS); cout << "The next of Data is>" << Pro (&mylist, POS) <&L T Endl;break;default:break;return 0;}}}
The results of the operation are as follows:
Implementation of sequential table of algorithm data structure + operation and analysis of generating problems