Stack of DS

Source: Internet
Author: User

For the understanding of the stack, early in the C + + class, the teacher gave us a simple introduction and use, this year contacted the data structure of this book, have a comprehensive study, have to say is a difficult to chew a book, but in order to write a better code, this course is inevitable.

In the data structure, the stack is a kind of important abstract data type. From the data structure point of view, the stack is also a linear structure, belonging to the linear table, its particularity is that the basic operation of the stack is a subset of linear table operations, it is the operation of the constrained linear table. From a data type perspective, it is a very different abstract multi-type data type from a linear table.

Definition of stacks

A stack is a linear table that restricts insertions or deletions only at the end of a table. Therefore, for the stack, the end of the table has its special meaning, called the top of the stack, accordingly, the table head end is called the bottom (bottom). Empty tables that do not contain elements are called empty stacks. Assuming the stack s= (A1,a2,a3,...,an), the A1 is the stack-bottom element, and an is the top element of the stack. The elements in the stack are stacked in the order of A1,a2,...,an, and the first element of the stack should be the top element of the stack. In other words, the stack's modification is based on the principle of last-in-first-out. Therefore, the stack is also known as a LIFO linear table. The following figure is a good explanation for this principle.

Presentation and implementation of stacks

Like linear tables, stacks also have two methods of storing representations.

Sequential stacks

First of all, the sequential stack: the sequential storage structure of a stack is a data element that is stored sequentially from the stack to the top of the stack using a contiguous set of storage units, with a pointer top representing the position of the top element of the stack in the sequence stack. Top is a stack-top pointer with its initial value pointing to the bottom of the stack, and the top of the pointer increases by 1 whenever a new top element is inserted. When the top element of the stack is deleted, the pointer top is minus 1, so the top pointer in the non-empty stack is always at the next position in the top element of the stack. The following figure illustrates the correspondence between the data elements in the sequential stack and the top pointers in the stack.

Link stacks

Sequential stack in the operation of the process may occur "overflow" phenomenon (overflow and underflow), that is, there is no stack after the stack of problems, this time to use the stack of the chain storage structure, known as the chain stack. The structure of each node is exactly the same as the node structure in a single linked list. A node consists of two domains: the domain in which the information for the data element is stored is called the data field, and the field that stores the direct successor storage location is called the pointer field. The information stored in the pointer field is called a pointer or chain. Learning about the list here will tell you how the chain stack implements the basic operations, and the following diagram explains the problem well.

Definition of a sequential stack

The usual practice is to use top=0 to represent empty stacks. Since the size of the maximum space required for the stack is difficult to estimate, the maximum capacity of the stack should not be limited when initializing the empty stack. A more reasonable approach is to first allocate a basic capacity for the stack, and then apply the process, when the stack of space is not enough to use and then expand by Segment. To do this, you can set two constants: Stack_init_size (the initial amount of storage space) and stackincrement (storage allocation increment).

<span style= "FONT-SIZE:18PX;" > #define STACK_INIT_SIZE 100//storage space Initial allocation # define Stackincrement 10//storage allocation increment typedef int SELEMTYPE;// Redefine selemtype to int typedef struct{//redefine sqstck to struct type selemtype *base;//stack bottom pointer selemtype *top;//stack top pointer int stacksize;// Maximum current usable capacity of the stack}sqstack</span>

Definition of a two-link stack

The most important link stack is the definition and implementation of the node type.

<span style= "FONT-SIZE:18PX;" >typedef int elemtype;//redefine elemtype as the int typedef struct NODE//redefine node for struct type {elemtype date;//defined node data domain struct node * next;//defines the pointer field of a node}node,*pointer;//defined by a pointer to the node struct type pointer</span>




Stack of DS

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.