"Data structure and algorithm analysis--c language description" after reading note 3

Source: Internet
Author: User

Stack:


Application of stacks, balance symbols:

Read into a string expression that includes 6 symbols (,), [,], {,}. Write a program that verifies that the 6 symbols in a string expression match correctly.


Program:

Stack.h

typedef char ElementType; #ifndef _stack_hstruct node;typedef struct Node *stack;int IsEmpty (stack s); int isfull (stack s); Stack createstack (int maxelements); void Disposestack (stack s); void Makeempty (stack s); void Push (ElementType X, Stack s) ; ElementType Top (stack s); void Pop (stack s);    ElementType Topandpop (Stack S), #endif #define EMPTYTOS ( -1) #define MINSTACKSIZE (5) struct node{int capacity;    int topofstack; ElementType *array;};

Stack.c

#include   "stack.h" #include <stdio.h> #include <error.h> #include <stdlib.h>int isempty ( Stack s) {    return s->topofstack==emptytos;} Int isfull (stack s) {    return s->topofstack== (S->Capacity-1);} Stack createstack ( int MaxElements ) {    stack s;     if (maxelements<minstacksize) error ("Stack size is too small");     s=malloc (sizeof (Struct node)),     if (s==null) error ("Out of  space!!! ");     s->array=malloc (sizeof (ElementType) *maxelements);     if (S-> Array==null) error ("Out of space!!!");     s->capacity=maxelements;    makeempty (S);     return s;} Void disposestack (stack s) {    if (s!=null)  &nbsP;  { free (S->array); free (S);     }}void makeempty (Stack S) {     s->topofstack=emptytos;} Void push (elementtype x,stack s) {    if (Isfull (S)) error ("Full  Stack ");    else    {         s->topofstack++;        s->array[s->topofstack]=x;     }}elementtype top ( stack s) {    if (! IsEmpty (S)) return s->array[s->topofstack];    error ("Empty stack");     return 0;} Void pop ( stack s) {    if (IsEmpty (S)) error ("Empty stack");     else        s->topofstack=emptytos;} Elementtype topandpop ( stack s) {    if (! IsEmpty(S))     {return S->Array[ S->TopOfStack--];    }     error ("Empty stack");     return 0;}

//signal_match.c

#include <stdio.h> #include <stdlib.h> #include <string.h> #include "stack.h" #define   Maxelements 100#define size 100int convert (char ch) {    if (ch== ' ( ') return 1;    else if (ch== ') return 2;    else  if (ch== ' [') return 3;    else if (ch== '] ') return 4;     Else if (ch== ' {') return 5;    else if (ch== '} ') return 6;     elsereturn 0;} Void solution (char* str,stack s) {    int i;    for (I=0;i<strlen (str); ++i)     {switch (CONVERT (Str[i]))     {     case 0:    {    break;    }     case 1:    case 3:    case 5:&nBsp;   {if (Isfull (s)) {    printf ("full stack!\n");     return ;}  push (str[i],s);break;    }    case 2:     {if (IsEmpty (s)) {    printf ("The string is wrong,there is no   ' ('  to  ') '!\n ');     return ;} if (Topandpop (s)! = ' (') {    printf ("the string is wrong,it should  not be a  ') '!\n ');     return ; }else    break ;     }    case 4:    {if (IsEmpty (s)) {     printf ("the string is wrong,there is no " ['  to  '] '!\ n ");     return ;} if (Topandpop (s)! = ' [') {    printf ("The string is wrong,it should not be a  '] '!\n ");     return ;} else    break;    }    case 6:     {if (IsEmpty (s))   {    printf ("The string is wrong, there is no  ' {'  to  '} '!\n ');     return ;} if (Topandpop (s)! = ' {') {    printf ("the string is wrong,it should  not be a  '} '!\n ');     return ;} else    break;    }}    }     if (! IsEmpty (s))     {printf ("there is too much " ('  ,  ' ['  or   ' {'  !\n '); return ;    }       printf (" It is a good string!\n ");  }int main () {    stack mystack =Createstack (maxelements);     char *str=malloc (sizeof (char) *size);     printf ("please input the string : "); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", str);     solution (str,mystack);      return 0;}


Stack.h and STACK.C are the declarations and definitions of stacks, and SIGNAL_MATCH.C is the application of stacks.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/71/6E/wKiom1XPR4HCU8ZYAAHn7rOrvuA430.jpg "title=" capture. JPG "style=" float:left; "alt=" wkiom1xpr4hcu8zyaahn7rorvua430.jpg "/>

"Data structure and algorithm analysis--c language description" after reading note 3

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.