Algorithm Data Structure sequence table Implementation + operation and problem analysis, algorithm Data Structure

Source: Internet
Author: User

Algorithm Data Structure sequence table Implementation + operation and problem analysis, algorithm Data Structure

The sequential storage of a linear table stores the elements in a linear table in a group.Continuous. The logical Adjacent Elements in a linear table are continuous in physical storage units. UseSequential StorageA linear table is called a sequence table.



The sequential storage structure of linear tables 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/* struct used to store basic information of the sequence table */typedef struct SeqList {ElemType * base; size_t capacity; // capacity of the sequence table size_t size;} SeqList; int begin (); // 0int end (SeqList * list); // void menu () at the end of the table (); // menu function void InitSeqList (SeqList * list); // initialization function bool isempty (SeqList * list); // determines whether empty bool isfull (SeqList * list ); // determine whether bool push_back (SeqList * list, ElemType x) is full; // The bool push_front (SeqList * list, ElemType x) function in the end ); // bool pop_back (SeqList * list); // bool pop_front (SeqList * list); // bool insert_pos (SeqList * list, ElemType key, elemType x); // insert bool delete_val (SeqList * list, ElemType key) in the position; // Delete void show (SeqList * list) by value ); // display function int find (SeqList * list, ElemType key); // query function // bool delete_val (SeqList * list, ElemType key); bool delete_pos (SeqList * list, elemType key); // Delete int getvalue (SeqList * list, ElemType key); // obtain the bool modify (SeqList * list, ElemType key, ElemType x) of a position ); // modify the void clear (SeqList * list) function; // clear the void destroy (SeqList * list) sequence table; // destroy the sequence table int length (SeqList * list ); // return the length of the table int next (SeqList * list, ElemType key); // find the next int pro (SeqList * list, ElemType key) in a certain position ); // find the precursor void sort (SeqList * list) at a certain position; // sort (bubble) int insert_val_pos (SeqList * list, ElemType x ); // insert by value (return position) void resver (SeqList * list); // flip function # endif

# Include "SeqList. h "/* function * // * menu */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:>";}/* Initialize sequence 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 whether it is empty */bool isempty (SeqList * list) {return end (list) = 0;}/* determine whether it is full */bool isfull (SeqList * list) {return end (list) >=list-> capacity;}/* Insert at the end */bool push_back (SeqList * list, ElemType x) {return insert_pos (list, end (list), x);}/* Delete the header */bool push_front (SeqList * list, ElemType x) {return insert_pos (list, begin (), x );} /* B Ool 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 deletion */bool pop_back (SeqList * list) {if (isempty (list) return false; list-> size --; // delete the end number minus 1 return true ;}/ * Header insertion */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 the end number minus 1 return true;}/* Insert position */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 [k Ey] = 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 position */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;}/* Delete location */boo L 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 (I Nt k = I; k <list-> size-1; ++ k) // {// list-> base [k] = list-> base [k + 1]; //} // list-> size --; // return true; ///}/* obtain the value of the input position */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 ;} /* modify the value of a position */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;}/* clear sequence table */void clear (SeqList * list) {list-> size = 0 ;} /* destroy sequence table */void destroy (SeqList * list) {if (list-> base) free (list-> base); list-> size = 0; list-> capacity = 0;}/* get the table length */int length (SeqList * list) {if (list-> capacity = NULL) {c Out <"INPUT_ERROR"; return ERROR;} return end (list);}/* return the successor to the output position */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 frontend of the output position */int pro (SeqList * list, ElemType key) {if (isempty (list) | key <1 | key> = end (list) {cout <"INPUT_ERROR"; return ERROR;} return list-> base [key-1];} /* sort in ascending order Order */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 According to the value size (return position), and then use insert_pos to implement the insert function */int insert_val_pos (SeqList * list, ElemType X) // only the insert position {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 ;}} /* flip sequence 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 ;}} /* display */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;}}}

The running result is as follows:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.