Data structure - sequence of stack

Sequence of stack memory structure is implemented using a sequential stack, i.e. using a set of consecutive addresses sequentially storing unit storing the data from the stack bottom to the top of the stack of elements. Respectively, using the top pointer and base pointer points to the top and bottom of the stack.

  • The stack is empty signs are: Top == Base.
  • Note that: stack pointer always points to a non-empty stack on a position of the top element .
  • Stack processing method when full:
    • 1, error, return to the operating system.
    • 2, allocated more space, storage space as a stack, the stack of original content into the new stack.

1. The stack represents the sequence

#define  MAXSIZE  100
typedef struct
{
    SElemType   *base;
    SElemType   *top;
    int stacksize;
}SqStack

2. Stack initialization sequence

  • Algorithm steps
    • 1, allocated space and check whether the space allocation fails, an error is returned on failure
    • 2, the bottom of the stack and stack pointer is provided: S.top = S.base; be noted here that the statement should not be S.top = S.base S.base = S.top
    • 3, the size of the stack is provided
  • Algorithm Description
int InitStack(SqStack &S)
{
	S.base=new int[MAX];  // 为顺序栈动态分配一个最大容量为MAX的数组空间
	if(!S.base)
	{
		return 0;
	}

	S.top=S.base;
	S.stacksize=MAX;
	return 1;
}

2. The order is determined whether the stack is empty

int StackEmpty( SqStack S )
{
    if(S.top == S.base) 
        return 0;  // 为空
    else 
        return 1;  // 非空
}

3. The order of stack required length

int StackLength(SqStack S)
{
    return S.top – S.base;
}

4. Empty sequence of stack

int ClearStack(SqStack S)
{
    if(S.base) 
        S.top = S.base;
	
    return 1;
}

The order of destruction of the stack

Status DestroyStack(SqStack &S)
{
    if(S.base)
    {
	    delete S.base ;
	    S.stacksize = 0;
	    S.base = S.top = NULL;
    }
  
    return 1;
}

6. The sequence of stack push

  • Algorithm steps
    • 1. If the stack is full, if it is full of errors
    • 2, onto stack element e
    • 3, a stack pointer plus
  • Algorithm Description
int Push_S(SqStack &S,int e)
{
    //将元素e入栈 
    if(S.top-S.base==S.stacksize)  //判断栈是否满 
    {
        return 0;
    }
	
    *S.top++=e;   // *S.top=e;  S.top++;
    return 1;
}

7. The sequence of stack pop

  • Algorithm steps
    • 1, it is determined whether the empty stack, if an error is null 
    • 2, the stack pointer is decremented
    • 3, the top element from the stack
  • Algorithm Description
//出栈
int Pop_S(SqStack &S,int &e)
{
	//用e返回出栈的元素
    if(S.top==S.base)
    {//栈空 
        return 0;
    } 
	
    e=*--S.top;  // --S.top;e=*S.top;
    return 1;
}

8. To remove the top element

  • Algorithm steps
    • Determine whether the empty stack, empty if an error is returned, otherwise get through the top element stack pointer
  • Algorithm Description
int GetTop( SqStack S, SElemType &e)  
{
    if( S.top == S.base )	 
        return 0; 	// 栈空
    e = *(S.top – 1);
    return 1;
}

9. code implementation

  • main.cpp
#include <iostream>

using namespace std;

#define MAX 100

//顺序栈的定义 
typedef struct 
{
	int *base;
	int *top;
	int stacksize;
}SqStack;

//初始化
int InitStack(SqStack &S) 
{
	S.base = new int[MAX]; // 为顺序栈动态分配一个最大容量为MAX的数组空间
	if (!S.base) 
	{
		return 0;
	}
	
	S.top = S.base;//初始化将栈置空,就是让头指针指向尾指针,从语句上讲就是把尾指针的值赋值给头指针 
	S.stacksize = MAX;
	
	return 1;
}

//入栈
int Push_S(SqStack &S, int e) 
{
	//将元素e入栈 
	if (S.top - S.base == S.stacksize) // 判断栈是否满 
	{
		return 0;
	}
	*S.top++ = e;   // *S.top=e;  S.top++;
	
	return 1;
}

//出栈
int Pop_S(SqStack &S, int &e) 
{
	//用e返回出栈的元素
	if (S.top == S.base) // 判断栈是否为空 
	{                
		return 0;
	}
	e = *--S.top;   // --S.top;e=*S.top;
	
	return 1;
}

// 取栈顶元素
int GetTop( SqStack S, int &e)  
{
	if (S.top == S.base)
		return 0; 	// 栈空
	
	e = *(S.top - 1);
	
	return 1;
}

int main()
{
	SqStack S;

	if (InitStack(S)) 
	{
		printf("顺序栈初始化成功!\n");
	}
	else 
	{
		printf("顺序栈初始化失败!\n");
	}

	int loop = 1;
	int e1;
	int i = 1;
	
	// 入栈
	printf("请输入入栈元素(输入0终止):\n");
	while (loop) 
	{
		printf("请输入第%d个元素值:",i++);
		scanf("%d", &e1);
		if (e1 != 0) 
		{
			Push_S(S,e1);

		}
		else if (e1 == 0) 
		{
			loop = 0;
		}
	}

	// 取栈顶元素
	int e2;
	GetTop(S, e2);
	printf("栈顶元素是:%d\n", e2);

	// 出栈
	int e3; int j = 1;
	while (S.top != S.base)
	{
		Pop_S(S, e3);
		printf("第%d个出栈的元素是:%d\n",j++,e3);
	}
	
	system("pause");
	
	return 0;
}
  • operation result

Guess you like

Origin blog.csdn.net/qq_22847457/article/details/94354768