C Language Stack (Stack order)

Stack

Stack operation is a restricted linear table, the latter is advanced out of the data structure is defined only insertions and deletions at one end, called the top of the stack to allow an end of the operation, the operation is referred to as a bottom of the stack is not allowed

Stack sequence (sequence structure)

Stack order: with a contiguous storage space for the data elements in the stack, more common is implemented by an array of sequential stack

Sequential storage structure: storage space occupied by the elements must be continuous (consecutive herein refers to continuous logical, rather than physically continuous)

       2. In the position of the storage element is stored in a logical sequence

 
Implement sequential stack typically includes a portion

Code declaration section

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 5 / * Maximum capacity stack * /
#define Empty 0 / * empty * /
#define. 1 Full / full * * /
#define Avail -1 / * available * /

STA struct typedef
{
    int * Top; / * stack pointer * /
    int * bottom; / * end of the stack pointer * /
    int stack_size is; / * maximum capacity of the stack * /
} Stack;
Stack the Push (P Stack); / * into stack * /
void DisplyStack (stack P); / * traverse the stack of elements * /
stack Pop (stack P); / * the stack * /
stack InitStack (stack P); / * initialize the stack * /
int StackEmpty (stack P) ; / * is determined whether the stack is empty * /
int StackFull (stack P); / * is determined whether the stack is full * /

A stack of statements

The first:

Typedef struct STA. 1
2 {
. 3 int Stack [SIZE]; / * store the stack one-dimensional array of elements * /
. 4 int Top; / * store subscript * / stack element
5} stack;

(Here, only the top of the stack to point to a top position, can also use two variables base, top to bottom of the stack the stack space pointing position and a stack position)

The second essay is the second statement :( Benpian method used)

Typedef struct STA. 1
2 {
. 3 int * Top; / * stack pointer * /
. 4 * int bottom; / * end of the stack pointer * /
. 5 stack_size is int; / * maximum capacity of the stack * /
. 6} Stack;

Second, the stack initialization

/ * Function: Stack Initialization * /
Stack InitStack (Stack P)
{
    p.bottom = (int *) the malloc (* p.stack_size the sizeof (int));
    IF (p.bottom == NULL)
    {
        the printf ( "Initialization failed stack \ n-");
        Exit (0);
    }
    p.top = p.bottom;
    p.stack_size = MAX_SIZE;

    return p;
}

Third, the stack (push)

/ * Function: push * /
Stack the Push (Stack P)
{
    int Data;
    IF (StackFull (P) == Full)
    {
        the printf ( "the stack is full, can not push");
        return P;
    }
    the printf ( " Data INPUT please ");
    Scanf ("% D ", & Data);
    * = Data p.top;
    p.top ++;

    return p;
}

Fourth, the stack

/ * Function: Stack * /
Stack Pop (Stack P)
{
    IF (StackEmpty (P) == Empty)
    {
        the printf ( "Stack is empty stack, the stack can not");
        return P;
    }
    p.top--;
    printf ( "pop elements:% d \ n", * p.top);

    return p;
}

(The position of the stack pointer is an element of the stack pointer, it is necessary p.top-- at the stack, in order to point out the elements of the stack)

Fifth, determine whether the stack is empty

/ * Function: determining whether the stack is empty * /
int StackEmpty (Stack P)
{
    IF (p.top == p.bottom)
    {
        return Empty;
    }
    the else
    {
        return Avail;
    }
}

Sixth, to determine whether the stack is full

/ * Function: determining whether the stack is full * /
int StackFull (Stack P)
{
    IF (p.top - p.bottom == p.stack_size)
    {
        return Full;
    }
    the else
    {
        return Avail;
    }
}

Seven, traversing the stack elements

/ * Function: traversing the stack elements from the top of the stack to the bottom of the stack * /
void DisplyStack (Stack P)
{
    IF (StackEmpty (P) == Empty)
    {
        the printf ( "Stack stack is empty, can not traverse \ n");
        return;
    }
    the printf ( "the stack elements:");
    the printf ( "top [");
    the while (p.top = p.bottom!)
    {
        p.top--;
        the printf ( "% D-", * P .top);
    }
    the printf ( "] bottom \ n-");
}

Stack order to achieve - the full code

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 5 / * Maximum capacity stack * /
#define Empty 0 / * empty * /
#define. 1 Full / full * * /
#define Avail -1 / * available * /

STA struct typedef
{
    int * Top; / * stack pointer * /
    int * bottom; / * end of the stack pointer * /
    int stack_size is; / * maximum capacity of the stack * /
} Stack;
Stack the Push (P Stack); / * into stack * /
void DisplyStack (stack P); / * traverse the stack of elements * /
stack Pop (stack P); / * the stack * /
stack InitStack (stack P); / * initialize the stack * /
int StackEmpty (stack P) ; / * is determined whether the stack is empty * /
int StackFull (stack P); / * is determined whether the stack is full * /

int main()
{
    stack p;
    char ch;
   
    p.stack_size = MAX_SIZE;
    p = InitStack (p);    /* 初始化栈 */
    printf("Do you want to push to stack?(Y/N)");
    scanf(" %c", &ch);
    while (ch == 'Y' || ch == 'y')
    {
        p = Push (p);    /* 入栈 */
        DisplyStack (p);/* 打印栈中元素 */
        printf("Do you want to push to stack?(Y/N)");
        scanf(" %c", &ch);
    }
    printf("Do you want to pop (Y/N)");
    scanf(" %c", &ch);
    while (ch == 'Y' || ch == 'y')
    {
        p = Pop (p);
        DisplyStack (p);
        printf("Do you want to pop (Y/N)");
        scanf(" %c", &ch);
    }

    0 return;
}
/ * Function: determining whether the stack is empty * /
int StackEmpty (Stack P)
{
    IF (p.top == p.bottom)
    {
        return Empty;
    }
    the else
    {
        return Avail;
    }
}
/ * Function: Analyzing if the stack is full * /
int StackFull (stack P)
{
    iF (p.top - p.bottom == p.stack_size)
    {
        return full;
    }
    the else
    {
        return Avail;
    }
}
/ * Function: push * /
stack the push (stack P)
{
    int Data;
    IF (StackFull (P) == full)
    {
        the printf ( "the stack is full, can not push");
        return p;
    }
    printf("Please input data");
    scanf("%d", &data);
    *p.top = data;
    p.top++;

    P return;
}
/ * Function: Stack * /
Stack Pop (Stack P)
{
    IF (StackEmpty (P) == Empty)
    {
        the printf ( "Stack is empty stack, the stack can not");
        return P;
    }
    P. top--;
    the printf ( "pop elements:% d \ n", * p.top);

    P return;
}
/ * Function: Stack Initialization * /
Stack InitStack (Stack P)
{
    p.bottom = (int *) the malloc (* p.stack_size the sizeof (int));
    IF (p.bottom == NULL)
    {
        printf ( "stack initialization failed \ n-");
        Exit (0);
    }
    p.top = p.bottom;
    p.stack_size = MAX_SIZE;

    P return;
}
/ * Function: traversing the stack elements from the top of the stack to the bottom of the stack * /
void DisplyStack (Stack P)
{
    IF (StackEmpty (P) == Empty)
    {
        empty stack printf ( "stack, for not traverse \ n-");
        return;
    }
    the printf (" the stack elements: ");
    the printf (" top [ ");
    the while (p.top = p.bottom)!
    {
        p.top--;
        the printf ("% D- ", * p.top);
    }
    the printf ("] bottom \ n-");
}

Stack pointer points to two ways, one is the top element of the latter pointing element (herein, this is used), the other point is the top element, both empty and full conditions in the stack is determined , stack, the stack pointer when moving from the stack there are some differences.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159764.htm