顺序表
code.h
#ifndef CODE_H_ #define CODE_H_ #define DATA_SIZE 10 typedef int data_type; enum CODE_OP { CODE_ERR = -1, CODE_OK }; class CODE { private: int count; int size; data_type *data; public: CODE(); CODE(unsigned int size); ~CODE(); int get_count(); void set_count(int count); int get_size(); void set_size(int size); void set_data(data_type *data); data_type *get_data(); data_type get_data(unsigned int i); void set_data(data_type data, unsigned int i); // CODE *Create(); // void Destroy(CODE *pList); int Insert(CODE *pList, data_type tData, int iOffset); int Delete(CODE *pList, data_type *pData, int iOffset); int Update(CODE *pList, data_type tNew, data_type tOld); int Select(CODE *pList, data_type tData); void ShowList(CODE *pList); }; #endif /* CODE_H_ */
code.cpp
#include <stdlib.h> #include <string.h> #include <iostream> #include "CODE.h" using namespace std; CODE::CODE() { // TODO Auto-generated constructor stub this->set_count(0); this->set_size(0); this->set_data(NULL); } CODE::CODE(unsigned int count) { this->set_count(count); this->set_size(0); this->set_data(new data_type[count]); } CODE::~CODE() { // TODO Auto-generated destructor stub cout << "~CODE" << endl; } int CODE::get_count() { return this->count; } void CODE::set_count(int count) { this->count = count; } int CODE::get_size() { return this->size; } void CODE::set_size(int size) { this->size = size; } data_type *CODE::get_data() { return this->data; } void CODE::set_data(data_type *data) { this->data = data; } data_type CODE::get_data(unsigned int i) { return this->data[i]; } void CODE::set_data(data_type data, unsigned int i) { this->data[i] = data; } int CODE::Insert(CODE *pList, data_type tData, int iOffset) { if (NULL == pList || 0 > iOffset || pList->get_count() < iOffset) { cout << "input error insert" << endl; return CODE_ERR; } if (pList->get_count() == pList->get_size()) { data_type *newData = new data_type[DATA_SIZE + pList->get_count()]; if (NULL == newData) { cout << "malloc newData error" << endl; return CODE_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()); } int iMoveCount = pList->get_size() - iOffset; int i = 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); return CODE_OK; } int CODE::Delete(CODE *pList, data_type *pData, int iOffset) { if (NULL == pList || NULL == pData || 0 > iOffset || pList->get_size() < iOffset) { cout << "input error delete" << endl; return CODE_ERR; } *pData = pList->get_data(iOffset); int i = iOffset; for (; i < pList->get_size() - 1; i++) { pList->set_data(pList->get_data(i + 1), i); } pList->set_size(pList->get_size() - 1); return CODE_OK; } int CODE::Update(CODE *pList, data_type tNew, data_type tOld) { if (!pList) { cout << "input error update" << endl; return CODE_ERR; } int i = 0; for (; i < pList->get_size() - 1; i++) { if (tOld == pList->get_data(i)) pList->set_data(tNew, i); } return CODE_OK; } int CODE::Select(CODE *pList, data_type tData) { if (!pList) { cout << "input error select" << endl; return CODE_ERR; } int i = 0; for (; i < pList->get_size(); i++) { if (pList->get_data(i) == tData) return i; } return CODE_OK; } void CODE::ShowList(CODE *pList) { if (!pList) { cout << "input error show" << endl; return; } int i = 0; for (; i < pList->get_size(); i++) cout << pList->get_data(i) << " "; cout << endl; return; }
main.cpp
#include "CODE/CODE.h" #include <iostream> using namespace std; void function() { CODE code; CODE *pList = new CODE(DATA_SIZE); if (!pList) { return; } cout << "rank ok" << endl; int i = 14; while (i--) { code.Insert(pList, i, 0); } code.ShowList(pList); int tData = -1; code.Delete(pList, &tData, 4); code.ShowList(pList); code.Update(pList, 999, 5); code.ShowList(pList); delete pList; return; } int main() { function(); return 0; }
时间: 2024-10-11 11:51:57