Data structure (C implementation) ------- sequential Stack

Source: Internet
Author: User

A stack is a table that is only inserted or deleted at one end of the table. It is usually called a table that allows insertion. The deleted end is the top of the stack, the other end is called the bottom of the stack ). A stack without elements is called an empty stack.

Stack S = {A1, A2, A3,..., an} is called A1 as the bottom element of the stack, and an as the top element of the stack. According to the stack definition, the elements at the top of the stack always last in the stack and first out of the stack; the elements at the bottom of the stack always first in the stack and finally out of the stack. That is, the stack is based on the principle of "back-to-first-out. Therefore, the stack is also called a linear table of the last-in-first-out (LIFO.

The sequential stack refers to the sequential storage structure of the stack. It stores data elements from the bottom of the stack to the top of the stack in sequence using a set of address-connected storage units, and attaches the pointer top to indicate the position of the top element in the sequence table. Similar to a sequence table, one-dimensional data is used to describe the storage area of data elements in sequence, while the position at the top of the stack changes with insertion and deletion, which is usually represented by an integer, set the end of the array subscript to 0 as the bottom of the stack.


Sequence stack type description:

# Define maxsize 100 // maximum stack space // description of the ordered stack type typedef int elemtype; typedef struct {elemtype data [maxsize]; int top;} sqstack;

In the ordered stack, if the end of the array subscript 0 is set as the bottom of the stack, when the top value is-1, it indicates that the stack is empty, when an element is added to the stack, the top value is added to 1, and the data element is located in this position. Each time an element pops up from the stack, the top element of the stack is first taken out, and then the top value is reduced by 1, indicating the new top element of the stack.

 

Basic operations on the sequence Stack:

1. initialize the stack init_sqstack (sqstack * s)

The initialization of the ordered stack is to construct an empty ordered stack. You only need to set the top of the empty sequence stack s to-1, indicating that there are no data elements in the stack, and the algorithm complexity is O (1 ).

// Initialize the stack void init_sqstack (sqstack * s) {S-> Top =-1 ;}

2. Determine the empty stack isempty_sqstack (sqstack * s)

Because the bottom of the stack is set at the zero subscript of the array, that is, S-> data [0] indicates the bottom element of the stack. Therefore, when the stack is empty, the top pointer of the stack is Top =-1. If Top =-1, the stack is empty, and 1 is returned. Otherwise, the stack is not empty, 0 is returned, and the complexity of the algorithm is O (1 ).

// Judge the empty stack int isempty_sqstack (sqstack * s) {If (S-> Top =-1) return 1; elsereturn 0 ;}


3. Determine whether the stack is full isfull_sqstack (sqstack * s)

Because the maximum stack space is set, you must first determine whether the stack is full when you import the stack. That is, when Top = maxsize, it indicates that the stack is full and 1 is returned. Otherwise, if the stack is not empty, 0 is returned, and the complexity of the algorithm is O (1 ).

// Judge whether the stack is full int isfull_sqstack (sqstack * s) {If (S-> Top = maxsize) return 1; elsereturn 0 ;}

4. push_sqstack (sqstack * s, elemtype X)

First, check whether the stack is full. If so, output a prompt to end. Otherwise, add 1 to the top value of the pointer and save element X to the top position of the stack, the complexity of the algorithm is O (1 ).

// Void push_sqstack (sqstack * s, elemtype X) {// when the stack is full, exit if (isfull_sqstack (s) {printf ("the stack is full! \ N "); exit (0) ;}else {S-> data [++ (S-> top)] = x ;}}


5. Out-of-stack pop_sqstack (sqstack * s, elemtype * X)

First, check whether the stack is empty. If so, output the prompt and end. Otherwise, assign the top element of the stack to X and subtract the top pointer by 1 ,, the complexity of the algorithm is O (1 ).

// Out-of-stack void pop_sqstack (sqstack * s, elemtype * X) {// If the stack is empty, a prompt message is output and if (isempty_sqstack (s) is exited )) {printf ("Stack empty! \ N "); exit (0);} else * x = s-> data [S-> top --];}


6. Read the top element top_sqstack (sqstack * s, elemtype * X)

Both the read top element and the output stack operation obtain the value of the top element of the stack. The difference between the two is that when the top element of the stack is read, the top pointer of the stack does not change. Only the top element of the stack is read, in addition, the top elements of the stack must be deleted from the stack. At this time, the top pointer of the stack also needs to change. The time complexity is O (1)

// Read the top element void top_sqstack (sqstack * s, elemtype * X) {If (isempty_sqstack (s) {printf ("Stack empty! \ N "); exit (0);} else * x = s-> data [S-> top];}


7. output the entire stack print_sqstack (sqstack * s)

The output stack only needs to read from the top of the stack to the bottom of the stack and output all the data elements in the stack.

// Output the whole stack void print_sqstack (sqstack * s) {If (isempty_sqstack (s) {printf ("Stack empty! \ N "); exit (0);} int length = s-> top; while (length>-1) printf (" % d \ t ", s-> data [length --]); printf ("\ n ");}





Data structure (C implementation) ------- sequential Stack

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.