Data structure stack and its usage (C language Implementation)

Source: Internet
Author: User

First, the definition of the stack

A stack is a linear table that restricts insert or delete operations only at the end of a table. Once to the stack, the end of the table has its special meaning, called the top of the stack, the corresponding

, the head end of the table becomes the bottom of the stack. Empty tables that do not contain elements are called empty stacks. inserting a new element into a stack, also known as a stack, a stack, or a stack, is to put a new element on top of the stack, making it a new stack top element, deleting an element from a stack, or making a stack or fallback, by removing the top element of the stack and making its adjacent elements a new stack top element.
second, the characteristics of the stack

The stack, as a data structure, is a special linear table that can only be inserted and deleted at one end. It stores data in accordance with the principle of advanced post-exit

, the first data is pushed into the bottom of the stack, the final data at the top of the stack, need to read the data from the top of the stack to eject the data (the last data is read out first). The stack has a memory function, and the stack's insertion and deletion operations do not need to change the bottom pointer. Therefore, we also call the stack as a LIFO, that is, the last in first out, abbreviated as the LIFO structure. Its structure is shown below:


Three, the basic operation of the stack

Since the stack is also a linear table, the storage structure of the linear table is also applicable to the stack, usually the stack has two storage structures, the sequential stack and the chain stack, the two kinds of storage nodes

The algorithm that implements the basic operation of the stack is different.

Sequential stacks, the sequential storage structure of stacks, are data elements that are stored sequentially from the bottom to the top of the stack using a contiguous set of storage units. In the sequential stack, there are

the concept of overflow and underflow. Sequential stacks like to put books in a box, you have to take the book from the top, if the book is placed in the box, more than the top of the joint venture, it is overflow. Anyway, if there is no book, we still go to get the book, then is underflow.

But the link stack does not have overflow limit, it only needs to operate in the head, we use the chain stack when we want to store a large number of data elements, the general situation

use the sequential stack. So let me give you an example of a sequential stack:

#include <iostream> #include <cstdio> #include <malloc.h>using namespace std;const int stack_size = 100      ; const int stackcreament = 10;typedef struct sqstack{int *base;       stack bottom pointer int *top; stack top pointer int stacksize;}    Sqstack;void Initstack (Sqstack *s)//constructs an empty stack {(*s). base = (int *) malloc (sizeof (int) * stack_size); if (! (    *s) (base) exit (0);    (*s). Top = (*s). Base; (*s). stacksize = Stack_size;}    void Destory (Sqstack &s)//destroys a stack {free (s.base);    S.base = NULL;    S.top = NULL; s.stacksize = 0;} void Clearstack (Sqstack &s)//emptying a stack {s.top = s.base;}    BOOL Emptystack (Sqstack &s)//Determine if a stack is empty {if (s.top = = s.base) return 1; return 0;} int Lengthstack (Sqstack &s)//Returns the length of this stack {return s.top-s.base;}    int GetTop (Sqstack &s)//fetch stack top element {if (s.base = = S.top) return 0; return * (S.TOP-1);} int push (sqstack &s, int data)//Insert new element as stack top element {if (s.top-s.base >= s.stacksize)//StackFull, append space {s.base = (int *) realloc (s.base, (s.stacksize+stackcreament) *sizeof (int));        S.top = S.base + s.stacksize;    S.stacksize + = stackcreament;    } * (s.top) = data; s.top++;}    int pop (Sqstack &s)//delete stack top element {if (s.base = = S.top) return 0; s.top--;}    int main () {sqstack s;    Initstack (&s);    for (int i=0;i<10;i++) push (S, i);        for (int i=0;i<10;i++) {int a = GetTop (s);        printf ("%d", a);    Pop (s);    } printf ("\ n");    if (Emptystack (s) = = 1) printf ("Not empty \ n");    int len = Lengthstack (s);    printf ("%d\n", Len);    Destory (s); return 0;}

examples of several typical applications of the stack: the conversion of the number, the check of bracket matching, the line editing program and so on, all using the advanced post-out features of the stack to solve.

Data structure stack and its usage (C language Implementation)

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.