Data Structure stack and Data Structure

Source: Internet
Author: User

Data Structure stack and Data Structure

Stack

1. Definition: Stack is a linear table that is only inserted or deleted at the end of the table. Therefore, for stacks, the end of a table has a special meaning, called the top of a stack. Correspondingly,

The header end is called the stack bottom. Empty Tables without elements are called empty stacks.

Suppose stack S = (a1, a2, a3,..., an), then a1 is the bottom element of the stack, and an is the top element of the stack. Elements in the stack are pushed to the stack in the order of a1, a2, a3,... and.

An element should be the top element of the stack. In other words, stack modification is performed based on the principle of "first-in-first-out. Therefore, the stack is also known as a linear table of backward-forward and first-out. Therefore, the data structure

Is:

#define STACK_INIT_SIZE 100struct BinTreeNode;struct StkNode;typedef enum {L, R}Tag;typedef struct StkNode{    struct BinTreeNode *ptr;    Tag                 tag;}StkNode;#ifdef  PREORIN#define ElemTypeStack BinTreeNode*#else#define ElemTypeStack StkNode#endiftypedef struct Stack{    ElemTypeStack *base;    size_t    capacity;    int       top;}Stack;

2. Therefore, the stack has the following operations:

bool IsFull(Stack *st);bool IsEmpty(Stack *st);void InitStack(Stack *st, int sz);void PushStack(Stack *st, ElemTypeStack x);void ShowStack(Stack *st);void PopStack(Stack *st);ElemTypeStack GetTop(Stack *st);void ClearStack(Stack *st);

The above methods have the following operations: (1) check whether the stack is full; (2) check whether the stack is empty; (3) initialize a stack operation; (4) Report to the stack.

Push element. (5) display content in stack. (6) Delete element in stack. (7) Get element at stack top. (8) Clear stack.

3. Implement the method stated above:

Bool IsFull (Stack * st) {return st-> top> = st-> capacity;} bool IsEmpty (Stack * st) {return st-> top = 0 ;} void InitStack (Stack * st, int sz = STACK_INIT_SIZE) {st-> capacity = sz> STACK_INIT_SIZE? Sz: STACK_INIT_SIZE; st-> base = (ElemTypeStack *) malloc (sizeof (ElemTypeStack) * st-> capacity); assert (st-> base! = NULL); st-> top = 0;} void PushStack (Stack * st, ElemTypeStack x) {if (IsFull (st) {cout <"the Stack is full, "<x <" cannot be added to the stack. "<endl; return;} st-> base [st-> top ++] = x;} void ShowStack (Stack * st) {for (int I = STACK_INIT_SIZE-1; i> = 0; -- I) {cout <I <":"; if (I> = st-> top) cout <"Nul. "<endl; else cout <st-> base [I] <". "<endl ;}void PopStack (Stack * st) {if (IsEmpty (st) {cout <" the Stack is empty and cannot be added to the Stack. "<endl; return;} st-> top --;} El EmTypeStack GetTop (Stack * st) {assert (! IsEmpty (st); return st-> base [st-> top-1];} void ClearStack (Stack * st) {st-> top = 0 ;}

 

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.