Chapter 4: 1. The definition of stack and sequential stack

This article refers to "Dahua Data Structure", thanks to the author Mr. Cheng Jie.

One: The definition of the stack:
1. The stack is a linear table limited to insert and delete operations only at the end of the table.

Stack top: the end that allows insertion and deletion; the
bottom of the stack: the end that does not allow insertion and deletion;
empty stack: the stack that does not contain any data elements;

2. The stack is also called the last in first out linear table, or LIFO structure for short;
the insertion operation of the stack is called push, also called push and push;
the delete operation of the stack is called out Stack (pop), also called pop stack;

Insert picture description here
Two: the sequential storage structure of the stack and its implementation:

typedef int SElemType; 	/* SElemType类型根据实际情况而定,这里假设为int */
/* 顺序栈结构 */
typedef struct
{
    
    
	SElemType data[MAXSIZE];
	int top; 		/* 用于栈顶指针 */
}SqStack;

Example: Assuming that the MAXSIZE of a stack is 5, the general situation, empty stack and full stack of the test stack are as follows:
Insert picture description here
1. Stack operation:

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

2. Stack operation:

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

Three: Sequence Stack:
Code:

/*
 
本篇文章参考的是《大话数据结构》,感谢作者程杰先生。

*/

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

#include "bigtalk_data_structure.h"


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

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

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

static Status visit(SElemType c)
{
    
    
	printf("%d ",c);
	return OK;
}

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

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

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

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

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

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

	return OK;
}

/* 若栈不空,则删除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;
}

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

//栈:顺序栈;
void test_main_4_4()
{
    
    
	
	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	

	int j;
	SqStack s;
	int e;
	
	if(InitStack(&s) == OK)
	{
    
    
		printf("[%s:%d]:[yang] ********1****** \n",__FUNCTION__,__LINE__);	

		for(j = 1;j <= 10; j++)
		{
    
    
			//printf("\n[%s:%d]:[yang] Push j = %d \n",__FUNCTION__,__LINE__,j);
			Push(&s,j);
		}
	}		
	printf("栈中元素依次为:");
	StackTraverse(s);
	printf("[%s:%d]:[yang] StackLength(s) = %d\n",__FUNCTION__,__LINE__,StackLength(s));

	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	

	Pop(&s,&e);
	printf("弹出的栈顶元素 e = %d\n",e);
	printf("[%s:%d]:[yang] StackLength(s) = %d\n",__FUNCTION__,__LINE__,StackLength(s));

	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	
	printf("栈空否:%d (1:空 0:否)\n",StackEmpty(s));


	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	

	GetTop(s,&e);
	printf("栈顶元素 e = %d 栈的长度为%d\n",e,StackLength(s));
	

	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	

	ClearStack(&s);
	printf("清空栈后,栈空否:%d(1:空 0:否)\n",StackEmpty(s));

	#if 0


	#endif

    return 0;
}




print:

[main:14]:[yang] ***************************************** 
[test_main_4_4:123]:[yang] ******************* 我是分割线******************* 
[test_main_4_4:131]:[yang] ********1****** 
栈中元素依次为:1 2 3 4 5 6 7 8 9 10 
[test_main_4_4:141]:[yang] StackLength(s) = 10
[test_main_4_4:143]:[yang] ******************* 我是分割线******************* 
弹出的栈顶元素 e = 10
[test_main_4_4:147]:[yang] StackLength(s) = 9
[test_main_4_4:149]:[yang] ******************* 我是分割线******************* 
栈空否:0 (1:0:)
[test_main_4_4:153]:[yang] ******************* 我是分割线******************* 
栈顶元素 e = 9 栈的长度为9
[test_main_4_4:159]:[yang] ******************* 我是分割线******************* 
清空栈后,栈空否:1(1:0:)

Guess you like

Origin blog.csdn.net/yanghangwww/article/details/110732221