Stacks are commonly known as LIFO structures.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Linked stacks implementation
template<class Datatype>
class LStack : public DlinkedList<Datatype>
{
Public:
};
// The Push function
void Push(Datatype p_data)
{
Append(p_data);
}
// The Pop function
void Pop()
{
RemoveTail();
}
// The Top function
Datatype Top()
{
return m_tail->m_data;
}
// The Count function
int Count()
{
return m_count;
}
Arrayed stacks implementation
template<class Datatype>
class AStack : public Array<Datatype>
{
Public:
int m_top;
};
// The constructor
AStack(int p_size) : Array<Datatype>(p_size)
{
m_top = 0;
}
// The Push function
bool Push(Datatype p_data)
{
if (m_size != m_top)
{
m_array[m_top] = p_data;
m_top++;
return true;
}
Return false;
}
// The Pop function
void Pop()
{
if (m_top > 0)
m_top--;
}
// The Top function
Datatype Top()
{
return m_array[m_top – 1];
}
// The Count function
int Count()
{
return m_top;
}
Basically, a queue is a FIFO structure.
Circular queue implementation
template<class Datatype>
class Aqueue : public Array<Datatype>
{
public:
int m_front;
int m_count;
};
// The constructor
Aqueue(int p_size) : Array<Datatype>(p_size)
{
m_front = 0;
m_count = 0;
}
// The Enqueue function
bool Enqueue(Datatype p_data)
{
if (m_size != m_count)
{
m_array[(m_count + m_front) % m_size] = p_data;
m_count++;
return true;
}
return false;
}
// The Dequeue function
void Dequeue()
{
if (m_count > 0)
{
m_count--;
m_front++;
if (m_front == m_size)
m_front = 0;
}
}
// The Front function
Datatype Front()
{
return m_array[m_front];
}
// The Access operator
Datatype& operator[] (int p_index)
{
return m_array[(p_index + m_front) % m_size];
}
转载于:https://blog.51cto.com/riser/48293