C Stack Stack

Source: Internet
Author: User

Stack is a special kind of linear table

Stacks can only operate at one end of a linear table

Top: One end of the allowed operation

Bottom of Stack (Bottom): One end of operation is not allowed

StackCommon operations of

Creating stacks

Destroying stacks

Empty stack

Into the stack

Out of the stack

Get top of stack element

Get the size of the stack

The design and realization of C language description ====="accumulation of life wealth storehouse

#ifndef _my_stack_h_

#define _my_stack_h_

typedef void Stack;

Stack* stack_create ();

void Stack_destroy (stack* Stack);

void Stack_clear (stack* Stack);

int Stack_push (stack* Stack, void* item);

void* Stack_pop (stack* Stack);

void* stack_top (stack* Stack);

int stack_size (stack* Stack);

#endif//_my_stack_h_

3.1.3analysis of relationship between stack model and linked list model

The basic concept of sequential storage design and implementation of stacks

Design and implementation

Header file

#ifndef   __my_seqlist_h__ 

#define  __my_seqlist_ h__

 

Typedef void seqlist;

Typedef void seqlistnode;

 

Seqlist* seqstack_create (int capacity);

 

Void seqstack _destroy (seqstack * list);

 

Void seqstack _clear (seqstack * list);

 

Int seqstack _length (seqstack * list);

 

Int seqstack _capacity (seqstack * list);

 

Int seqstack _insert (seqstack * list, seqlistnode* node, int pos);

 

Seqlistnode* seqlist_get (seqlist* list, int pos);

 

Seqlistnode* seqlist_delete (seqlist* list, int pos);

 

#endif   //__my_seqlist_h__

 

3.1.5design and implementation of chain storage for stacks

Design and implementation

Header file

#ifndef _my_linkstack_h_

#define _my_linkstack_h_

typedef void Linkstack;

Linkstack* linkstack_create ();

void Linkstack_destroy (linkstack* stack);

void Linkstack_clear (linkstack* stack);

int Linkstack_push (linkstack* stack, void* item);

void* Linkstack_pop (linkstack* stack);

void* linkstack_top (linkstack* stack);

int linkstack_size (linkstack* stack);

#endif//_my_linkstack_h_

3.1.6Application of StacksCase1: Nearest Match

Application 1: Nearest Match

Almost all compilers have the ability to detect if parentheses match

How do I implement symbolic pair detection in the compiler?

#include <stdio.h> int main () {int a[4][4]; int (*p) [4]; p = a[0]; return 0;

Algorithm Ideas

Start scanning from the first character

Ignored when ordinary characters are met,

Pressed into the stack when the left sign is met

POPs the top symbol from the stack when the right symbol is met and matches

Match succeeded: Continue reading into next character

Match failed: Stop immediately, and error

End:

Successful : All characters are scanned and the stack is empty

Failed: Match failed or all characters scanned but stack not empty

When it is necessary to detect things that are not mutually adjacent

You can use the "LIFO" feature of the stack

Stacks are ideal for situations where you need a "near match"

The essential work of a computer is to do mathematical arithmetic, and that computer can read into a string

"9 + (3-1) * 5 + 8/2" and calculate the value?

Case2: infix expressions and suffix expressions

Application 2: infix suffix

The essential work of a computer is to do mathematical arithmetic, and that computer can read into a string

"9 + (3-1) * 5 + 8/2" and calculate the value?

Suffix expression = =? Compliant with computer operations

Polish scientists have proposed a suffix expression that puts the operator behind the number in the decade.

We are accustomed to the mathematical expression called infix expression = = ="In line with human thinking habits

Instance:

5 + 4=> 5 4 +

1 + 2 * 3 = 1 2 3 * +

8 + (3–1) * 5 = 8 3 1–5 * +

Infix expression conforms to human reading and thinking habits

The suffix expression conforms to the computer's "operating Habits"

How do I convert an infix expression to a suffix expression?

infix suffix algorithm:

Iterating through the numbers and symbols in an infix expression

For numbers: Direct output

For symbols:

Left parenthesis: Into the stack

Operational symbols: Precedence compared to top of stack symbols

If the stack top symbol priority is low: this conforms to the stack (the default stack top if the opening parenthesis, the left parenthesis the lowest priority)

If the stack top symbol priority is not low: the stack top symbol is ejected and output, and then into the stack

Closing parenthesis: POPs the top of the stack and outputs it until it matches the opening parenthesis

Traversal end: Eject and output all symbols in the stack

infix suffix

How is the computer computed based on the suffix expression?

8 3 1 – 5 * + 

Traverse numbers and symbols in postfix expressions

For numbers: Stack

For symbols:

Eject right operand from stack

Eject left operand from stack

Perform operations on symbols

Press the result of the operation into the stack

Traversal end: The only number in the stack is the result of the calculation

Stacks of magic!     

Infix expression is the expression of the human habit

Suffix expression is the way the computer likes to express

Convert infix form to suffix by using the stack

The calculation process of infix expression is similar to the procedure of program compiling and running   

 

Extension: give you a string, calculation result

"1 + 2 *  (66 /  (2 * 3)  + 7 )"     

1

String parsing

Lexical parsing

Precedence analysis      

Data structure selection ===

C Stack Stack

Related Article

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.