Recently at home in the evening to see algorithems,4th Edition, I bought the English version, I think this book is relatively straightforward to understand, and "map code and Mao", taking advantage of this opportunity to learn to do a good job of notes, this will also be impressed, this is also the reason for writing this series of articles. In addition, Princeton University in Coursera also has this book synchronized public class, there is another algorithm analysis class, the course of the author is also the author of the book, both classes are very good.
The computer program can not be separated from the algorithm and data structure, this paper introduces the stack (stack) and the implementation of queues (queue). NET related to the data structure, the typical application, etc., hope to deepen their understanding of these two simple data structures.
1. Basic Concepts
The concept is simple, stacks (stack) is a OFF,LIFO data structure, whereas queues (queue) is a first-in, first-out (fisrt in-first OUT,FIFO) structure, as shown in the following illustration:
2. To achieve
Now let's see how to implement the two data structures above. Before doing this, the Framework design guidelines This book tells us that when designing APIs or entity classes, API specifications should be written around the scenario.
Implementation of 1.1 stack
Stacks are a LIFO data structure, and we want to at least provide the following methods for stack:
To implement these functions, we have two methods, arrays and linked lists, to first look at the list implementation:
Stack of linked list implementation:
We first define an inner class to hold the node for each list, which includes the current value and points to the next value, and then establishes a node to hold the value at the top of the stack and the number of elements in the stack;
Class Node
{public
T item{get;set;}
Public Node Next {get; set;}
}
Private Node-i = null;
private int number = 0;
Now to implement the push method, push an element to the top of the stack, first save the original element at the top of the stack, and then create a new top element of the stack, and then point the next element to the original stack top element. The entire pop process is as follows: