C + + to implement static order table and delete and modify and initialize

Source: Internet
Author: User

C + + to implement static order table and delete and modify


Sequential table: The linear structure of the data element is stored in a contiguous storage unit according to S, which is a kind of linear table.


seqlist.h#pragma once#include <assert.h> #include  <string.h> #define  max_size  5typedef int datatype;//Define order table structure body typedef struct seqlist{datatype array[max_size] ;   //Data Block Array size_t size;                //the current number of valid data}seqlist;//the declaration void initseqlist (SEQLIST*&NBSP;PSEQ) about the sequential table function; Void pushback ( SEQLIST*&NBSP;PSEQ,DATATYPE&NBSP;X); Void popback (SEQLIST*&NBSP;PSEQ); Void pushfront (SeqList*  PSEQ,DATATYPE&NBSP;X); Void popfront (SEQLIST*&NBSP;PSEQ); void printseqlist (SEQLIST*&NBSP;PSEQ); void  insert (seqlist* pseq, size_t pos, datatype x); Int find (SeqList* pSeq, &NBSP;DATATYPE&NBSP;X); Void erase (Seqlist* pseq, size_t pos); Void Remove (SeqList* &NBSP;PSEQ,&NBSP;DATATYPE&NBSP;X); Void removeall (seqlist* pseq, datatype x);//Initialize void  initseqlist (seqlist*&NBSP;PSEQ) {assert (PSEQ), memset (pseq->array, 0, sizeof (DataType) *max_size);p seq->size =  0;} Tail plug Void pushback (seqlist* pseq, datatype x) {assert (PSEQ);if  (pseq->size > = max_size) {cout <<  "the seqlist is full!"  << endl;return;} Pseq->array[pseq->size++] = x;} Tail Delete void popback (seqlist* pseq) {assert (PSEQ);if  (pseq->size <= 0) {cout  <<  "the seqlist is empty!"  << endl;return;} pseq->array[--pseq->size] = 0;} Head plug Void pushfront (seqlist* pseq, datatype x) {assert (PSEQ);D atatype begin =  pSeq->size - 1;if  (pseq->size >= max_size) {cout <<  " the seqlist is full! "  << endl;return;} for  (; begin >= 0;--begin) {pseq->array[begin + 1] = pSeq->array[begin];} Pseq->array[0] = x;++pseq->size;} Header Delete Void popfront (SEQLIST*&NBSP;PSEQ) {assert (PSEQ);D atatype begin = 0;if  (pSeq-> size <= 0) {cout <<  "the seqlist is empty!"  << endl;return;} for  (; begin <= pseq->size;++begin) {Pseq->array[begin] = pseq->array[begin &NBSP;+&NBSP;1];} Pseq->array[pseq->size] = 0;--pseq->size;} Print Void printseqlist (seqlist* pseq) {assert (PSEQ);D atatype i = 0;for  (;i  < pseq->size;++i) {cout << pseq->array[i]<< " ";} Cout << endl;} Modifies data at a location Void insert (seqlist* pseq, size_t pos, datatype x) {assert (PSEQ);D atatype begin = pseq->size;if  (pos >= pseq->size) {cout <<   "the pos is wrong!"  << endl;return;} If  (pseq->size >= max_size) {cout <<  "the seqlist is full!"  << endl;return;} for  (; begin >= pos;--begin) {pseq->array[begin] = pseq->array[begin - &NBSP;1];} Pseq->array[pos - 1] = x;++pseq->size;} Find Int find (seqlist* pseq, datatype x) {assert (PSEQ);int i = 0;for  (; I  < pseq->size;++i) {if  (pseq->array[i] == x) {return i;}} return -1;    //indicates that no x}//was found to delete the data at a location void erase (seqlist* pseq, size_t  pos) {assert (PSEQ);//datatype begin = pseq->size -1;if  (pos >=  pseq->size) {cout <<  "the pos is wrong!"  << endl;return;} if  (pseq->size <= 0) {cout <<  "the seqlist is empty!"  << endl;return;} for  (;pOs < pseq->size;++pos) {pseq->array[pos] = pseq->array[pos + 1];} --pseq->size;} Delete Data Void remove (seqlist* pseq, datatype x) {assert (PSEQ) with the first value x in the order table; int pos =  0;pos = find (pseq, x);if  (pos != -1) {Erase (Pseq, pos);}} Delete Data Void removeall (seqlist* pseq, datatype x) {assert (PSEQ) for all values x in the order table; int pos =  0;pos = find (pseq, x);while  (pos != -1) {Erase (pseq, pos);p os =  find (pseq, x);}} test.cpp   #define  _CRT_SECURE_NO_WARNINGS 1#include <iostream>using  namespace std; #include   "SeqList.h"//test Tail plug Void test1 () {seqlist seq;initseqlist (&AMP;SEQ); Pushback (&seq, 1); Pushback (&AMP;SEQ,&NBSP;2); Pushback (&seq, 3); Pushback (&seq, 4); Pushback (&seq, 5); Pushback (&AMP;SEQ,&NBSP;6); Printseqlist (&AMP;SEQ);} Test Head Plug Delete void test2 () {SeQlist seq;initseqlist (&AMP;SEQ); Pushback (&seq, 1); Pushback (&AMP;SEQ,&NBSP;2); Pushback (&seq, 3); Pushback (&seq, 4); Pushfront (&seq, 0); Pushfront (&seq, -1); Printseqlist (&AMP;SEQ); Popback (&AMP;SEQ); Popback (&AMP;SEQ); Popback (&AMP;SEQ); Popback (&AMP;SEQ); Popback (&AMP;SEQ); Popback (&AMP;SEQ); Printseqlist (&AMP;SEQ);} The test modifies the data at a location Void test3 () {seqlist seq;initseqlist (&AMP;SEQ); Pushback (&seq, 1); Pushback (&AMP;SEQ,&NBSP;2); Pushback (&seq, 4); Pushback (&seq, 5); Insert (&seq, 3, 3); Printseqlist (&AMP;SEQ);} Test Find and delete Void test4 () {seqlist seq;initseqlist (&AMP;SEQ); Pushback (&seq, 1); Pushback (&AMP;SEQ,&NBSP;2); Pushback (&seq, 3); Pushback (&seq, 4); Printseqlist (&AMP;SEQ); Int ret = find (&seq, 2);cout <<  "POS:"   << ret << endl; Erase (&seq, 3); Printseqlist (&AMP;SEQ);} Test the first data in the Delete order table with the value x VOID&NBSP;TEST5 (){seqlist seq;initseqlist (&AMP;SEQ); Pushback (&seq, 1); Pushback (&AMP;SEQ,&NBSP;2); Pushback (&seq, 3); Pushback (&seq, 4); Printseqlist (&AMP;SEQ); Remove (&AMP;SEQ,&NBSP;2); Printseqlist (&AMP;SEQ);} Int main () {//test1 ();//test2 ();//test3 ();//test4 (); TEST5 (); System ("pause"); return 0;}

If you have any questions, please advise me.


This article is from the "clown" blog, make sure to keep this source http://clown5.blog.51cto.com/10730975/1753486

C + + to implement static order table and delete and modify and initialize

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.