_ DataStructure_C_Impl: chain Stack

Source: Internet
Author: User

_ DataStructure_C_Impl: chain Stack

// _ DataStructure_C_Impl: chain stack # include
 
  
# Include
  
   
Typedef char DataType; typedef struct node {DataType data; struct node * next;} LStackNode, * LinkStack; // The chain stack Initialization is empty. Dynamically generate the header node and set the pointer field of the header node to void InitStack (LinkStack * top) {if (* top = (LinkStack) malloc (sizeof (LStackNode ))) = NULL) // allocate a bucket for the header node exit (-1); (* top)-> next = NULL; // set the head node pointer field of the chain stack to null} // determine whether the chain stack is empty, that is, whether the pointer field of the head node is null int StackEmpty (LinkStack top) {if (top-> next = NULL) // if the link stack is empty, 1 is returned; otherwise, 0 return 1; else return 0;} is returned ;} // The stack import operation is to insert a new node in front of the first node of the linked list, and return 1int PushStack (LinkStack top, DataType e) {LStackNode * p; // pointer p points to the new node if (p = (LStackNode *) Malloc (sizeof (LStackNode) = NULL) {printf (memory allocation failed); exit (-1);} p-> data = e; p-> next = top-> next; // the pointer p points to the header node top-> next = p; return 1 ;} // Delete the node at the I position in a single-chain table. If the deletion succeeds, 1 is returned. if the deletion fails, 0 int PopStack (LinkStack top, DataType * e) {LStackNode * p; p = top-> next; if (! P) {// determine whether the chain stack is empty printf (the stack is empty); return 0;} top-> next = p-> next; // disconnect the top node of the stack from the linked list, that is, the output stack * e = p-> data; // assign the output stack element to efree (p ); // release p to the node return 1;} // The operation int GetTop (LinkStack top, DataType * e) {LStackNode * p; p = top-> next; if (! P) {// determine whether the chain stack is empty printf (the stack is empty); return 0;} * e = p-> data; // assign the top element of the stack to ereturn 1;} // calculate the int StackLength (LinkStack top) {LStackNode * p; int count = 0; p = top; while (p-> next! = NULL) {p = p-> next; count ++;} return count;} // The void DestroyStack (LinkStack top) {LStackNode * p, * q; p = top; while (! P) {q = p; p = p-> next; free (q) ;}} void main_LinkStack () {LinkStack S; DataType ch [50], e, * p; initStack (& S); printf (Please input the character into the stack :); gets (ch); p = & ch [0]; while (* p) {PushStack (S, * p); p ++;} printf (the element at the top of the current stack is :); if (GetTop (S, & e) = 0) {printf (the stack is empty !); Return;} else {printf (% 4c, e);} printf (the number of elements in the current stack is: % d, StackLength (S )); printf (the sequence of element output stack is :); while (! StackEmpty (S) {PopStack (S, & e); printf (% 4c, e);} printf (); system (pause );} // ********************************* int Match (DataType e, dataType ch) {if (e = '(' & ch = ') return 1; else if (e = '[' & ch = ']') return 1; else if (e = '{' & ch = '}') return 1; elsereturn 0;} void main_Match () {LinkStack S; char * p; DataType e; DataType ch [100]; InitStack (& S ); printf (enter an expression with parentheses ('{}', '[]', '()').); gets (ch); p = ch; while (* p) {switch (* p) {case '(': case '[': case '{': PushStack (S, * p ++); break; case ')': case ']': case '}': if (StackEmpty (S) {printf (left parenthesis missing .); return;} else {GetTop (S, & e); if (Match (e, * p) PopStack (S, & e ); else {printf (left and right parentheses are not paired .); return ;}} default: p ++; }}if (StackEmpty (S) printf (matching brackets .); elseprintf (the right parenthesis is missing .); system (pause) ;}//============ void main () {main_LinkStack (); main_Match ();}
  
 

 

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.