C++ 造房子list

来源:互联网 发布:电脑桌面软件下载 编辑:程序博客网 时间:2024/05/16 01:44
#pragma oncetemplate<typename T>class Clist{public:Clist();~Clist();Clist(const T *data);Clist(const Clist &ls);bool Push_bak(  T *const data);//后插bool Push_first(T*const data);//前插bool Push_bak(const int& pos,  T *const data);//定位插入const  T * const Pop_bak() ;//后const T *const Pop_first() ;//前T *Head_Top()const ;//返回顶部元素T *Edn_Top()const;//尾部元素T *findval(const T &fin_val)const;//返回朝找的元素void out_show()const;//显示所有元素void out_show_reverse()const;bool close();//清空bool empty();//是否为空size_t getLen(){ return len; }bool deleteVal(const T &data);//删除摸个元素private:class Node{public:Node() :data(NULL), base(NULL), pNext(NULL){}Node(T *const m_data, Node *m_base);Node *getPNext()const;Node *getBase()const;void SetPNext(Node * m_pNext);void SetBase(Node *  m_base);T *getData()const;void SetData( T *const data);private:T *data;Node *pNext;Node *base;}*head,*end;size_t len;};

#include "Clist.h"#include <iostream>template <typename T>Clist<T>::Node::Node(T *const m_data, Node *m_base) :data(m_data), base(m_base){}template<typename T>typename Clist<T>::Node *Clist<T>::Node::getPNext()const{return pNext;}template<typename T>typename Clist<T>::Node *Clist<T>::Node::getBase()const{return base;}template<typename T>void Clist<T>::Node::SetPNext(Node * m_pNext){pNext = m_pNext;}template<typename T>void Clist<T>::Node::SetBase(Node *  m_base){base = m_base;}template<typename T>T *Clist<T>::Node::getData()const{return data;}template<typename T>void Clist<T>::Node::SetData(T *const data){this->data = data;}template<typename T>Clist<T>::Clist() :head(NULL), end(NULL), len(0){}template<typename T>Clist<T>::~Clist(){if (head){close();}}template<typename T>Clist<T>::Clist(const Clist<T> &ls){len = ls.getLen();Node *temp = ls.head;head = new Node((temp->getData()), NULL);end = head;temp = temp->getPNext();while (temp){end->SetPNext(new Node(temp->getPNext(), end));end = end->getPNext();temp = temp->getPNext();}}template<typename T>Clist<T>::Clist(const T *data) : head(NULL), end(NULL), len(0){if (!head&&data){head = new Node(data, head);end = head;len++;}else{}}template<typename T>bool Clist<T>::Push_bak(T *const data){if (!data)return false;if (!head){head = new Node(data, head);end = head;len++;return true;}Node *pNew = new Node(data, end);end->SetPNext(pNew);end = pNew;len++;return true;}template<typename T>bool Clist<T>::Push_first(T*const data){if (!data)return false;if (!head){head = new Node(data, head);end = head;len++;return true;}Node *pNew = new Node(data, NULL);head->SetBase(pNew);pNew->SetPNext(head);head = pNew;len++;return true;}template<typename T>bool Clist<T>::Push_bak(const int &pos, T *const data){if (pos<0 || pos>len - 1)return false;int tmplen = 0;Node *temp = head;while (tmplen < pos){temp = temp->getPNext();tmplen++;}Node *pNew = new Node(data, temp->getBase());pNew->SetPNext(temp);temp->SetBase(pNew);len++;return true;}template<typename T>const T * const Clist<T>::Pop_bak(){if (!head){}else{if (head == end){T *temp = head->getData();delete head;end = head = NULL;len--;return temp;}T *temp = end->getData();Node *ptmp = end->getBase();ptmp->SetPNext(NULL);delete end;end = ptmp;len--;return temp;}}template<typename T>const T *const Clist<T>::Pop_first(){if (!head){}else{if (head == end){T *temp = head->getData();delete head;head = end = 0;len--;return temp;}T *temp = head->getData();head = head->getPNext();delete head->getBase<span style="font-family: Arial, Helvetica, sans-serif;">();</span>head->SetBase(NULL);len--;return temp;}}template<typename T>T *Clist<T>::Head_Top()const{if (!head)//不处理任意崩溃{return NULL;}else{return  head->getData();}}template<typename T>T * Clist<T>::Edn_Top()const{if (!head)//不处理任意崩溃{return NULL;//return NULL;}else{return  end->getData();}}template<typename T>T *Clist<T>::findval(const T &fin_val)const{if (!head){//return NULL;}else{Node *temp = head;while (temp){if (fin_val == *(temp->getData()))break;temp = temp->getPNext();}if (!temp){return NULL;}return temp->getData();}}template<typename T>void Clist<T>::out_show()const{if (!head)return;Node *temp = head;while (temp){std::cout << *(temp->getData()) << " ";temp = temp->getPNext();}}template<typename T>void  Clist<T>::out_show_reverse()const{if (!end)return;Node *temp = end;while (temp){std::cout << *(temp->getData()) << " ";temp = temp->getBase();}}template<typename T>bool Clist<T>::close(){if (!head)return false;while (head){delete Pop_bak();}return true;}template<typename T>bool Clist<T>::empty(){return head;}template<typename T>bool Clist<T>::deleteVal(const T &data){if (!head){return false;}else{if (data == *(head->getData()) && !head->getPNext()){delete head->getData();delete head;head = end = NULL;len--;return true;}else{if (data == *(head->getData())){delete head->getData();head = head->getPNext();delete head->getBase();head->SetBase(NULL);len--;return true;}else if (data == *(end->getData())){delete end->getData();end = end->getBase();delete end->getPNext();end->SetPNext(NULL);len--;return true;}else{Node *temp = head;while (temp != end){if (data == *(temp->getData())){Node *ptmp = temp;temp->getPNext()->SetBase(ptmp->getBase());temp->getBase()->SetPNext(ptmp->getPNext());delete temp->getData();delete temp;temp = NULL;len--;return true;}temp = temp->getPNext();}return false;}}}}

#include<iostream>#include "Clist.hpp"void main(){Clist<int>s;for (int i = 0; i < 3; i++){s.Push_bak(new int(i+1));}int val = 0;std::cout << "朝找数据:" <<std:: endl;std::cin >> val;std::cout << s.findval(val);std::cout.clear();std::cout << "删除数据" << std::endl;std::cin >> val;if (s.deleteVal(val)){std::cout << "删除成功\n";}s.out_show();std::cout << std::endl;std::cout << "长度:"<<s.getLen() << std::endl;;s.Push_bak(1, new int(55));std::cout << std::endl;s.out_show_reverse();s.close();s.out_show();system("pause");}

0 0