#ifndef _MY_LIST_H_
#define _MY_LIST_H_
class OutOfBound {};
class IllegalSize{};
template <class elemType>
class MyList
{
public:
virtual void clear() = 0;
virtual int length() const = 0;
virtual void insert(int, const elemType &) = 0;
virtual void remove(int) = 0;
virtual int search(const elemType &) const = 0;
virtual const elemType& visit(int) const = 0;
virtual void traverse() const = 0;
virtual ~MyList(){}
};
#endif
#ifndef _MYDIRLIST_H_
#define _MYDIRLIST_H_
#include "mylist.h"
#include <iostream>
using namespace std;
template <class elemType>
class MyDirList : public MyList < elemType >
{
private:
struct node{
elemType data;
node *pNext;
node() : pNext(NULL){}
node(const elemType &_data, node *p = NULL) :
data(_data), pNext(p){}
};
node *m_pHead;
int m_dLength;
node *moveTo(int) const;
public:
MyDirList(){
m_pHead = new node;
m_dLength = 0;
}
~MyDirList(){
clear();
delete m_pHead;
}
void clear();
int length() const { return m_dLength; }
void insert(int, const elemType &);
void remove(int);
int search(const elemType &) const;
const elemType &visit(int) const;
void traverse() const;
};
template<class elemType>
typename MyDirList<elemType>::node* MyDirList<elemType>::moveTo(int pos) const
{
if (pos < -1 || pos >= m_dLength) throw OutOfBound();
node *p = m_pHead;
while (pos-->=0) p = p->pNext;
return p;
}
template<class elemType>
void MyDirList<elemType>::clear()
{
node *p = m_pHead->pNext, *q;
while (p != NULL)
{
q = p->pNext;
delete p;
p = q;
}
m_dLength = 0;
}
template<class elemType>
void MyDirList<elemType>::insert(int pos, const elemType &_data)
{
if (pos < 0 || pos > m_dLength) throw OutOfBound();
node *p = moveTo(pos - 1);
node *tmp = new node(_data, p->pNext);
p->pNext = tmp;
m_dLength++;
}
template<class elemType>
void MyDirList<elemType>::remove(int pos)
{
if (pos < 0 || pos >= m_dLength) throw OutOfBound();
node *p = moveTo(pos - 1);
node *q = p->pNext;
p->pNext = q->pNext;
delete q;
m_dLength--;
}
template<class elemType>
int MyDirList<elemType>::search(const elemType &_data) const
{
node *p = m_pHead->pNext;
int pos = 0;
while (p != NULL)
{
if (_data == p->data) return pos;
p = p->pNext;
pos++;
}
return -1;
}
template<class elemType>
const elemType &MyDirList<elemType>::visit(int pos) const
{
if (pos < 0 || pos >= m_dLength) throw OutOfBound();
node *p = moveTo(pos);
return p->data;
}
template<class elemType>
void MyDirList<elemType>::traverse() const
{
node *p = m_pHead->pNext;
while (p != NULL)
{
int i = 0;
cout << "Data #" << i++ << ": " << p->data << endl;
p = p->pNext;
}
cout << "End of data" << endl;
}
#endif
#ifndef _MYSTACK_H_
#define _MYSTACK_H_
#include "mydirlist.h"
//在本檔案中完成類介面的定義
template <class elemType>
class MyStack
{
public:
MyStack()
{
m_pStack = new MyDirList<elemType>;
};
~MyStack();
void push_back(const elemType &value);
void pop_back();
bool isEmpty() const { return m_pStack == NULL; };
const elemType &back() const { return m_pStack->visit(m_pStack->length()); };
private:
MyDirList<elemType> *m_pStack;
};
template <class elemType>
MyStack<elemType>::~MyStack()
{
delete m_pStack;
}
template<class elemType>
void MyStack<elemType>::push_back(const elemType & value)
{
m_pStack->insert(m_pStack->length(), value);
}
template<class elemType>
void MyStack<elemType>::pop_back()
{
m_pStack->remove(m_pStack->length());
}
#endif
#include <iostream>
#include "mystack.h"
using namespace std;
int main( )
{
MyStack<int>a;
a.push_back(1);
a.pop_back();
cout<<a.back()<<endl;
}
最好只改stack里的。小白求賜教...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63020.html
標籤:基礎類

