数据结构-顺序表与单链表的C++模板类实现

来源:互联网 发布:妙味云课堂js视频下载 编辑:程序博客网 时间:2024/04/25 20:06

C++模板类实现顺序表

实现功能: 

1.尾插,2.头插,3.显示,4.尾删,5.头删,6.按位置,7.插按值插,8.按位置删,9.按值删,10.按值查,11.求表长,12.清除数据,13.摧毁该顺序表,14.反转,15.冒泡排序。

顺序表模板类实现代码:

//顺序表***************************************************************#include <iostream>using namespace std;const int INIT_SIZE = 100;template<class Type>class SeqList{public:SeqList(size_t sz = INIT_SIZE);             //构造长度为len的顺序表int Length(){ return length; }              //求顺序表长度Type Get(int i);                            //按位查找void push_back(const Type &x);              //尾插  void push_front(const Type &x);             //头插  void show_list();                           //显示  void pop_back();                            //尾删  void pop_front();                           //头删  void insert_pos(int pos, const Type &x);    //按位置插  void insert_val(const Type &x);             //按值插  void delete_pos(int pos);                   //按位置删  void delete_val(const Type &x);             //按值删  int  find(const Type &key);                 //按值查  int  length()const;                         //求表长  void clear();                               //清除数据  void destroy();                             //摧毁该顺序表  void reserv();                              //反转  void sort(/*int low,int high*/);            //排序  bool isfull()const{return size >= capacity;}//判断顺序表是否满bool isempty()const{ return size == 0; }    //判断顺序表是否空private:Type *base;size_t capacity;size_t size;};template<class Type>//构造长度为len的顺序表SeqList<Type>::SeqList(size_t sz){capacity = sz > INIT_SIZE  ? sz : INIT_SIZE ;base = new Type[capacity];size = 0;}template<class Type>//尾插void SeqList<Type>::push_back(const Type &value_X){if (isfull()){cout << "顺序表已满,不能尾插!" << endl;return;}base[size++] = value_X;}template<class Type>//头插void SeqList<Type>::push_front(const Type &value_X){if (isfull()){cout << "顺序表已满,不能头插!" << endl;return;}for (int i = size; i > 0; --i)base[i] = base[i - 1];base[0] = value_X;size++;}template<class Type>void SeqList<Type>::show_list()//显示{for (int i = 0; i< size; ++i)cout << base[i] << " ";cout << endl;}template<class Type>void SeqList<Type>::pop_back()//尾删{if (isempty()){cout << "顺序表已满" << endl;return;}size = size - 1;}template<class Type>void SeqList<Type>::pop_front()//头删  {int i;for (i = 0; i< size - 1; i++){base[i] = base[i + 1];}size--;}template<class Type>void SeqList<Type>::insert_pos(int pos, const Type &x)//按位置插 {if (pos < 0 || pos > size){cout << "要插入的位置非法!" << endl;return;}if (isfull()){cout << "顺序表已满,不能插入!" << endl;return;}for (int i = size; i > pos; --i)base[i] = base[i - 1];base[pos] = x;size++;}template<class Type>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>void SeqList<Type>::insert_val(const Type &x)//按值插{int pos;pos = find(x);insert_pos(pos, x);}template<class Type>void SeqList<Type>::delete_pos(int pos)//按位置删{for (int i = pos; i<size - 1; ++i){base[i] = base[i + 1];}size--;}template<class Type>void SeqList<Type>::delete_val(const Type &x)//按值删 {int pos = find(x);if (pos == -1){return;}for (int i = pos; i<size; ++i){base[i] = base[i + 1];}size--;}template<class Type>int SeqList<Type>::length()const//求表长{cout << "表长是:" << size << endl;return size;}template<class Type>void SeqList<Type>::clear()//清除数据 {while (size)base[size--] = 0;}template<class Type>void SeqList<Type>::destroy()//摧毁该顺序表 {int i;delete base;base = 0;capacity = 0;size = 0;}template<class Type>void SeqList<Type>::reserv()//反转{int m = size - 1;for (int i = 0; i <= ((size - 1) / 2); ++i){int tmp = base[i];base[i] = base[m];base[m] = tmp;m--;}}template<class Type>void SeqList<Type>::sort()//冒泡排序  {for (int i = 0; i <= size; i++)for (int j = i + 1; j <= size - 1; j++){if (base[i]>base[j]){int tmp = base[j];base[j] = base[i];base[i] = tmp;}}}//主函数int main(){SeqList<int> mylist;int select = 1;int Item;int pos;while (select){cout << "数据结构--顺序表**********************" << endl;cout << "**************************************" << endl;cout << "* [1] push_back       [2] push_front *" << endl;cout << "* [3] show_list       [0] quit_system*" << endl;cout << "* [4] pop_back        [5] pop_front  *" << endl;cout << "* [6] insert_pos      [7] insert_val *" << endl;cout << "* [8] delete_pos      [9] delete_val *" << endl;cout << "* [10] find           [11]length     *" << endl;cout << "* [12] clear          [13]destroy    *" << endl;cout << "* [14] reserv         [15]sort       *" << endl;cout << "**************************************" << endl;cout << "请选择:>";cin >> select;switch (select){case 1:cout << "请输入要插入的值(-1结束):>";while (cin >> Item, Item != -1){mylist.push_back(Item);}break;case 2:cout << "请输入要插入的值(-1结束):>";while (cin >> Item, Item != -1){mylist.push_front(Item);}break;case 3:mylist.show_list();break;case 4:mylist.pop_back();break;case 5:mylist.pop_front();break;case 6:cout << "请输入要插入的位置:>";cin >> pos;cout << "请输入要插入的值:>";cin >> Item;mylist.insert_pos(pos, Item);break;case 7:cout << "请输入要插入的值:>";cin >> Item;mylist.insert_val(Item);case 8:cout << "请输入要删除的位置:>";cin >> pos;mylist.delete_pos(pos);break;case 9:cout << "请输入要删除的值:>";cin >> Item;mylist.delete_val(Item);break;case 10:cout << "请输入要查找的值:>";cin >> Item;int pos;pos = mylist.find(Item);break;case 11:mylist.length();break;case 12:mylist.clear();break;case 13:mylist.destroy();break;case 14:mylist.reserv();break;case 15:mylist.sort();break;default:break;}}return 0;}
单链表模板类实现代码:
//单链表********************************************************#include <iostream>using namespace std;//线性表抽象类的定义**template <class dataType>  class list{public:        virtual void empty() = 0;   //清空线性表       virtual int  getLength()=0;   //求表的长度        virtual void insert(int i,const dataType& x)=0;   //在表中第i个位置插入值为x的元素       virtual void remove(int i)=0; //删除表中第i个位置的元素        virtual int search(const dataType& x)=0;  //查找并返回值为x的元素在表中的位置       virtual dataType visit(int i)=0;  //访问表中第i个元素的值       virtual void traverse()=0;    //遍历线性表 }; template <class dataType>  class linkList :public list<dataType>       //公有继承自list类     {                                           public:          linkList();                             //构造函数,创建对象时生成空链表         void empty();                           //清空链表          int getLength();                        //求表的长度          void insert(int i,const dataType& x);   //在表中第i个位置插入值为x的元素         void remove(int i);                     //删除表中第i个位置的元素          int search(const dataType& x);          //查找并返回值为x的元素在表中的位置         dataType visit(int i);                  //访问表中第i个元素的值         void traverse();                        //将表中的元素逐一输出         ~linkList();                            //析构函数     private:                                    //定义节点         struct node{             dataType data;            node* next;              node():next(0){};             node(dataType d):data(d),next(0){}; };            node* head;                             //链表头         node* tail;                             //链表尾          int currentLength;                      //链表长度 }; template <class dataType> linkList<dataType>::linkList(){ head = new node;     tail = new node;     head->next = tail;     currentLength = 0;        cout << "\nCreate list success!\n"; }   template <class dataType>  void linkList<dataType>::empty(){if (currentLength == 0){cout << "\nThe list is empty!\n";return;}node* nowPos = head->next;    while (nowPos != tail){ node* tmp = nowPos;        nowPos = nowPos->next;        delete tmp; }     head->next = tail;    currentLength = 0;      cout << "\nEmpty list success!\n"; }   template <class dataType>  int linkList<dataType>::getLength(){ return currentLength; }    template <class dataType>  void linkList<dataType>::insert(int i, const dataType& x){if (i<0 || i>currentLength){ cout << "\nThe index is out of range!\n";         return; }       node* add = new node(x);     if (i == 0){ add->next = head->next;        head->next = add;}else{          //找到第i-1个节点         node* nowPos=head->next;         while(--i){             nowPos=nowPos->next;   }          add->next=nowPos->next;        nowPos->next=add;  }      ++currentLength;      cout<<"\nInsert data success!\n";}   template <class dataType>void linkList<dataType>::remove(int i){   if(i<0||i>=currentLength){        cout<<"\nThe index is out of range!\n";     return;    }     if(i==0){        node* tmp=head->next->next;        delete head->next;        head->next=tmp;    }      else{          //找到第i-1个节点   node* nowPos=head->next;  while(--i){           nowPos=nowPos->next;    } node* tmp = nowPos->next->next;  delete nowPos->next;        nowPos->next = tmp;}   --currentLength;        cout << "\nDelete data success!\n";}  template <class dataType>int linkList<dataType>::search(const dataType& x){node* nowPos = head;    int i;     for (i = 0; i<currentLength; ++i){nowPos = nowPos->next;  if (nowPos->data == x)break; }      if (i == currentLength)return -1;    return i; }  template <class dataType> dataType linkList<dataType>::visit(int i){     //下标越界抛出异常值0     if(i<0||i>=currentLength){cout << " 越界" << endl;return 0;throw 0;     }       //找到第i个节点     node* nowPos=head->next;   while(i--){         nowPos=nowPos->next;  }      return nowPos->data;}  template <class dataType> void linkList<dataType>::traverse(){    if(currentLength==0){          cout<<"\nThe list is empty!\n";     return;    }node* nowPos = head->next;     cout << nowPos->data;   nowPos = nowPos->next;    while (nowPos != tail){cout << " " << nowPos->data; nowPos = nowPos->next;}     cout << endl;}   template <class dataType> linkList<dataType>::~linkList(){ empty();   delete head;  delete tail; }//主函数int main(){linkList<int> mylist;int select = 1;int Item;int pos;while (select){cout << "数据结构--单链表************" << endl;cout << "****************************" << endl;cout << "* [1] 按位访问             *" << endl;cout << "* [2] 遍历打印             *" << endl;cout << "* [3] 按位插入             *" << endl;cout << "* [4] 按位删除             *" << endl;cout << "* [5] 按值查找             *" << endl;cout << "* [6] 单链表长度           *" << endl;cout << "* [7] 释放单链表内存       *" << endl;cout << "* [8] 清空单链表           *" << endl;cout << "****************************" << endl;cout << "请选择:>";cin >> select;switch (select){case 1:cout << "请输入要访问的位置(-1结束):>";while (cin >> Item, Item != -1){cout<<mylist.visit(Item)<<endl;}break;case 2:mylist.traverse();break;case 3:cout << "请输入要插入的位置:>";cin >> pos;cout << "请输入要插入的值:>";cin >> Item;mylist.insert(pos, Item);break;case 4:cout << "请输入要删除的位置:>";cin >> pos;mylist.remove(pos);break;case 5:cout << "请输入要查找的值:>";cin >> Item;cout << mylist.search(Item) << endl;break;case 6:cout<<"单链表长度:"<<mylist.getLength()<<endl;break;case 7:mylist.~linkList();cout << "单链表内存已释放" << endl;break;case 8:mylist.empty();cout << "单链表已清空"<<endl;break;default:break;}}return 0;}

参考博客:

http://www.bubuko.com/infodetail-825525.html

http://3y.uu456.com/bp_8mpdu2v9r83gznb0fxez_1.html

0 0
原创粉丝点击