表的实现(顺序)一
SeqList.h:
- /*=============================================================================
- #
- # Author: liangshu - cbam
- #
- # QQ : 756029571
- #
- # School : 哈尔滨理工大学
- #
- # Last modified: 2015-10-27 20:03
- #
- # Filename: 顺序栈实现 - 副本.cpp
- #
- # Description:
- # The people who are crazy enough to think they can change the world, are the ones who do !
- =============================================================================*/
- #
- using namespace std;
- const int Dafultsize = 10;
- enum Error_code{success, overflow, underflow, Range_error};
- template<class List_entry>
- class List{
- public:
- List(int Size = Dafultsize):Max_list(Size), num(0){
- Entry = new List_entry[Max_list];
- }
- int Size_list()const;
- bool Full_list()const;
- bool Empty_list()const;
- void Clear();
- void Print_list()const;
- void Tra_list(void (*visit)(List_entry &));
- Error_code retrieve(int postion, List_entry &item)const;
- Error_code Replace(int postion, const List_entry &item);
- Error_code Remove(int postion, List_entry &item);
- Error_code Insert(int postion, const List_entry &item);
- ~List(){
- delete []Entry;
- }
- protected:
- int num;
- List_entry *Entry;
- int Max_list;
- };
- template<class List_entry>
- int List<List_entry>::Size_list()const{
- return num ;
- }
- template<class List_entry>
- bool List<List_entry>::Full_list()const{
- return num == Max_list - 1;
- }
- template<class List_entry>
- bool List<List_entry>::Empty_list()const{
- return num == 0;
- }
- template<class List_entry>
- void List<List_entry>::Clear(){
- num = 0;
- }
- template<class List_entry>
- Error_code List<List_entry>::Insert(int position, const List_entry &item){
- if(Full_list()){
- return overflow;
- }
- if(position < 0 || position > num){
- return Range_error;
- }
- for(int i = num - 1; i >= position; i--){
- Entry[i + 1] = Entry[i];
- }
- Entry[position] = item;
- num++;
- return success;
- }
- template<class List_entry>
- void List<List_entry>::Tra_list(void(*visit)(List_entry &)){
- for(int i = 0; i < num; i++){
- (*visit)(Entry[i]);
- }
- }
- template<class List_entry>
- Error_code List<List_entry>::retrieve(int position, List_entry &item)const{
- if(Full_list()){
- return underflow;
- }
- if(position < 0 || position > num){
- return Range_error;
- }
- item = Entry[position];
- return success;
- }
- template<class List_entry>
- Error_code List<List_entry>::Replace(int position, const List_entry &item){
- if(position > num || position < 0){
- return Range_error;
- }
- Entry[position] = item;
- return success;
- }
- template<class List_entry>
- Error_code List<List_entry>::Remove(int position, List_entry &item){
- if(Empty_list()){
- return underflow;
- }
- if(position < 0 || position > num){
- return Range_error;
- }
- item = Entry[position];
- for(int i = position;i < num; i++){
- Entry[i] = Entry[i + 1];
- }
- num--;
- return success;
- }
- template<class List_entry>
- void List<List_entry>::Print_list()const{
- cout<<"| ";
- for(int i = 0; i < num; i++){
- cout<<Entry[i];
- if(i != num - 1){
- cout<<" -- ";
- }
- }
- cout<<" |"<<endl;
- }
Test.cpp:
- #include<iostream>
- #include"a.h"
- using namespace std;
- int main()
- {
- List<int>list_1(9);
- for(int i = 1; i <= 5; i++){
- list_1.Insert(i - 1, i);
- }
- int x;
- list_1.Print_list();
- cout<<list_1.Size_list()<<endl;
- list_1.retrieve(1, x);
- cout<<x<<endl;
- list_1.Replace(2, 100);
- list_1.Print_list();
- list_1.Remove(3, x);
- list_1.Print_list();
- list_1.Clear();
- list_1.Print_list();
- return 0;
- }

浙公网安备 33010602011771号