C 模板實現順序表和煉表

2021-08-20 04:13:57 字數 2905 閱讀 9190

順序表

#pragma once

#include

#include

#include

using

namespace

std;

// 模板實現順序表--考慮深層次的深淺拷貝問題

template

class seqlist

t& operator(size_t pos);

void print();

private:

t* _a;

size_t _size;

size_t _capacity;

};template

seqlist::seqlist()

:_a(new t[10])

, _size(0)

, _capacity(10)

template

seqlist::seqlist(const seqlist& s)

:_a(new t[s._capacity])

, _size(s._size)

, _capacity(s._capacity)

template

<> //特化string,本來應該特化所有內建型別,其他自定義型別用for迴圈呼叫自定義型別的operate=過載

seqlist::seqlist(const seqlist& s)

:_a(new

string[s._capacity])

, _size(s._size)

, _capacity(s._capacity)

}template

seqlist& seqlist::operator=(seqlist& s)

return *this;

}template

<>

seqlist& seqlist::operator=(seqlist& s)

}return *this;

}template

seqlist::~seqlist()

template

void seqlist::pushback(const t& x)

_a[_size] = x;

_size++;

}template

void seqlist::popback()

}template

void seqlist::insert(size_t pos, const t& x)

for (int i = _size; i > pos; i--)

_size++;

}template

void seqlist::erase(size_t pos)

template

t& seqlist::operator(size_t pos)

}template

void seqlist::print()

}

鍊錶

#pragma once

#include

#include

#include

using namespace std;

// 帶頭節點的雙向迴圈鍊錶--思考結構的優勢

templatet>

struct node

;templatet>

class list

private:

node* _head;

};templatet>

list::list()

:_head(new node)

templatet>

list::list(const list& l)

:_head(new node)

}templatet>

list& list::operator=(const list& l)

node *cur = l._head->_next;

while (cur != l._head)

}return *this;

}templatet>

list::~list()

delete _head;

}templatet>

void list::pushback(const t& x)

templatet>

void list::popback()

templatet>

void list::pushfront(const t& x)

templatet>

void list::popfront()

templatet>

void list::insert(node* pos, const t& x)

templatet>

void list::erase(node* pos)

templatet>

void list::print()

}

棧和佇列

// 1.介面卡模式 

// 2.模板的模板引數 實現棧

template

class container = seqlist>

class stack

const t& top()

void pop()

protected:

container_con;

};//模板引數 實現佇列

template

class queue

t& front()

void pop()

protected:

container _con;

};

模板實現順序表和雙向鍊錶

模板詳解可以點這裡 include include include using namespace std 區別下面兩種概念 類的型別 vector 類 vector template class vector vector 析構函式 v2 v1 vector vector v 拷貝建構函式 del...

C 模板實現順序表

pragma once include include include linearlist.h using namespace std const int defaultsize 100 templateclass seqlist public linearlist int size const ...

順序表C 模板實現

include using namespace std const int maxsize 20 template class sqlist template sqlist sqlist template sqlist sqlist template void sqlist creatlist t ...