C + + Mode implementation Order table

Source: Internet
Author: User
Tags pos index

SeqList.h

#include <iostream>using namespace std; #define MAXSIZE 15typedef int datatype;typedef struct{datatype seqlist[ MAXSIZE]; Typically, an array is used to describe the data stored in the sequential table as an int size; /* Linear table length */} seqlist; Seqlist *init_seqlist (); Initialization order table void pushback (seqlist* seq, DataType x);//tail plug void Popback (seqlist* seq);//tail delete void Pushfront (seqlist* seq, DataType x);//head insert void Popfront (seqlist* seq);//head delete void Display_seqlist (Seqlist *seq); Extract the elements in the Order table int insert_seqlist (seqlist *seq, int pos, DataType x); Adds an element to the specified position (starting from) int delete_seqlist (seqlist *seq, int pos); Deletes the element at the specified position (from the beginning)


SeqList.cpp

#include   "SeqList.h" #include <iostream> #include <assert.h>using namespace std; Seqlist *init_seqlist ()//Order table initialization algorithm, empty the order table {seqlist *seq;seq= new seqlist;seq->size  = 0; /* length to -1*/return seq;} Void pushback (seqlist* seq, datatype x)//tail plug {assert (seq);if  (seq->size >  maxsize) return;if  (seq->size == maxsize) {cout<< "Order table is full! "<<endl;return;} seq->seqlist[(Seq->size) ++] = x;} Void popback (SEQLIST*&NBSP;SEQ)//tail Delete {assert (seq);if  (seq->size == 0) {cout <<   "Order table is empty! " << endl;return;} --(seq->size);} Void pushfront (seqlist* seq, datatype x)//head Insert {Int end = seq->size;assert ( SEQ);if  (seq->size > maxsize) seq->size = 0;if  (seq->size ==  maxsize) {cout<< "Order table is full! "<<endl;return;} for  (;  end > 0;  --end) {seq->seqlist[end] = seq->seqlist[end - 1];} seq->seqlist[0] = x;seq->size++;} Void popfront (SEQLIST*&NBSP;SEQ)//Header Delete {Size_t start = 0;assert (seq);if  (seq->size  == 0) {cout<< "Order table is empty! "<<endl;return;} for  (;  start <seq->size; ++start) {seq->seqlist[start] = seq->seqlist [Start + 1];} --seq->size;} Void display_seqlist (SEQLIST&NBSP;*SEQ)//sequential table output algorithm {cout <<  "The element stored in the order table is"  <<  endl;int pos;for  (pos = 0; pos<= seq->size - 1; pos++ ) {cout << seq->seqlist[pos] <<  " ";} Cout << endl;} Int insert_seqlist (seqlist *seq, int pos, datatype x)    // Insert algorithm for sequential tables {cout <<  "Insert element"  << x <<  "insertion to position"  << pos  <<  "On " << endl;int j;if  (seq->size == maxsize - 1)  // The array length is equal to the set value-1, then the table full {cout <<  " << endl;return -1";} if  (pos<0 | |  pos>seq->size)  //insert position before No. 0, or insert to length greater than current array {cout <<  "position wrong"  <<  endl;return 0;} for  (j = seq->size-1; j >= pos; j--)  //pos after all move back {Seq->seqList [J + 1] = seq->seqlist[j];} seq->seqlist[pos] = x; //filling elements into pos position seq->size++;cout <<  "Insert Success"  < < endl;display_seqlist (seq); return 1;} Int delete_seqlist (Seqlist *seq, int pos)//delete algorithm for sequential table {cout <<  "place as"   << pos <<  "element Removal"  << endl;int j;if  (pos<0 | |  pos>seq->size-1) {cout <<  "does not exist"  << pos <<  "elements"  << endl;return 0;} for  (j = pos; j <= seq->size - 1; j++) {Seq->seqList[j]  = seq->seqlist[j + 1]; //pos Index All forward}seq->size--;cout <<  "Delete succeeded " << endl;display_seqlist (seq); return 1;}

Test_seqlist.cpp

#include "Seqlist.h" #include <iostream>using namespace std; The definition of the int main () {seqlist *seq;//sequential table is seq = Init_seqlist ();//initialization of the Order table Pushfront (seq, 0); Pushfront (SEQ, 1); Pushfront (SEQ, 2); Pushfront (SEQ, 3); Pushfront (SEQ, 3);D isplay_seqlist (seq);//output//insert_seqlist of the sequential table (SEQ, 4, 6);//insert//insert_seqlist of sequential table (SEQ, 5, 6); Nsert_seqlist (SEQ, 6, 6);//delete_seqlist (seq, 0);//delete of order table////delete_seqlist (SEQ, 4);D elete_seqlist (SEQ, 5); System ( "Pause"); return 0;}


C + + Mode implementation Order table

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.