C language stack (stack chain)

Chain stack operation generally contain: the stack, stack, the stack initialization, determines whether the stack is empty, empty the stack below the first part of the code declaration

#include <stdio.h>
#include <stdlib.h>
#define Empty 0        /* 栈空 */
#define Avail 1        /* 栈可用 */

struct SNODE typedef
{
    int Data;
    struct * Next SNODE;
} StackNode;
typedef struct LStack
{
    StackNode * Top; / * stack pointer * /
    StackNode * bottom; / * end of the stack pointer * /
    int height; / * chain Stack Height * /
} LinkStack;

LinkStack InitStack (LinkStack pStack); / * stack pointer, the stack pointer is a bottom, the stack height initialization * /
LinkStack the Push (LinkStack pstack); / * push * /
LinkStack Pop (LinkStack pstack); / * the stack * /
int StackEmpty (LinkStack pStack); / * is determined whether the stack is empty * /
LinkStack DeletStack (LinkStack pStack); / * empty stack * /
void DisplyStack (LinkStack pStack); / * ---- traverse the stack from top to bottom * /

A statement of nodes

1 typedef struct SNode
2 {
3    int data;
4    struct SNode *next;
5 }StackNode;

Statement stack node chain with the same statement way linked list, and the pointer is a data field domains, not repeated here

Second, the top of the stack, bottom of the stack, the stack height of the statement

Typedef struct LStack. 1
2 {
. 3 * Top StackNode; / * stack pointer * /
. 4 * StackNode bottom; / * end of the stack pointer * /
. 5 int height; / stack height chain * * /
. 6} LinkStack;

Third, the function declaration

1 LinkStack InitStack (LinkStack pStack); / * stack pointer, the stack pointer is a bottom, the stack height initialization * /
2 LinkStack the Push (LinkStack pstack); / * push * /
. 3 LinkStack Pop (LinkStack pstack); / * pop * /
. 4 int StackEmpty (LinkStack pstack); / * is determined whether the stack is empty * /
. 5 LinkStack DeletStack (LinkStack pstack); / * empty stack * /
. 6 DisplyStack void (LinkStack pstack); / * ---- traversable stack top * to the end /

The difference between the stack and the chain way linked list

As already mentioned chain stack is singly linked list of operating a limited (nonsense ·), let's look at the process of establishing a one-way linked list (can not clear before the first one-way linked list of another essay list (to establish a one-way linked list, delete, insert, print)), in a one-way linked list when adding a new node is the last node in the list of original

Pointer field points to the new node and the new node pointer field set to NULL as the last node in the list, while the chain stack when adding a new node to operate not the same, first to stack the analysis operation, but the stack top of the stack to do insertions and deletions, then the top of the stack on the head or tail of the list it? The one-way linked list head pointer has

The stack pointer is a must, then put the stack pointer as a pointer to the first use, the better approach is to stack into a single head of the list. Also top of the stack in the head, then the head node of a single list of loses its meaning, usually for a chain stack, the head node is not required, it is now operating a chain stack add a new node

Chain stack: a new node -> the node pointer field points to the new top of the stack of the original node -> the stack pointer is moved to the new node

Singly linked lists: a new node -> the original pointer field of the last node in the list point to a new node -> the node pointer field is set to NULL as the new last node of the new list

For convenience of the reader a more intuitive understanding of this process the following chart:

Chain stack operation portion

First, the stack

/* Function: 入栈 */
LinkStack Push (LinkStack pStack)
{
    int data;
    StackNode *temp;

    IF ((TEMP = (StackNode *) the malloc (the sizeof (StackNode))) == NULL)
    {
        the printf ( "insufficient memory space \ n-");
        return pstack;
    }
    IF (StackEmpty (pstack) == Empty) / * if the stack is empty * /
    {
        pStack.top = pStack.bottom = TEMP; / * the stack, the stack pointer points to the end of the new node * /
        temp-> Next = NULL; / * node pointer field is empty * /
        the printf ( "Please Data INPUT ");
        Scanf ("% D ", & Data);
        pStack.top-> Data = Data;
        pStack.height ++;

        pstack return;
    }
    the else / * the stack is not empty * /
    {
        temp-> Next = pStack.top; / * new node pointing to the original stack * /
        pStack.top = TEMP; / * pointer to the new node stack * /
        the printf ( "Please INPUT Data");
        Scanf ( "% D", & Data);
        pStack.top-> Data = Data;
        pStack.height ++;

        return pStack;
    }
}

Second, the stack

/* Function: 出栈 */
LinkStack Pop (LinkStack pStack)
{
    StackNode *Second;

   
    if (StackEmpty (pStack) == Empty ) / * is determined whether the stack is empty * /
    {
        the printf ( "the stack is empty, not popping \ n-");
        return pstack;
    }
    IF (pStack.top == pStack.bottom) / * If the popped element is the last element * /
    {
        the printf ( "pop elements D% \ n-", pStack.top-> Data);
        Free (pStack.top);
        pStack.top = pStack.bottom = NULL; / * the stack, the stack pointer is set to the bottom are empty * /
        pStack.height--;

        pstack return;
    }
    the printf ( "pop elements D% \ n-", pStack.top-> Data);
    Second pStack.top- => Next; / * point to the previous element in the stack * /

    free (pStack.top); / * release the stack node * /
    pStack.top = Second; / * pointer to the head node of the new top of stack * /
    pStack.height--;

    return pStack;
}

Three cases need to determine when the stack, the first case: the stack is empty, the second case: only one element in the stack, the third case: the stack elements of two greater than or equal

Third, determine whether the stack is empty

/ * Function: determining whether the stack is empty * /
int StackEmpty (LinkStack pstack)
{
    IF (pStack.top == NULL && pStack.bottom == NULL)
    {
        return Empty;
    }
    the else
    {
        return Avail;
    }
}

Fourth, the traversal stack

/ * Function: traversing the stack top in the end * /
void DisplyStack (LinkStack pstack)
{
    IF (StackEmpty (pstack) == Empty)
    {
        the printf ( "the stack is empty, can not traverse \ n-");
        return;
    }
    the printf ( "Stack elements [ ");
    the while (pStack.top = NULL)!
    {
        the printf ("% D-> ", pStack.top-> Data);
        pStack.top = pStack.top-> Next;
    }
    the printf ("] \ n-");
}

Five empty stack

/ * Function: Kiyoshisora栈* /
LinkStack DeletStack (LinkStack pstack)
{
    StackNode * del;

    the while (pStack.top = NULL!)
    {
        del = pStack.top-> Next; / * previous node stack node * /
        Free (pStack.top); / * release the node * /
        pStack.top = del; / * stack pointer is moved to the top of stack * /
    }

    return pStack;
}

Six, initializing the stack, the stack pointer and the stack height of the bottom

/ * Function: initializing the stack, the stack bottom, the stack height * /
LinkStack InitStack (LinkStack pstack)
{
    pStack.top pStack.bottom = = NULL;
    pStack.height = 0;

    return pStack;
}

Chain stack to achieve complete code

#include <stdio.h>
#include <stdlib.h>
#define Empty 0        /* 栈空 */
#define Avail 1        /* 栈可用 */

struct SNODE typedef
{
    int Data;
    struct * Next SNODE;
} StackNode;
typedef struct LStack
{
    StackNode * Top; / * stack pointer * /
    StackNode * bottom; / * end of the stack pointer * /
    int height; / * chain Stack Height * /
} LinkStack;

LinkStack InitStack (LinkStack pStack); / * stack pointer, the stack pointer is a bottom, the stack height initialization * /
LinkStack the Push (LinkStack pstack); / * push * /
LinkStack Pop (LinkStack pstack); / * the stack * /
int StackEmpty (LinkStack pStack); / * is determined whether the stack is empty * /
LinkStack DeletStack (LinkStack pStack); / * empty stack * /
void DisplyStack (LinkStack pStack); / * ---- traverse the stack from top to bottom * /

int main()
{
    LinkStack p;
    char ch;

    p.height = 0; / * initialize the stack height to zero * /
    P = InitStack (P); / * initialize the stack * /
    the printf ( "Stack Push the Do you want to (the Y / N)?");
    Scanf ( "% C ", & CH);
    the while (CH == 'the Y' || CH == 'Y')
    {
        P = the push (P); / * push * /
        DisplyStack (P); / * traversable stack * /
        the printf ( "the Do you want to Push Stack (the Y / N)?");
        Scanf ( "% C", & CH);
    }
    the printf ( "the Do you want to POP Stack (the Y / N)?");
    Scanf ( "% C ", & CH);
    the while (CH == 'the Y' || CH == 'Y')
    {
        P = Pop (P); / * the stack * /
        DisplyStack (P); / * traversable stack * /
        the printf ("Do you want to pop stack(Y/N)?");
        scanf(" %c", &ch);
    }

    0 return;
}
/ * Function: initializing the stack, the stack bottom, the stack height * /
LinkStack InitStack (LinkStack pstack)
{
    pStack.top pStack.bottom = = NULL;
    pStack.height = 0;

    return pStack;
}

/ * Function: determining whether the stack is empty * /
int StackEmpty (LinkStack pstack)
{
    IF (pStack.top == NULL && pStack.bottom == NULL)
    {
        return Empty;
    }
    the else
    {
        return Avail;
    }
}

/* Function: 入栈 */
LinkStack Push (LinkStack pStack)
{
    int data;
    StackNode *temp;

    IF ((TEMP = (StackNode *) the malloc (the sizeof (StackNode))) == NULL)
    {
        the printf ( "insufficient memory space \ n-");
        return pstack;
    }
    IF (StackEmpty (pstack) == Empty) / * if the stack is empty * /
    {
        pStack.top = pStack.bottom = TEMP; / * the stack, the stack pointer points to the end of the new node * /
        temp-> Next = NULL; / * node pointer field is empty * /
        the printf ( "Please Data INPUT ");
        Scanf ("% D ", & Data);
        pStack.top-> Data = Data;
        pStack.height ++;

        pstack return;
    }
    the else / * the stack is not empty * /
    {
        temp-> Next = pStack.top; / * new node pointing to the original stack * /
        pStack.top = TEMP; / * pointer to the new node stack * /
        the printf ( "Please INPUT Data");
        Scanf ( "% D", & Data);
        pStack.top-> Data = Data;
        pStack.height ++;

        return pStack;
    }
}

/* Function: 出栈 */
LinkStack Pop (LinkStack pStack)
{
    StackNode *Second;

   
    if (StackEmpty (pStack) == Empty ) / * is determined whether the stack is empty * /
    {
        the printf ( "the stack is empty, not popping \ n-");
        return pstack;
    }
    IF (pStack.top == pStack.bottom) / * If the popped element is the last element * /
    {
        the printf ( "pop elements D% \ n-", pStack.top-> Data);
        Free (pStack.top);
        pStack.top = pStack.bottom = NULL; / * the stack, the stack pointer is set to the bottom are empty * /
        pStack.height--;

        pstack return;
    }
    the printf ( "pop elements D% \ n-", pStack.top-> Data);
    Second pStack.top- => Next; / * point to the previous element in the stack * /

    free (pStack.top); / * release the stack node * /
    pStack.top = Second; / * pointer to the head node of the new top of stack * /
    pStack.height--;

    return pStack;
}

/ * Function: traversing the stack top in the end * /
void DisplyStack (LinkStack pstack)
{
    IF (StackEmpty (pstack) == Empty)
    {
        the printf ( "the stack is empty, can not traverse \ n-");
        return;
    }
    the printf ( "Stack elements [ ");
    the while (pStack.top = NULL)!
    {
        the printf ("% D-> ", pStack.top-> Data);
        pStack.top = pStack.top-> Next;
    }
    the printf ("] \ n-");
}

/ * Function: Kiyoshisora栈* /
LinkStack DeletStack (LinkStack pstack)
{
    StackNode * del;

    the while (pStack.top = NULL!)
    {
        del = pStack.top-> Next; / * previous node stack node * /
        Free (pStack.top); / * release the node * /
        pStack.top = del; / * stack pointer is moved to the top of stack * /
    }

    return pStack;
}

Guess you like

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