Data structure: stack-basic function realization (C++ realization)

Here is gossip: Computer Xiaobai tried to publish his own article for the first time, and after thinking about it, he felt that the data structure of the stack was the most appropriate. The following is the simplest function implementation of the stack, including stack initialization, judging that the stack is full and empty, pushing the stack and popping the stack, etc., for reference only~

 

1. The structure of the stack:

    The stack implemented here is the most basic sequential stack. An array is used to store the elements of the stack, and the top of the stack and the bottom of the stack are defined.

typedef struct {
	SElemType elem[STACK_INIT_SIZE];
	int top, base;
}SqStack;

 

Two, the basic operation of the stack data structure:

    1. Initialize the stack : make the top subscript of the stack equal to the subscript base of the bottom of the stack, so that both are zero at the same time

Status InitStack(SqStack& s) {
	s.top = s.base = 0;
	return OK;
}

    2. Judge whether the stack is full : If the subscript top on the top of the stack is equal to the amount of storage space in the stack, then the stack is judged to be full

bool StackFull(SqStack s) {
	return s.top == STACK_INIT_SIZE;
}

    3. Determine whether the stack is empty : if the top of the stack is equal to the bottom of the stack, it is judged as the stack is empty

bool StackEmpty(SqStack s) {
	return s.top == s.base;
}

    4. Push the stack : first judge whether the stack is full, if the stack is not full, assign the inserted element to the element pointed to by top, and finally add one to top.

Status Push(SqStack& s, SElemType e) {
	if (StackFull(s)) {
		cout << "栈满。" << endl;
		return ERROR;
	}
	s.elem[s.top++] = e;
	return OK;
}

    5. Pop the stack : delete the top element of the stack, store the top element first, and then subtract one from the subscript top of the top of the stack.

//删除栈顶元素,并用e返回其值,复杂度为O(1)
Status Pop(SqStack& s, SElemType& e) {
	GetTop(s,e);
	s.top--;
	return OK;
}

    6. Return the stack length : top is the stack length.

//返回栈长,复杂度为O(1)
int StackLength(SqStack s) {
	return s.top;
}

    7. Return the value of the top element of the stack : first determine whether the stack is empty, if the stack is not empty, store the element pointed to by top, and finally reduce top by one.

//用e返回栈顶元素的值,复杂度为O(1)
Status GetTop(SqStack s, SElemType& e) {
	if (StackEmpty(s)) {
		cout << "栈空。" << endl;
		return ERROR;
	}
	e = s.elem[s.top - 1];
	return OK;
}

    8. Set to empty stack : Assign the subscript base at the bottom of the stack to the subscript top at the top of the stack.

//置为空栈,复杂度为O(1)
Status ClearStack(SqStack& s) {
	s.top = s.base;
	return OK;
}

    9. Reload the output stack : first judge whether the stack is not empty, if the stack is not empty, output each element in the stack in turn.

//重载输出栈,复杂度为0(n)
ostream& operator<<(ostream& out, SqStack s) {
	if (StackEmpty(s)) {
		out << "栈空。" << endl;
		return out;
	}
	out << "(";
	for (int i = 0; i < s.top; i++)
		out << s.elem[i] << ",";
	out << "\b)";
	return out;
}

    10. Operation results and inspection:

①Inspection problem: The basic operation of inspection is as follows

  1. Put 6, 5, 4, 3, 2, 1 into the stack and output the stack;
  2. Pop stack
  3. Return the top element of the stack;
  4. Get the stack length;
  5. Empty the stack;

②Theoretical results:

  1. S1: (6, 5, 4, 3, 2, 1) ;
  2. After deleting S1: (6, 5, 4, 3, 2), the deleted element is: 1;
  3. The top element of the stack is: 2;
  4. The S1 stack length is: 5;
  5. The stack is empty.

③ Actual results:

    After testing, the experimental results are consistent with the theory.


This concludes the basic operations on the stack! The next article is the application of the stack, including numerical conversion, bracket matching, line editing program, maze solving, expression evaluation, interested friends can continue to browse~

Here is the link: https://blog.csdn.net/jokerCe/article/details/114141469

Guess you like

Origin blog.csdn.net/jokerCe/article/details/114139123