List templates, queue templates, sequential table templates, stack templates,

Source: Internet
Author: User

Stack and queue #pragma once#include<iostream> with container adapters #include <string> #include <cassert>using  namespace std;template<typename t>struct node{public:node (CONST&NBSP;T&AMP;&NBSP;D) : _next (NULL),  _prev (null)     ,_data (d) {}t _data; node<t> *_next; node<t> *_prev;}; Template<typename t>class linklist{public:linklist (): _head (null),  _tail (null) {}LinkList ( Const linklist<t>& list); Linklist<t>& operator= (linklist<t> list); ~linklist (); Void pushback (Const T &AMP;&NBSP;D); Void pushfront (const t& d); Void popback (); Void popfront (); void  insert (node<t> *addr, const t& d);   //inserts an element behind the current node node<t>*  search (const t& d); Void remove (const t& d); Void RemoveAll (const &NBSP;T&AMP;&NBSP;D); Void sort (); Void display (); size_t&nbSp Size (); T& getfront () {assert (_head); return _head->_data;} T& getback () {assert (_tail); return _tail->_data;} private:node<t> *_head; node<t> *_tail;}; Template<typename t>size_t linklist<t>::size () {Node<t> *cur = _head ;size_t count = 0;while  (cur) {count++;cur = cur->_next;} Return count;} Template<typename t>linklist<t>::linklist (const linklist<t>& list): _head ( NULL),  _tail (null) {node<t> *cur = list._head;while  (cur) {pushback (cur->_data ); cur = cur->_next;}} Template<typename t>linklist<t>& linklist<t>::operator= (LinkList<T>  list) {Std::swap (_head,list._head); Std::swap (_tail,list._tail); return *this;} Template<typename t>linklist<t>::~linklist () {Node<t> *cur = _head;while   (cur) {_head = _head->_next;delete cur;cur = _head;} _tail = null;} Template<typename t>void linklist<t&gt::P ushback (const t& d) {Node<T>  *NewHead = new Node<T> (d);if  (Null == _head) {_head =  Newhead;_tail = newhead;} else{_tail->_next = newhead; Newhead->_prev = _tail;_tail = _tail->_next;}} Template<typename t>void linklist<t&gt::P ushfront (const t& d) {Node<T>  *NewHead = new Node<T> (d);if  (Null == _head) {_head =  Newhead;_tail = newhead;} Else{newhead->_next = _head;_head->_prev = newhead;_head = newhead;}} Template<typename t>void linklist<t&gt::P opback () {if  (null == _head) {return ;} else if  (null==_head->_next) {Delete _tail;_head = null;_tail = null;} else{node<t> *cur = _tail;_tail = _tail->_prev;_tail->_next =  Null;delete cur;}} Template<typename t>void linklist<t&gt::P Opfront () {if  (null == _head) { return;} else if  (null == _head->_next) {delete _tail;_head = null;_tail =  null;} else{node<t> *cur = _head;_head = _head->_next;_head->_prev =  Null;delete cur;}} Template<typename t>void linklist<t>::insert (node<t> *addr, const t & d)    //inserting elements behind the current node {node<t> *newnode = new node<t> (d) ;if  (_head == addr) {newnode->_next = _head;_head->_prev = newnode;_ Head = newnode;} else if  (_TAIL&NBSP;==&NBSP;ADDR) {pushback (d);} Else{newnode->_next = addr->_next;addr->_next->_prev = NewNode;addr->_next = NewNode; Newnode->_prev = addr;}} Template<typename t>node<t>* linklist<t>::search (const T& d) {Node <T> *cur = _head;while  (cur) {if  (cur->_data == d) {return cur;} Cur = cur->_next;} Return null;} Template<typename t>void linklist<t>::remove (const t& d) {Node<T>  *cur =search (d);if  (Cur == _head) {Popfront ();} else if  (cur == _tail) {popback ();} else{cur->_prev->_next = cur->_next;cur->_next->_prev = cur->_prev; Delete cur;}} Template<typename t>void linklist<t>::removeall (const t& d) {while  ( Search (d)  != null) {Remove (d);}} Template<typename t>void linklist<t>::sort () {node<t> *end = _tail; while  (_HEAD&NBSP;!=&NBsp;end) {node<t> *cur = _head;int flag = 1;while  (cur!=end) {if  ( Cur->_data > cur->_next->_data) {t tmp = cur->_data;cur->_data  = cur->_next->_data;cur->_next->_data=tmp;flag = 0;} Cur = cur->_next;} if  (flag) {return;} End = end->_prev;}} Template<typename t>void linklist<t>::D isplay () {node<t> *cur = _ head;while  (cur) {cout << cur->_data <<  " "; cur = cur- >_next;} Cout << endl;}




#include "LinkList.h" template<typename t,template<class> class container>class  Queue{public:void push (const t& d); Void pop (); T& front (); T& back (); Size_t size (); Bool empty (); Void display () {while  (! Empty ()) {Cout << front ()  <<  " "; Pop ();} Cout << endl;} private:container<t> _con;}; Template<typename t,template<class> class container>void queue<t,container ::P Ush (const t& d) {_con. Pushback (d);} Template<typename t,template<class> class container>void queue<t,container ::P op () {_con. Popfront ();} Template<typename t,template<class> class container>t& queue<t, Container>::front () {Return _con. Getfront ();} Template<typename t,template<class> class container>t& queue<t,containeR>::back () {Return _con. Getback ();} Template<typename t,template<class> class container>size_t queue<t, Container>::size () {Return _con. Size ();} Template<typename t,template<class> class container>bool queue<t,container >::empty () {Return _con. Size () == 0;}







#pragma  once#include<iostream> #include <cstring> #include <string> #include <cassert> Using namespace std;template<typename t>class seqlist{public:seqlist (); Seqlist (CONST&NBSP;SEQLIST&LT;T&GT;&AMP;&NBSP;SEQ); Seqlist & operator= (SEQLIST&LT;T&GT;&NBSP;SEQ); ~seqlist (); Void pushback (const T& &NBSP;D); Void pushfront (const t& d); Void popback (); Void popfront ();void  Insert (int index,const t& d); Int search (const t& d); Void Remove ( CONST&NBSP;T&AMP;&NBSP;D); Void removeall (const t& d); Void sort (); Void Reserve (int  n); Void display (); Int getsize (); T& operator[] (int index);p rivate:void checkcapacity (int n=0);p rivate:t *_pdata; int _sz;int _capacity;}; Template<typename t>t& seqlist<t>::operator[] (Int index) {assert (index > =&NBSP;0); asSERT (INDEX&NBSP;&LT;&NBSP;_SZ); return _pdata[index];} Template<typename t>int seqlist<t>::getsize () {RETURN&NBSP;_SZ;} Template<typename t>seqlist<t>::seqlist ()          &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;:_SZ (0)                , _capacity (0)   , _pdata (NULL) {}template<typename t>seqlist<t >::seqlist (const seqlist<t>& seq) {_pdata = new t[seq._capacity];for   (int i = 0; i < seq._sz; i++) {_pdata[i] = seq._pdata[i];} _sz = seq._sz;_capacity = seq._capacity;} Template<typename t>seqlist<t>& seqlist<t>::operator= (Seqlist<T>  SEQ) {swap (_pdata,seq._pdata); _sz = seq._sz;_capacity = seq._capacity;return *this;} template<typename t>seqlist<T>::~seqlist () {    if  (_pdata != null) {delete[] _pdata;_pdata  = null;_sz = 0;_capacity = 0;}} Template<typename t>void seqlist<t>::checkcapacity (int n=0) {if  (_sz ==  _capacity| | n>_capacity) {int newcapacity =2*_capacity+1;if  (n > _capacity) {NewCapacity  = n;} t* tmp = new t[newcapacity];for  (int i = 0; i < _sz;  i++) {tmp[i] =_pdata[i];} delete[] _pdata;_pdata = tmp;_capacity = newcapacity;}} Template<typename t>void seqlist<t&gt::P ushback (const t& d) {CheckCapacity (); _pdata[_sz++] = d;} Template<typename t>void seqlist<t&gt::P Ushfront (const t& d) {CheckCapacity () ;//memmove (_pdata + 1,_pdata,sizeof (T) *_SZ);for  (int i =_sz;i>0; i--) {_pdata[ I]&nbSP;=&NBSP;_PDATA[I-1];} _pdata[0] = d;_sz++;} Template<typename t>void seqlist<t>::P opback () {_sz--;} Template<typename t>void seqlist<t>::P Opfront () {//memmove (_pdata,_pdata+1,sizeof (T) * (--_ SZ));for  (int i = 0; i<_sz-1; i++) {_pdata[i] = _pdata[i+1];} _sz--;} Template<typename t>void seqlist<t>::insert (int index, const t&  D) {assert (index >= 0); assert (INDEX&LT;_SZ); Checkcapacity ();//memmove (_pdata+index+1,_pdata+index,sizeof (T) * (_sz-index));for  (int i =  _sz; i>index; i--) {_pdata[i] = _pdata[i - 1];} _sz++;_pdata[index] = d;} Template<typename t>int seqlist<t>::search (const t& d) {int i =  0;for  (i = 0; i < _sz; i++) {if  (_pdata[i] == d) { Return i;}} Return -1;  &nBsp;           //didn't find the return -1}template<typename t. >void seqlist<t>::remove (const t& d) {int index = search (d);if  (index == -1) {return;} Else{//memmove (_pdata+index,_pdata+index+1,sizeof (T) * (_sz-index-1));for  (int i =index; i <_sz-1; i++) {_pdata[i] = _pdata[i+1];} _sz--;}} Template<typename t>void seqlist<t>::removeall (const t& d) {while  ( Search (d)  != -1) {Remove (d);}} Template<typename t>void seqlist<t>::reserve (int n) {CheckCapacity (n);} Template<typename t>void seqlist<t>::sort () {int end = _sz - 1; for  (int i = 0; i < _sz - 1; i++) {int flag =  1;int k = end;for  (int j = 0; j <end;j++) {if  (_ Pdata[j]>_pData[j+1]) {t tmp = _pdata[j];_pdata[j] = _pdata[j + 1];_pdata[j +  1] = tmp;flag = 0;k= j;}} if  (flag == 1) {return;} end = k;}} Template<typename t>void seqlist<t>::D isplay () {for  (int i = 0;  i < _sz; i++) {cout << _pdata[i] <<  " ";} Cout << endl;}





#include "Seqlist.h" template<typename t,template<class> class container>class  Stack{public:bool empty (); Void push (const t& d); Void pop (); T& top (); Int size (); Void display () {while  (! Empty ()) {cout << top ()  <<  " "; Pop ();} Cout << endl;} private:container<t> _con;}; Template<typename t, template<class> class container>bool stack<t, Container>::empty () {Return _con. GetSize () ==0;} Template<typename t, template<class> class container>void stack<t,  container>::P op () {_con. Popback ();} Template<typename t, template<class> class container>void stack<t, Container&gt::P ush (const t& d) {_con. Pushback (d);} Template<typename t, template<class> class container>int stack<t, Container>::size () {Return _con. GetSize ();} Template<typename t, template<class> class container>t& stack<t,  container>::top () {Int sz=_con. GetSize () -1;return _con[sz];}


This article from the "11132019" blog, reproduced please contact the author!

List templates, queue templates, sequential table templates, stack templates,

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.