The application of the 11th lesson stack of data structure tutorial

Source: Internet
Author: User

the subject of this lesson: Stack Application

Teaching Purpose: Mastering the application of stack and understanding the important role of stack

Teaching Emphasis: using stack to implement line editing, using stack to implement expression evaluation

Teaching Difficulty: using the stack to realize the evaluation of expression

Teaching Content:

One, stack application one: the conversion of the numbering

The

Converts a decimal number to a number of other systems in a simple way:

Example: decimal conversion to octal: (10=) (102) 8

66/8=8 2

8/8=1 0

1/8=0 remaining 1

The result is a reverse order of the remainder: 102. The remainder in the final write out the results, the last to find the remainder of the first write, in line with the stack before the nature of the first, it can be used to implement the stack conversion:

void conversion () {

Psqstack S;

Selemtype e;

int n;

Initstack (&s);

printf ("Input a number to convert to oct:\n");

scanf ("%d", &n);

if (n<0)

{printf ("\nthe number must is over 0");

Return;}

If (!n) Push (s,0);

while (n) {

Push (s,n%8);

N=n/8;}

printf ("The result is:");

while (! Stackempty (*s)) {

Pop (s,&e); printf ("%d", e);}

}

See: C-source program for the conversion of a.

Second, stack application of the second: row editor

The function of a simple line editor is to accept the program or data entered by the user from the terminal and deposit it into the user's data area. Allow users to enter errors in time to correct. You can contract # to BACKSPACE to indicate that the previous character is invalid, @ is a regression, and that all characters in the current line are invalid.

Example: On the terminal the user enters as

whli# #ilr #e (s#*s) should be

while (*s)

void Lineedit () {

Psqstack s,t; char str[1000];

int strlen=0; char e; char ch;

Initstack (&s);

Initstack (&t);

Ch=getchar ();

while (ch!=eofile) {

while (ch!=eofile&&ch!= ' \ n ') {

S Witch (CH) {

Case ' # ': POPs (s,&ch); break;

Case ' @ ': Clearstack (S);;

Default:push (S,CH);

Ch=getchar ();

}

if (ch== ' \ n ') Push (s,ch);

while (! Stackempty (*s)) {Pop (s,&e); Push (t,e);

while (! Stackempty (*t)) {Pop (t,&e); str[strlen++]=e;}

if (ch!=eofile) Ch=getchar ();

}

str[strlen]= ';

printf ("\n%s", str);

Destroystack (S);

Destroystack (T);

}

Please see: line edit C source program

Three, stack application of three: expression evaluation

A programming language should allow the designer to describe the calculation process as needed, and the compiler should be able to parse the expression and calculate the result. The elements of an expression are operators, operands, delimiting characters, and operator precedence relationships. Example: 1+2*3 has +,* two operators, * has a high priority, 1,2,3 is the operand. The qualifier has parentheses and expression Terminator, and so on.

Algorithm basic idea:

1 first set the operand stack to empty stack, the expression starting character # is the stack bottom element of operator stack;

2 in turn, each character in the speech expression, if the operand into the OPND stack, if the operator, and the stack of OPTR stack operator precedence after the corresponding operation, until the entire expression evaluation completed.

Char evaluateexpression () {

Sqstack *opnd,*optr;

Char C,x,theta; char a,b;

Initstack (&OPTR); Push (Optr, ' # ');

Initstack (&OPND);

C=getchar ();

while (c!= ' # ' | | GetTop (*optr)!= ' # ') {

if (! In (C,op)) {Push (opnd,c); C=getchar ();}

Else

Switch (Precede (GetTop (*optr), C)) {

Case ' < ': Push (optr,c); C=getchar ( ); Break

Case ' = ': POPs (optr,&x); C=getchar (); break;

Case ' > ': Pop (Optr,&theta);

Pop (opnd,&b); Pop (Opnd,&a);

Push (Opnd,operate (a,theta,b));

Break;

}

}

C=gettop (* OPND);

Destroystack (optr);

Destroystack (OPND);

return C;

}

Please see: expression evaluation of C-source program

Iv. Summary

The advanced, backward, first out features of the stack.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.