C + + Implementation data structure 1. Sequential table

Source: Internet
Author: User

Header file Seqlist.h

#ifndef _seqlist_h_#define _seqlist_h_#include<iostream>using namespace Std;template <class Type>class Seqlist{public:seqlist () {capacity = Defaultsize;base = new type[capacity];size = 0;} seqlist (int sz) {capacity = SZ > defaultsize?

Sz:defaultsize;base = new type[capacity];size = 0;} ~seqlist () {delete []base;} Public:bool Full () const{if (Size > capacity) return True;return false;} BOOL Empty () const{if (size = = 0) return True;return false;} BOOL Push_back (Type const x) {if (full ()) return false;else{base[size] = x;size++;} return true;} BOOL Push_front (Type const x) {if (full ()) return flse;else{for (int i = size; i > 0; ++i) {base[i] = base[i-1];} Base[0] = x;} return true;} void Show_list () {for (int i = 0; i < size; ++i) {cout << base[i] << "--";} cout << "End" << Endl;} void Pop_back () {size--;} void Pop_front () {for (int i = 0; i < size-1; ++i) {Base[i] = base[i + 1];} size--;} BOOL Insert_val (Type x) {if (Empty () | | size = = capacity) return False;sort (); int i = 0;while (x > Base[i] && i < size) {i++;} Insert_pos (i,x); return true;} BOOL Insert_pos (int pos,type x) {if (pos <0 | | pos>size) return false;else{for (int i = size; i > pos;-I.) {BASE[I] = Base[i-1];} Base[pos] = x;}size++;} int find (Type key) {for (int i = 0; i < size; ++i) {if (base[i] = = key) return i;} cout << "No Exit" << endl;return-1;} BOOL Delete_pos (int pos) {if (pos<0 | | pos > Size) return false;else{for (int i = pos; i < size-1; ++i) {Base[i] = Base[i + 1];} size--;}} BOOL Delete_val (Type key) {if (Empty ()) return false;else{int pos = Find (Key);d Elete_pos (POS);} return true;} void sort () {Type tmp;for (int i = 1; i<size; ++i) {tmp = Base[i];int j;for (j = i; tmp < base[j-1];--j) {Base[j] = BASE[J-1];} BASE[J] = tmp;}} void Resever () {if (size = = 0 | | size = = 1) return;for (int i = 0; i < (SIZE/2); ++i) {Type tmp = base[i];base[i] = BASE[SIZE-I-1];BASE[SIZE-I-1] = tmp;}} int Length () {return size;} void Clear () {size = 0;} void Destory () {delete[]base;} private:enum {defaultsize = 8}; Type *base;int capacity;int size;}; #endif

Main program

#include "seqlist.h" void Main () {seqlist<int> mylist;int select = 1;int item;int pos;while (select) {cout << "& amp;&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&& "<< endl;cout << "& &" << endl;cout << "& [1] push_back [2] Push_for NT & "<< endl;cout <<" & [3] show_list [4] Pop_back & "<< endl;cout <<" &amp ; [5] Pop_front [6] insert_val & "<< endl;cout <<" & [7] insert_pos [8] Find & "&L        t;< endl;cout << "& [9] delete_pos [ten] Delete_val &" << endl;cout << "& [one] sort [Resever] & "<< endl;cout <<" & [[] length [] Clear & "<< endl;c Out << "& [Destroy] [0] Quit_systEM & "<< endl;cout <<" & & "<< endl;cout <<" & &&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&& "<< Endl;cout < < "choose->"; Cin >> Select;switch (SELECT) {case 1:cout << "Please cin data end with-1:" <&lt ; Endl;while (CIN >> Item, Item! =-1) {Mylist.push_back (item);} Break;case 3:mylist.show_list (); break;case 4:mylist.pop_back (); break;case 5:mylist.pop_front (); Break;case 6:cout << "Please cin the data want insert:"; CIN >> Item;mylist.insert_val (item); Break;case 7:cout << "plea SE cin POS and data: "; Cin >> POS >> item;mylist.insert_pos (POS, item); Break;case 8:cout <<" please cin T He data you want find, it'll back it pos: "; Cin >> Item;cout<<mylist.find (item) <<endl;breaK;case 9:cout << "Please cin the pos you want delete:"; Cin >> Pos;mylist.delete_pos (POS); Break;case 10:cout & lt;< "Please cin the data you want to Dalete:"; Cin >> Item;mylist.delete_val (item); Break;case 11:cout << " Sort! " <<endl;mylist.sort (); break;case 12:mylist.resever (); break;case 13:cout<<mylist.length () <<endl; Break;case 14:mylist.clear (); break;case 15:mylist.destory (); break;default:break;}}}


C + + Implementation data structure 1. Sequential 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.