[Data structure] Implementation of sequential stack in C language

Hello, everyone, I haven't blogged for a long time. I've been busy lately. I mainly focus on java, and the other part is on data structure. At the same time, I have to deal with some school courses, such as high numbers and discrete things. , resulting in not blogging for a long time. I will write some blogs about java and data structures and algorithms in the future.

Well, without further ado, today I'm talking about some basic operations on the sequential stack of data structures in c language.

Definition of sequential stack

First of all, let's take a brief look at the sequential stack. We know that the linear table is divided into sequential list and singly linked list according to sequential storage or chain storage. Similarly, according to the different storage methods, we divide the stack into sequential storage stacks. It is called a sequential stack, and a chain stored in a chain is called a chain stack. What we're talking about is the sequential stack. In fact, with some knowledge of the previous linear table, it is relatively easy to understand the operation of the stack.

Understanding of sequential stack

Question comes? How do we define it? Usually we can use an array and a variable that records the position of the top element of the stack, and the top position of the stack uses the integer variable Top to record the subscript value of the current top element of the stack. When Top==-1, it means an empty stack. When top==MAXSIZE-1, it means the stack is full. Well, let's start implementing the sequential stack.

Ready to work

1. Macro definition and its renaming

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */

typedef int Status; 
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */

2. Structure (representation of sequential stack)

/* 顺序栈结构 */
typedef struct
{
        SElemType data[MAXSIZE];
        int top; /* 用于栈顶指针 */
}SqStack;

Implementation

1. Initialization

/*  构造一个空栈S */
Status InitStack(SqStack *S)
{ 
        /* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */
        S->top=-1;
        return OK;
}

2. Empty

/* 把S置为空栈 */
Status ClearStack(SqStack *S)
{ 
        S->top=-1;
        return OK;
}

3. Determine whether it is empty

/* 若栈S为空栈,则返回TRUE,否则返回FALSE */
Status StackEmpty(SqStack S)
{ 
        if (S.top==-1)
                return TRUE;
        else
                return FALSE;
}

4. Find the length

/* 返回S的元素个数,即栈的长度 */
int StackLength(SqStack S)
{ 
        return S.top+1;
}

5. Find the top element of the stack

/* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR */
Status GetTop(SqStack S, SElemType* e)
{
    if (S.top == -1) {
        return ERROR;
    }
    else {
        *e = S.data[S.top];
        return OK;
    }
}

6. Push into the stack (judging whether it is full)

/* 插入元素e为新的栈顶元素 */
Status Push(SqStack* S, SElemType e)
{
    if (S->top == MAXSIZE - 1) /* 栈满 */
    {
        return ERROR;
    }
    S->top++;				/* 栈顶指针增加一 */
    S->data[S->top] = e;  /* 将新插入元素赋值给栈顶空间 */
    return OK;
}

7. Pop the stack (judging whether it is empty)

/* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
Status Pop(SqStack* S, SElemType* e)
{
    if (S->top == -1)
        return ERROR;
    *e = S->data[S->top];	/* 将要删除的栈顶元素赋值给e */
    S->top--;				/* 栈顶指针减一 */
    return OK;
}

8. Traverse

/* 从栈底到栈顶依次对栈中每个元素显示 */
Status StackTraverse(SqStack S)
{
    int i;
    i = 0;
    while (i <= S.top)
    {
        visit(S.data[i++]);
    }
    printf("\n");
    return OK;
}
Status visit(SElemType c)
{
    printf("%d ", c);
    return OK;
}

main function

int main()
{
    int j;
    SqStack s;
    int e;
    if (InitStack(&s) == OK)
        for (j = 1; j <= 10; j++)
            Push(&s, j);
    printf("栈中元素依次为:");
    StackTraverse(s);
    Pop(&s, &e);
    printf("弹出的栈顶元素 e=%d\n", e);
    printf("栈空否:%d(1:空 0:否)\n", StackEmpty(s));
    GetTop(s, &e);
    printf("栈顶元素 e=%d 栈的长度为%d\n", e, StackLength(s));
    ClearStack(&s);
    printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s));

    return 0;
}


Well, this time some knowledge of the sequence stack is over.

Guess you like

Origin blog.csdn.net/weixin_60478154/article/details/123809529