c++__ sequence Table (practice)

Source: Internet
Author: User
Tags stub

Sequential table

Code.h

#ifndef Code_h_#defineCode_h_#defineData_size 10typedefintdata_type;enumCode_op {code_err= -1, CODE_OK};classCODE {Private:    intcount; intsize; Data_type*data; Public: CODE (); CODE (unsignedintsize); ~CODE (); intget_count (); voidSet_count (intcount); intget_size (); voidSet_size (intsize); voidSet_data (Data_type *data); Data_type*Get_data (); Data_type get_data (unsignedinti); voidSet_data (data_type data, unsignedinti);//CODE *create ();//void Destroy (CODE *plist);    intInsert (CODE *plist, Data_type tdata,intioffset); intDelete (CODE *plist, Data_type *pdata,intioffset); intUpdate (CODE *pList, Data_type tnew, Data_type told); intSelect (CODE *pList, data_type tdata); voidShowlist (CODE *pList);};#endif/* Code_h_ */

Code.cpp

#include <stdlib.h>#include<string.h>#include<iostream>#include"CODE.h"using namespacestd; Code::code () {//TODO auto-generated Constructor stub     This->set_count (0);  This->set_size (0);  This-Set_data (NULL);} Code::code (unsignedintcount) {     This-Set_count (count);  This->set_size (0);  This->set_data (Newdata_type[count]);} CODE::~CODE () {//TODO auto-generated destructor stubcout <<"~code"<<Endl;}intCode::get_count () {return  This-count;}voidCode::set_count (intcount) {     This->count =count;}intcode::get_size () {return  This-size;}voidCode::set_size (intsize) {     This->size =size;} Data_type*Code::get_data () {return  This-data;}voidCode::set_data (Data_type *data) {     This->data =data;} Data_type code::get_data (unsignedinti) {return  This-data[i];}voidCode::set_data (data_type data, unsignedinti) { This->data[i] =data;}intCode::insert (CODE *plist, Data_type tdata,intIoffset) {    if(NULL = = PList | |0> Ioffset | | Plist->get_count () <Ioffset) {cout<<"input Error Insert"<<Endl; returnCode_err; }    if(Plist->get_count () = = Plist->get_size ()) {Data_type*newdata =NewData_type[data_size + plist->get_count ()]; if(NULL = =NewData) {cout<<"malloc newdata Error"<<Endl; returnCode_err; } memset (NewData,0, Data_size + plist->get_count ()); memcpy (NewData, PList-get_data (), PList->get_size () *sizeof(data_type)); Delete(plist->get_data ()); PList-Set_data (NewData); PList->set_count (data_size + plist->get_count ()); }    intImovecount = Plist->get_size ()-Ioffset; inti = plist->get_size ()-1;  while(imovecount--) {pList->set_data (Plist->get_data (i), i +1); I--; } pList-Set_data (Tdata, Ioffset); PList->set_size (plist->get_size () +1); returnCode_ok;}intCode::D elete (Code *plist, Data_type *pdata,intIoffset) {    if(NULL = = PList | | NULL = = PData | |0>Ioffset|| Plist->get_size () <Ioffset) {cout<<"Input Error Delete"<<Endl; returnCode_err; }    *pdata = plist->Get_data (Ioffset); inti =Ioffset;  for(; i < plist->get_size ()-1; i++) {pList->set_data (Plist->get_data (i +1), i); } pList->set_size (Plist->get_size ()-1); returnCode_ok;}intCode::update (CODE *pList, Data_type tnew, Data_type told) {    if(!pList) {cout<<"Input Error Update"<<Endl; returnCode_err; }    inti =0;  for(; i < plist->get_size ()-1; i++) {        if(told = = plist->Get_data (i)) pList-Set_data (tnew, i); }    returnCode_ok;}intCode::select (CODE *pList, Data_type tdata) {    if(!pList) {cout<<"input Error Select"<<Endl; returnCode_err; }    inti =0;  for(; i < plist->get_size (); i++) {        if(Plist->get_data (i) = =tdata)returni; }    returnCode_ok;}voidCode::showlist (CODE *pList) {    if(!pList) {cout<<"input Error Show"<<Endl; return; }    inti =0;  for(; i < plist->get_size (); i++) cout<< Plist->get_data (i) <<"  "; cout<<Endl; return;}

Main.cpp

#include"code/code.h"#include<iostream>using namespacestd;voidfunction () {Code code; CODE*plist =NewCODE (data_size); if(!pList) {        return; } cout<<"Rank OK"<<Endl; inti = -;  while(i--) {code. Insert (PList, I,0); } code.    Showlist (pList); intTdata =-1; Code. Delete (PList,&tdata,4); Code.    Showlist (pList); Code. Update (PList,999,5); Code.    Showlist (pList); DeletepList; return;}intMain () {function (); return 0;}

c++__ sequence Table (practice)

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.