Data.Structures.For.Game.Programmers.PART2.Basics.7.Stacks&Queues
来源:互联网 发布:彩棉是转基因吗 知乎 编辑:程序博客网 时间:2024/06/08 18:37
1.Linked Stacks
A linked stack uses a linked list to store the data in the stack.
template <class Datatype>class LStack : public DLinkedList<Datatype>{public:};
The head of the list points to the bottom of the stack, and the tail of the list points to the top of the stack.
[Push & Pop & Top] places a new item at the top of the stack;pop an item off the top of the stack.
void Push(Datatype p_data){Append(p_data);}void Pop(){RemoveTail();}void Top(){return m_tail->m_data;}// The top func will not work correctly if the stack is empty.Be careful and make sure that the stack //is not empty before you call this function.int Count(){return m_count;}
2.Arrayed Stacks
It is of a fixed size, but you can use the array's Resize function to make it bigger or smaller as you desire.
template <class Datatype>class AStack : public Array<Datatype>{public:int m_top;// point to the next empty cell};[Cons]AStack(int p_size) : Array<Datatype>(p_size){m_top = 0;}declare a instance:AStack<int> stack(10);
[Push & Pop & Top]
bool Push(Datatype p_data){if (m_size != m_top){m_array[m_top] = p_data;m_top ++;return true;}return false;}void Pop(){if (m_top > 0) m_top --;}Datatype Top(){return m_array[m_top - 1];}int Count(){return m_top;}
3.Linked Queues
template <class Datatype>class LQueue : public DLinkedList<Datatype>{public:void Enqueue(Datatype p_data){Append(p_data);}void Dequeue(){RemoveHead();}Datatype& Front(){return m_head->m_data;}};
4.Arrayed Queues
For a circular queue, you need 2 new vars: the index of the front of the queue and the number of items within the queue.
template <class Datatype>class AQueue : public Array<Datatype>{public:int m_front;int m_count;};
[Cons & Enqueue]
AQueue(int p_size) : Array<Datatype>(p_size){m_front = 0;m_count = 0;}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;}void Dequeue(){if (m_count > 0){m_count --;m_front ++;if (m_front == m_size)m_front = 0;}}Datatype Front(){return m_array[m_front];}Datatype& operator [] (int p_index){return m_array[(p_index + m_front) % m_size];}
- Data.Structures.For.Game.Programmers.PART2.Basics.7.Stacks&Queues
- Data.Structures.For.Game.Programmers.PART2.Basics.3.Array
- Data.Structures.For.Game.Programmers.PART2.Basics.4.Bitvector
- Data.Structures.For.Game.Programmers.PART2.Basics.5.Multi_Dimensional_Arrays
- Data.Structures.For.Game.Programmers.PART2.Basics.6.LinkedLists
- Data.Structures.For.Game.Programmers.PART2.Basics.8.HashTables
- Data Structures for Game Programmers
- Data.Structures.For.Game.Programmers.PART1.Concepts.1&2
- Data Structures for Game Programmers\examples\ch03\01 - Static Arrays
- Physics Modeling for Game Programmers
- Shaders for Game Programmers and Artists(目录)
- Shaders for Game Programmers and Artists
- 3D Math Primer for Game Programmers
- shaders for game programmers and artists
- Ternary Data Types for C Programmers
- UESTC Training for Data Structures(总结)
- 【UESTC】2015 UESTC Training for Data Structures
- 2016 UESTC Training for Data Structures
- apache 学习笔记
- unix进程终止方式
- Java线程:线程的调度-优先级
- SQL中的Where,Group By,Order By和Having
- Win7+Ubuntu下Gurb2修复过程总结
- Data.Structures.For.Game.Programmers.PART2.Basics.7.Stacks&Queues
- Java线程:线程的调度-让步
- 运放稳定性连载6:RO与ROUT(1)
- Java线程:线程的调度-合并
- hdu 2352 Verdis Quo
- Windpws下Common Lisp的实现
- .NET MVC权限设计思考之切入点
- vc编译apache 模块配置
- Java线程:线程的调度-守护线程