Implementation of the C + + data structure Order table (template class implementation)

Source: Internet
Author: User

Using template classes to implement sequential table operations

The functions implemented:

1. Tail plug, 2. Head plug, 3. Display, 4. End Delete, 5. Header Delete, 6. Press position, 7. Insert by value, 8. By location, 9. By value, 10. For the table length, 11. Clear the data, 12. Destroy the order table, 13. Reverse, 14. Sort (bubble sort, quick sort).

Header File Source code:

#pragma once//prevent duplicate compilation #include<iostream>using namespace Std;template<class Type>class seqlist{public:se Qlist (size_t sz=init_size);p ublic:bool isfull () Const{return size>=capacity;} BOOL IsEmpty () Const{return size==0;}   Public:void push_back (const Type &x);  Tail plug void push_front (const Type &x);                Head inserted void show_list ();                 show void Pop_back ();                Tail delete void Pop_front ();        Header delete void Insert_pos (int pos,const type &x);//insert void Insert_val by position (const Type &x);              insert void Delete_pos (int pos) by value;        Delete void Delete_val by location (const Type &x);            Delete by value int find (const Type &key);                    check int length () const by value;                          The table length void clear ();                        Clear data void destroy ();                         Destroy the order table void Reserv ();       reverse void sort (/*int low,int high*/); Sort private:enum{init_size=8}; Type *base;size_t capacity;size_t size;}; Template<class type>seqlist< Type>::seqlist (size_t sz) {capacity = SZ > init_size? sz:init_size;base = new type[capacity];size = 0;} Template<class type>void seqlist<type>::p ush_back (const Type &x) {if (Isfull ()) {cout<< "Order table full, Can't insert! " <<endl;return;} base[size++] = x;} Template<class type>void seqlist<type>::p ush_front (const Type &x) {if (Isfull ()) {cout<< "Order table full, Can't insert! " <<endl;return;} for (int i=size; i>0;-I.) {BASE[I] = base[i-1];} Base[0] = x;size++;} Template<class type>void seqlist<type>::show_list () {for (int i=0; i<size; ++i) {Cout<<base[i] << "";} Cout<<endl;} Template<class type>void seqlist<type>::p op_back () {if (IsEmpty ()) {cout<< ' order table full ' <<en        dl    Return } size = size-1;}    Template<class type>void seqlist<type>::p Op_front () {int i;    for (i = 0;i<size-1;i++) {base[i]=base[i+1]; } size--;} Template<class type>void seqlist<type>::insert_pOS (int pos,const Type &x) {if (pos<0 | | pos>size) {cout<< "The location to be inserted is illegal!" <<endl;return;} if (Isfull ()) {cout<< "Order table is full, cannot be inserted!" <<endl;return;} for (int i=size; i>pos;-I.) {BASE[I] = base[i-1];} Base[pos] = x;size++;}    Template<class type>void seqlist<type>::insert_val (const Type &x) {int pos;    pos = find (x); Insert_pos (pos,x);}    Template<class type>void seqlist<type>::d elete_pos (int pos) {int i;    for (i = pos;i<size-1;++i) {Base[i] = base[i+1]; } size--;} Template<class type>void seqlist<type>::d elete_val (const Type &x) {int pos = find (x); if (pos = =-1) { return;} for (int i=pos; i<size; ++i) {base[i] = base[i+1];} size--;} Template<class type>int seqlist<type>::find (const Type &key) {for (int i=0; i<size; ++i) {if (base[i] = = key) return i;} return-1;}    Template<class type>int seqlist<type>::length () const{cout<< "table length is:" <<size<<endl; return size;} Template&lT;class type>void Seqlist<type>::clear () {while (size) {base[size--] = NULL;    }}template<class type>void seqlist<type>::d Estroy () {int i;    Delete base;    base = NULL;    capacity = 0; size = 0;}    Template<class type>void Seqlist<type>::reserv () {int i = 0;    int m = size-1;            for (; i<= ((size-1)/2); ++i) {int tmp = Base[i];            Base[i] = base[m];            BASE[M] = tmp;        m--; }}template<class type>void Seqlist<type>::sort ()//Sort {for (Int. i=0;i<=size;i++) for (int j=i+1;j<= size-1;j++) {if (base[i]>base[j]) {int tmp = BASE[J];BASE[J]=BASE[I];BASE[I]=TMP;}}}        /*template<class type>///fast sort void seqlist<type>::sort (int low,int high) {if (low >= high) {    Return    } int first = low;    int last = high;             int key = Base[first]; Use the first record of the Word table as the pivot while (initial < last) {while (primary < last && base[last); = key) {last--;            } Base[first] = base[last];//will move to the low side while (first < last && Base[first] <= key) {        first++; } Base[last] = base[first];//will move to high end than the first large} Base[first] = key;//pivot record in place sort (low, first-            1); Sort (first+1, high);} */


Main function:

#include "SeqList.h" int main () {seqlist<int> mylist;int select = 1;int item;int pos;while (SELECT) {cout<< "* * "<<endl;cout<<" * [1] push_back [2] push_front * "&LT;&LT;ENDL;COUT&L t;< "* [3] show_list [0] quit_system*" <<endl;cout<< "* [4] pop_back [5] Pop_front *" <<end l;cout<< "* [6] insert_pos [7] insert_val *" <<endl;cout<< "* [8] delete_pos [9] delete_val *" &lt    ;<endl;cout<< "* [ten] Find [11]length *" <<endl;cout<< "* [] clear [13]destroy * "<<endl;cout<<" * [Reserv] [15]sort * "<<endl;cout<<" ************************** "<<endl;cout<<" Please select:> "; cin>>select;switch (select) {Case 1:cout<<" Enter the value you want to insert ( -1 end):> "; while (Cin>>item, item!=-1) {mylist.push_back (Item);} Break;case 2:cout<< "Please enter the value to be inserted (-1 end):>"; while (Cin>>item, item!=-1) {MYLIST.PUSH_FROnt (Item);}        Break;case 3:mylist.show_list (); break;            Case 4:mylist.pop_back ();        Break            Case 5:mylist.pop_front (); Break;case 6:cout<< "Please enter a location to insert:>";cin>>pos;cout<< "Enter the value to insert:>";cin>>item;        Mylist.insert_pos (Pos,item);            Case 7:cout<< "Please enter the value to insert:>";            cin>>item;        Mylist.insert_val (Item);            Case 8:cout<< "Please enter the location to delete:>";            cin>>pos;            Mylist.delete_pos (POS); Break;case 9:cout<< "Please enter the value to delete:>"; Cin>>item;mylist.delete_val (Item); Break;case 10:cout<< "        Please enter the value you want to find:> "; Cin>>item;int Pos;pos = Mylist.find (Item);            Case 11:mylist.length ();        Break            Case 12:mylist.clear ();        Break            Case 13:mylist.destroy ();        Break            Case 14:mylist.reserv ();        Break     Case 15:       int A;            A = Mylist.length ();            Mylist.sort (/*0,a-1*/); Break;default:break;}}}



Implementation of the C + + data structure Order table (template class implementation)

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.