数据结构与算法分析课后习题第三章(11)
来源:互联网 发布:托克托网络花店 编辑:程序博客网 时间:2024/05/12 04:24
3.31 Efficiently implement a stack class using a singly linked list, with no header or tail nodes.
3.32 Efficiently implement a queue class using a singly linked list, with no header or tail nodes.
3.33 Efficiently implement a queue class using a circular array. You may use a vector (rather than an primitive array) as the underlying array structure.
//3.31-3.32 slist.hpp
#ifndef SSList_HPP__
#define SSList_HPP__
template< typename Object >
class SListStack;
template< typename Object >
class SListQueue;
#include <iostream>
template< typename Object >
class SList {
private:
class Node {
public:
Node(const Object& val = Object(), Node* n = 0)
: next(n), data(val){}
public:
Node* next;
Object data;
};
public:
SList() : theSize(0), head(0),tail(0){}
~SList()
{
clear();
}
bool empty() const { return theSize == 0; }
const int size() const { return theSize; }
void push_front(const Object& val)
{
if(empty())
{
Node* newnode = new Node(val);
head = tail = newnode;
++theSize;
}
else
{
Node* newnode = new Node(val,head);
head = newnode;
++theSize;
}
}
void pop_front()
{
if(empty())
return;
Node* p = head;
head = head->next;
delete p;
--theSize;
}
void push_back(const Object& val)
{
if(empty())
{
Node* newnode = new Node(val);
head = tail = newnode;
++theSize;
}
else
{
Node* newnode = new Node(val,0);
tail->next = newnode;
tail = newnode;
++theSize;
}
}
void clear()
{
while(!empty())
{
if(head->next == 0)
{
delete head;
--theSize;
}
else
{
pop_front();
}
}
}
void print()
{
if(empty())
return;
Node* p = head;
while(p->next != 0)
{
std::cout << p->data << "/n";
p = p->next;
}
std::cout << p->data << "/n";
}
private:
SList(const SList&);
SList& operator=(const SList&);
Node* head;
Node* tail;
int theSize;
friend class SListStack<Object>;
friend class SListQueue<Object>;
};
#endif
//slstack.hpp
#ifndef SLSTACK_HPP__
#define SLSTACK_HPP__
#include "slist.hpp"
template< typename Object >
class SListStack {
public:
SListStack() : lst(new SList<Object>()){}
~SListStack() { delete lst; }
public:
const int size() const { return lst->size(); }
bool empty() const { return lst->empty(); }
void push(const Object& val)
{
lst->push_front(val);
}
void pop()
{
lst->pop_front();
}
const Object& top() const
{
return lst->head->data;
}
private:
SList<Object>* lst;
};
#endif
//slqueue.hpp
#ifndef SLQUEUE_HPP__
#define SLQUEUE_HPP__
#include "slist.hpp"
template< typename Object >
class SListQueue {
public:
SListQueue() : lst(new SList<Object>()) {}
~SListQueue() { delete lst; }
public:
bool empty() const { return lst->empty(); }
const int size() const { return lst->size(); }
void printQueue()
{ lst->print(); }
void enqueue(const Object& val)
{ lst->push_back(val); }
void dequeue()
{ lst->pop_front(); }
private:
SList<Object>* lst;
};
#endif
//3.33 cycarray.hpp
#ifndef CYCARRAY_HPP__
#define CYCARRAY_HPP__
#include <vector>
#include <iostream>
template< typename Object >
class CircularArray {
public:
CircularArray() : vec(new std::vector<Object>())
{
vec->reserve(10);
}
~CircularArray() { delete vec; }
const int size() const { return vec->size(); }
bool empty() const { return vec->size() == 0; }
void push_back(const Object& val)
{
if(size() < 10)
{
vec->push_back(val);
}
else
std::cerr << "Full/n";
}
void pop_front()
{
if(!empty())
{
std::vector<Object>::iterator it = vec->begin();
vec->erase(it);
}
else
std::cerr << "Empty/n";
}
const Object front() const
{
return *(vec->begin());
}
const Object back() const
{
return *(--vec->end());
}
private:
std::vector<Object>* vec;
};
#endif
//queue.hpp
#ifndef QUEUE_HPP__
#define QUEUE_HPP__
#include "cycarray.hpp"
template< typename Object>
class Queue {
public:
Queue() : theArray(new CircularArray<Object>())
{}
~Queue()
{ delete theArray; }
bool empty() const { return theArray->empty(); }
const int size() const { return theArray->size(); }
void enqueue(const Object& val)
{ theArray->push_back(val); }
Object dequeue() const
{
Object retVal = theArray->front();
theArray->pop_front();
return retVal;
}
private:
CircularArray<Object>* theArray;
};
#endif
- 数据结构与算法分析课后习题第三章(11)
- 数据结构与算法分析课后习题第三章(2)
- 数据结构与算法分析课后习题第三章(3)
- 数据结构与算法分析课后习题第三章(4)
- 数据结构与算法分析课后习题第三章(5)
- 数据结构与算法分析课后习题第三章(6)
- 数据结构与算法分析课后习题第三章(7)
- 数据结构与算法分析课后习题第三章(8)
- 数据结构与算法分析课后习题第三章(9)
- 数据结构与算法分析课后习题第三章(10)
- 数据结构与算法分析课后习题第四章(1)
- 数据结构与算法分析课后习题第四章(2)
- 数据结构与算法分析课后习题第四章(3)
- 数据结构与算法分析课后习题第四章(4)
- 数据结构与算法分析课后习题第五章
- 算法导论课后习题解析 第三章
- 算法入门经典第三章课后习题
- 算法设计与分析课后习题3.5
- 比尔·盖茨精辟良言,送给职场新人的10句话
- 用代码向网站提交数据
- eclipse treeViewer的使用 (例程)
- 画直线算法
- 对象和接口(通俗讲解)
- 数据结构与算法分析课后习题第三章(11)
- ibatis 对象关系实现
- Design Patterns -- Visitor
- php上传文件,内存不足解决之道
- 占地儿
- ADO.NET读取Eexcel数据!
- sessionfilter
- Microsoft的测试题
- 实时数据库