A brief talk on algorithm and data structure stack and queue

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.