Target
in this chapter,you will learn that:identify the characteristics of the stackImplementation Stackusing stacks to solve programming problems
What is a stack?
a stack is a data structure that can access only the data at its end, which is also called the top. data can only be inserted and deleted at the top of the operation. the most recently inserted data will be deleted first. Therefore, the stack is also known as a LIFO data structure (Last-in-first-out).
the following two basic operations can be used on a stack:PUSH(push):into the stackPOP(out) :out of the stack
PUSH: It is the process of inserting a new element at the top of the stack.
POP: It is the process of removing elements from the top of the stack.
some of the real-lifeLIFOexamples of principles.
Answer:a pile of books: Suppose there is a stack of books together. When you want to take out a book, you must first take outthe other books above. Similarly, when you put another book, it will be placed at the top of the pile of books. a bunch of plates .: The first plate is placed at the bottom, then the second plate is placed on the first plate, and aThree plates are placed on the second plate, and so on. Generally, if you want to be in this pile of platesTo add a new plate, you have to put the new plate on top. Similarly, if you want to remove aplate, you must first remove the top plate. bracelet on the hand: When a man wears a bracelet, he can only remove the last bracelet he put on.
Implementation Stack
you need to develop a method to check if the parentheses in an arithmetic expression are properly nested. How do you solve this problem? you can easily resolve this issue by using the stack.
consider an example. the expression is assumed to be:{(A + B)x(c + D) + (cx d)]}Check this expression from left to right. the first check-in entry is'{', it is an opening parenthesis. add it to the stack.
The stack is similar to a list that allows only add or delete operations at one end. so, like with lists, stacks can be implemented using arrays and link lists.
to use an array to implement the stack:declares an array:
int stack[5];
//
need to pre-define maximum sizedeclare a variable to hold the index of the top data in the stack:
int top;initially, when the stack is empty, set:
top =–1
to avoid stack overflow, you need to check if the stack is full before adding elements to the stack. Let's modify this algorithm to check for this condition.
Problem Description:write a program to implement a stack by using an array that requires the stack to hold5an element. So-kinsoku-overflow:1 ' >
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;namespace Check if parentheses match {public partial class Form1:form {public F Orm1 () {InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) {string expression; stack<char> leftstack = new stack<char> (); expression = TxtExpression.Text.Trim (); bool Isright = true; char c; for (int i = 0; i < expression. Length; i++) {c = expression[i]; If the opening parenthesis if (Isleft (c)) {Leftstack.push (c); } else if (Isright (c))//If the right parenthesis {//If the stack is empty, indicates that there is an extra closing parenthesis if (Leftstack.count = = 0) {Txtresult.text ="Expression error: There is an extra closing parenthesis" + c.tostring (); Isright = false; Break Return } else if (Ispipei (Leftstack.peek (), C))//Determine if the closing parenthesis of the fetch matches the left parenthesis of the top of the stack { Leftstack.pop (); } else {txtresult.text = "expression error: Closing parenthesis" + c.tostring () + "with opening parenthesis" + Leftstack.peek (). ToString () + "mismatch"; Isright = false; Break Return }}} if (Leftstack.count = = 0)//&& isright==true) { Txtresult.text = "expression is correct"; } else {txtresult.text = "expression error: There is extra left parenthesis"; } } Determines whether the passed character is an opening parenthesis bool Isleft (char c) {if (c = = ' (' | | c = = ' {' | | c = = ') { return true; } else {return false; }}//Determine if the passed character is a closing parenthesis bool Isright (char c) {if (c = = ') ' | | c = = '} ' | | c = = '] ') {return true; } else {return false; }}//To determine if the left and right brackets are matched with bool Ispipei (char left, char ' right) {//First you need to verify that Ieft is an opening parenthesis if (Isleft (left) && isright (right)} {if (left = = ' (' && right = = ') )' || left = = ' {' && right = = '} ' | | left = = ' [' && right = = '] ' {return true; } else {return false; }} else {throw new Exception ("Left should be an opening parenthesis, right should be the closing parenthesis"); } } }}
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;namespace Multi-binary conversion {public partial class Form1:form {public Form 1 () {InitializeComponent (); private void Btntransform_click (object sender, EventArgs e) {string Digitchar = "0123456789ABC DEF "; int number;//holds 10 binary number int d;//deposit binary number stack<char> Stack = new stack<char> (); string result = NULL; Store the converted result number = Int32.Parse (Txtnumber.text); D = Int32.Parse (Txtd.text); The first step is to put each digit of the conversion result in the stack do {stack. Push (digitchar[number% d]); Number = number/d; } while (number! = 0); The second step is to stack each digit stored in the stack and add it to the end of the result string (stack. Count! = 0) {result = result + stack. Pop (); } Txtresult.text = result; } }}
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using system.windows.forms;namespace Remove train Compartment {public partial class Form1:form {Stack<c Har> Resultstack; Public Form1 () {InitializeComponent (); } private void Btnremove_click (object sender, EventArgs e) {char removechar; stack<char> middlestack = new stack<char> (); if (Resultstack = = null) {Resultstack = new stack<char> (); Resultstack.push (' A '); Resultstack.push (' B '); Resultstack.push (' C '); Resultstack.push (' D '); Resultstack.push (' E '); } Removechar = txtremove.text[0]; while (Resultstack.count! = 0) {//If the number you want to remove equals the number of the top element of the stack if (Removechar = = Resultstac K.peek ()) {Resultstack.pop (); Break } else//if the number to be removed is not equal to the number of the top element of the stack {//the stack top element of the result stack is stacked, and the element is placed in the middle stack Middlestack.push (Resultstack.pop ()); }} while (Middlestack.count! = 0) {Resultstack.push (Middlestack.pop ()); } Txttrain.text = ""; foreach (char c in resultstack) {Txttrain.text = Txttrain.text + c.tostring (); }//reverse the result string}}}
Seven stacks of data structures and algorithms