Data Structure Learning Experience Series (ii)

Source: Internet
Author: User

This blog is intended to consolidate the basics, master please bypass. Section code and content reference Min People's post and telecommunications publishing house "data Structure"

Stacks and queues

Stack: a linear table that restricts insertion or deletion only at one end

Top: One end of the INSERT, delete operation is allowed, also known as the footer.

Stack Bottom (Bottom): Is a fixed end, also known as a table header.

Empty stack: When there are no elements in the table, it is called an empty stack.

In the stack is first put in the stack of elements at the bottom of the stack, the last element placed on the top of the stack, and the deletion of the element is just the opposite, the last element is deleted first, the first element is deleted.

Features: LIFO LIFO (last on First out)

Application of Stacks

Test for matching parentheses

It is assumed that two types of parentheses are allowed in the expression: parentheses and brackets, which are nested in a random order, i.e. ([] ()) or [([]]) and so on in the correct format, [(]) or ([()) or ()]) are in an incorrect format.

Algorithm idea: Set a stack, when reading the opening parenthesis, the left parenthesis into the stack. When the closing parenthesis is read, an element is popped from the stack, matched to the left parenthesis read, and continued to be read if the match succeeds, otherwise the match fails and returns flase.

Algorithm description

#define TRUE  0#define  flase  -1sqstack  S; S=init_stack ();  /* Stack initialization */int match_brackets () {   char ch, x; scanf ("%c", &ch), while (ASC (CH)!=13) {   if  ((ch== ' (') | | (ch== ' [')]  Push (S, CH); else if  (ch== '] ')      {  x=pop (S);          if (x!= ' [')                 {  printf ("' [' parenthesis Mismatch");                      return flase  ;  }   } else if  (ch== ') ')       {  x=pop (S);          if (x!= ' (')               {printf ("' (' parenthesis mismatch ');                  return flase  ;}      } }if  (s.top!=0) {    printf ("The number of parentheses does not match!") ");      return flase  ;} else  return TRUE  ;}

Queue

Queue: a linear table that restricts the insertion at one end and the deletion at the other end.

Team header: The one end of the allowed delete is called the team header (front)

End of line: one end of the allowed insert is called the tail of the queue (rear)

Empty queue: An empty queue when there are no elements in the queue

Features: FIFO First Out

Chain representation and implementation of queues

A chained storage structure for a queue is simply a chain queue, which is a single linked list that restricts the insertion of a table header to a delete operation and footer. Two different types of nodes are required: Data element node, queue's first pointer, and tail pointer node.

Chain operations and pointer changes

The operation of the chain is actually a single-linked list operation, except that it is deleted at the table header and inserted at the end of the table. The different pointers are modified separately when inserting or deleting.

Data Structure Learning Experience Series (ii)

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.