Basic operations of the "c++/data structure" sequential table

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" ><strong> #pragma once#include <iostream>using namespace std;typedef enum {FALSE, TRUE}status;template& Lt;class type>class seqlist{public:seqlist (int sz = defaultsize) {capacity = SZ > defaultsize?

Sz:defaultsize;base = new type[capacity];size = 0;} ~seqlist () {destroy ();/*delete []base;base = null;capacity = size = 0;*/}public:status Inc () {Type *newbase = new Type[capac ity + inc_size];if (newbase = = NULL) {return FALSE;} memcpy (newbase, base, sizeof (Type) *size);d elete[]base;base = newbase;capacity + = Inc_size;return TRUE;} Status merge (seqlist<type> <1, seqlist<type> <2) {int i = 0;int j = 0;int k = 0;while (i < lt1.size &A mp;& J < Lt2.size) {if (Lt1.base[i] > Lt2.base[j]) {base[k++] = lt2.base[j++];} else{base[k++] = lt1.base[i++];}} while (I < lt1.size) {base[k++] = lt1.base[i++];} while (J < lt2.size) {base[k++] = lt2.base[j++];} Size = lt1.size + Lt2.size;return TRUE;} Status isfull () const{if (size >= capacity) {return TRUE;} Else{return FALSE;}} Status Empty () const{if (size = = 0) {return TRUE;} Else{return FALSE;}} Status push_back (const type& x) {if (Isfull () &&! INC ()) {cout << "isfull! not pluggable" << x << endl;return FALSE;} Base[size++] = X;return TRUE;} Status Push_front (const type& x) {if (Isfull () &&! INC ()) {cout << "isfull! not Header" << x << endl;return FALSE;} for (int i = size; i > 0; i.) {base[i] = base[i-1];} Base[0] = X;size++;return TRUE;} Status Pop_back () {if (Empty ()) {cout << "empty! cannot be deleted" << Endl;return FALSE;} Size--;return TRUE;} Status Pop_front () {if (Empty ()) {cout << "empty! cannot be deleted by header" << Endl;return FALSE;} for (int i = 0; i < size; ++i) {Base[i] = base[i + 1];} Size--;return TRUE;} Status insert_val (const type& x) {if (Isfull () &&! INC ()) {cout << "isfull! cannot be inserted by value" << x << endl;return FALSE;} int i = 0;while (Base[i] < x && I < size) {i++;} Insert_pos (i, X); return TRUE;} Status insert_pos (int pos, const type& x) {if (Isfull () &&! INC ()) {cout << isfull! Cannot insert "<< Endl;return FALSE by Location"; if (pos < 0 | | pos > Size) {return FALSE;} for (int i = 0; i < size; ++i) {if (i = = pos) {for (int j = SizE J > i; --J) {Base[j] = base[j-1];}}} Base[pos] = X;size++;return TRUE;} int find (const type& x) {if (size = = 0) {return-1;} for (int i = 0; i < size; ++i) {if (base[i] = = x) {return i;}}} Status delete_pos (int pos) {if (pos < 0 | | pos > Size) {return FALSE;} for (int i = 0; i < size; ++i) {if (L = = pos) {for (int j = i; j < size; ++j) {Base[j] = base[j + 1];} Break;}} Size--;return TRUE;} Status delete_val (const type& x) {int key = find (x); if (key = =-1) {return FALSE;} Delete_pos (key), Return True;/*if (Empty ()) {cout << "empty!" << Endl;return FALSE;} for (int i = 0, i < size; ++i) {if (base[i] = = x) {for (int j = i; j < size; ++j) {base[j] = base[j+1];} Break;}} Size--;return true;*/}void Sort () {if (size = = 0 | | size = = 1) {return;} for (int i = 0, i < size-1; ++i) {for (int j = 0; J < size-i-1; ++j) {if (Base[j] > base[j + 1]) {int tmp = BA SE[J];BASE[J] = base[j + 1];base[j + 1] = tmp;}}} void Resver () {if (size = = 0 | | size = = 1) {return;} for (int i = 0, J = Size-1; I < J; ++i,--j) {int tmp = Base[i];base[i] = base[j];base[j] = tmp;}} int Length () {return size;} void Clear () {size = 0;} Status Destroy () {if (base = = NULL) {return FALSE;} Delete[]base;base = Null;size = capacity = 0;return TRUE;} Status modify_val (const type& x, const type& y) {int key = find (x); if (key = =-1) {return FALSE;} Modify_pos (key, y); return TRUE;} Status modify_pos (int pos, const type& x) {if (pos < 0 | | pos > Size) {return FALSE;} for (int i = 0; i < size; ++i) {if (i = = pos) {Base[i] = x;}} return TRUE;} void Show_list () {for (int i = 0; i < size; ++i) {cout << base[i] << ";} cout << Endl;} Private:enum {defaultsize = $, inc_size = 3}; Type *base;int capacity;int size;}; </strong></span>


<span style= "FONT-SIZE:18PX;" ><strong> #include "SeqList.h" void Main () {seqlist<int> It1; Seqlist<int> It2; seqlist<int> Mylist;int select = 1;int pos;int item;system ("Color 0d"); while (select) {cout << *************  "<< endl;cout <<" * [0] quit_system [1] push_back * "<< endl;cout <<" *  [2] push_front [3] show_list * "<< endl;cout <<" * [4] pop_back [5] Pop_front * "<< endl;cout << "* [6] insert_val [7] Insert_pos *" << endl;cout << "* [8] Find [9] delete_pos *" &LT;&L T      Endl;cout << "* [ten] delete_val [one] sort *" << endl;cout << "* [[] resver [] Length * "<< endl;cout <<" * [[] clear [] Destroy * "<< endl;cout <<" * [+] modify_val [17] Modify_pos * "<< endl;cout <<" * [] Merge * "<< endl;cout <<" *********** *****************"<< endl;cout <<" Select:> ", Cin >> Select;switch (SELECT) {case 1:cout <<" Enter the data to be inserted (- 1 end):> ";" while (CIN >> item, Item! =-1) {Mylist.push_back (item);} Break;case 2:cout << "Please enter the data to be inserted (-1 end):>"; while (CIN >> item, Item! =-1) {Mylist.push_front (item);} Break;case 3:system ("CLS"); Mylist.show_list (); System ("pause"); break;case 4:mylist.pop_back (); Break;case 5: Mylist.pop_front (); Break;case 6:cout << "Please enter the value to insert:>"; Cin >> Item;mylist.insert_val (item); Break;case 7 : cout << "Please enter subscript:> for the value to be inserted"; Cin >> pos;cout << "Enter the value to insert"; Cin >> Item;mylist.insert_pos (POS, Item); Break;case 8:cout << "Please enter the value to find:>"; Cin >> item;cout << "The value is labeled:" << mylist.find (item) << endl;break;case 9:cout << "Please enter subscript:> for the value to be removed"; Cin >> Item;mylist.delete_pos (item); Break;case 10: cout << "Please enter the value to delete:>"; Cin >> Item;mylist.delete_val (item); Break;case 11:mylist.sort (); Break;case 12: Mylist.resVer (); break;case 13:cout << "linear table Length:" << mylist.length () << endl;break;case 14:mylist.clear (); break ; case 15:mylist.destroy (); Break;case 16:cout << "Please enter the value to change:>"; Cin >> item;cout << "Please enter the changed value:>"; CIN >> Pos;mylist.modify_val (item, POS); break;case 17:cout << "Please enter subscript:> for the value to be changed; CIN >> Pos;cout < < "Please enter the changed value:>"; Cin >> Item;mylist.modify_pos (POS, item); break;case 18:for (int i = 1; i < ten; i + = 2) {IT1.P Ush_back (i);} for (int i = 2; I <=; i + = 2) {it2.push_back (i);} Mylist.merge (IT1, It2); Mylist.show_list ();d efault:break;}} </strong></span>

The results of the execution are as follows:





Basic operations of the "c++/data structure" sequential table

Related Article

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.