"Data Structure" in C + + implementation of sequential table of various operations (including header delete, tail delete, insert, reverse, destroy, empty, etc.)

Source: Internet
Author: User

Sequential table of various operations (including header delete, tail delete, insert, reverse order, destroy, empty etc.)//header file #pragma once#include <iostream>using namespace Std;template<class Type>class seqlist{public:seqlist (size_t sz = init_size) ~seqlist ();p ublic:bool isfull () const{return size >= capacity; }bool IsEmpty () const{return (size = = 0);} Public:void show_list (), void Push_front (const type &x), void push_back (const type &x), void Insert_pos (int pos, Const type &AMP;X); int find (const type &key); void Pop_front (); void Pop_back (); void sort (); void Insert_val (const type &x), void Delete_pos (int pos), void Delete_val (const Type &x), int length (), void Clear (), void Destory (); void reverse (); void Quit_system (int &a);p rivate:enum{init_size = 8}; Type *base;size_t capacity;size_t size;}; Template<class type>//Constructor Seqlist<type>::seqlist (size_t sz) {capacity = SZ > Init_si ZE? Sz:init_size;base = new type[capacity];size = 0;} Template<class type>//destructor Seqlist<type>::~seqlist () {delete base;} Template<class type>//Show void Seqlist<type>::show_list () {if (size = = 0) {cout << " The data table is empty. "<< Endl;return;} for (int i = 0; i<size; ++i) {cout << base[i] << "";} cout << Endl;} Template<class type>//head insert void Seqlist<type>::p ush_front (const Type &x) {if (Isfull ( ) {cout << "Order table is full, cannot be inserted." << Endl;return;} for (int i = size; i > 0; i.) {base[i] = base[i-1];} Base[0] = x;size++;} Template<class type>//end plug void seqlist<type>::p ush_back (const Type &x) {if (Isfull ()) {cout << "Order table is full, cannot be inserted." << Endl;return;} Base[size] = x;size++;} Template<class type>//by position socket void Seqlist<type>::insert_pos (int pos, const Type &x) {if (pos<0 | | pos >size) {cout << "is not inserted correctly. "<< 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>//Find 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>//head Delete void Seqlist<type>::p Op_front () {if (IsEmpty ()) {cout << order table is empty, cannot be deleted. "<< Endl;return;} for (int i = 0; i < size; ++i) {Base[i] = base[i + 1];} size--;} Template<class type>void seqlist<type>::p op_back ()//tail Delete {if (IsEmpty ()) {cout <<] The order table is empty and cannot be deleted. "<< Endl;return;} size--;} Template<class type>//sort void Seqlist<type>::sort () {if (IsEmpty ()) {return;} for (int i = 1, i < size; i++) {for (int j = 0; J < Size-i; J + +) {int temp;if (base[j]>base[j + 1]) {temp = Base[j ];BASE[J] = base[j + 1];base[j + 1] = temp;}}}  Template<class type>//by value insert void Seqlist<type>::insert_val (const Type &x) {push_back (x);   Tail interpolation sort (); Sort}template<class type>//Delete void seqlist<type&gt by location;::d elete_pos (int pos) {if (IsEmpty ()) {cout << "The order table is empty and cannot be deleted. "<< Endl;return;} if (pos < 0 | | pos >= size) {cout << "the deleted location is incorrect. "<< Endl;return;} for (int i = pos; i < size; ++i) {Base[i] = base[i + 1];} size--;} Template<class type>//Delete void seqlist<type&gt by value;::d elete_val (const Type &x) {if (IsEmpty ()) {cout << The order table is empty and cannot be deleted. "<< Endl;return;}  int Pos=find (x); if (pos = =-1) {cout << "the number was not found. "<< Endl;return;} Delete_pos (POS);} Template<class type>//length int seqlist<type>::length () {return size;} Template<class type>//Clear table void Seqlist<type>::clear () {size = 0;} Template<class type>//destroys table void Seqlist<type>::d estory () {base = NULL;} Template<class type>//reverse void Seqlist<type>::reverse () {for (int i = 0,j = size-1; i < SIZE/2; i++, j--) { int temp = Base[i];base[i] = base[j];base[j] = temp;}} Template<class type>//exit void Seqlist<type>::quit_system (int &a) {a = 0;} Main function # include "SeqList.h" void Main () {seqlist<int> mylist; int select = 1;int item;int pos;while (select) {cout << ******************************************************* "<< endl;cout <<" * [1] show_list [2] Quit_system * " << endl;cout << "* [3] push_front [4] push_back *" << endl;cout << "* [5] pop_front [6] pop_back *" << endl;cout << "* [7                        ] Insert_pos [8] insert_val * "<< endl;cout <<" * [9] Delete_pos [Ten] Delete_val * "<< endl;cout <<" * [one] find [1              2] Length * "<< endl;cout <<" * [[] clear [] destory * "<< endl;cout <<" * [[] Reverse (reverse) [+] Sort (order) * "<< endl;c Out << "**************"<< endl;cout <<" Please select function: "; CIN >> Select ; switch (SELECT) {case 1:mylist.show_list (); break;case 2:mylist.quit_system (select); Break;case 3:cout << " Please enter the number you want to insert (ends with-1): "; while (CIN >> item, Item! =-1) {Mylist.push_front (item);} Break;case 4:cout << "Please enter the number you want to insert (ends with-1):" while (CIN >> item, item!=-1) {Mylist.push_back (item);} Break;case 5:mylist.pop_front (); break;case 6:mylist.pop_back (); Break;case 7:cout << "Please enter where you want to insert:"; CIN >> Pos;cout << "Please enter the value you want to insert:"; Cin >> Item;mylist.insert_pos (POS, Item); break;case 8:cout << "Please enter the number to insert:";    CIN >> Item;mylist.insert_val (Item), break;case 9:cout << "Please enter the location to be deleted:"; CIN >> Pos;mylist.delete_pos (POS); break;case 10:cout << "Please enter the value to be deleted:"; Cin >> Item;mylist.delete_val ( Item); Break;case 11:cout << "Please enter the number to find:", cin >> Item;pos = Mylist.find (item), if (pos! =-1) {cout << "the number is "<< Pos <<" number"<< Endl;} cout << "The number was not found. "<<endl;break;case 12:cout <<" The Order table length is: "<< mylist.length () << endl;break;case 13:mylist.clear (); Break;case 14:mylist.destory (); break;case 15:mylist.reverse (); break;case 16:mylist.sort (); break;default:break;}} 

"Data Structure" in C + + implementation of sequential table of various operations (including header delete, tail delete, insert, reverse, destroy, empty, etc.)

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.