zoukankan      html  css  js  c++  java
  • 栈的链表实现

      1 /*
      2   栈是限制插入和删除只能在一个位置上进行的表,该位置是表的末端(最后进入栈的元素位置),叫做栈的顶。
      3   栈具有后进先出的特点,用链表实现时可采用首插法实现这一特性。
      4   对于栈,进栈操作要检查栈是否有空间;出栈和取栈顶值要检查栈是否为空栈。
      5 */
      6 
      7 /*栈的链表实现*/
      8 
      9 /*接口头文件*/
     10 typedef int ElementType;
     11 
     12 #ifndef _STACK_H
     13 #define _STACK_H
     14 #include <stdbool.h>
     15 
     16 struct Node;
     17 typedef struct Node * PtrToNode;
     18 typedef PtrToNode Stack;
     19 typedef PtrToNode Position;
     20 
     21 /*操作集*/
     22 bool IsEmpty( Stack S );
     23 Stack CreateStack( void );
     24 void MakeEmpty( Stack S );
     25 void Pop( Stack S );
     26 void Push( ElementType X, Stack S );
     27 ElementType Top( Stack S );
     28 void DisposeStack( Stack S );
     29 void PrintfStack( Stack S );
     30 
     31 #endif 
     32 
     33 
     34 /*接口实现*/
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include "stack.h"
     38 
     39 /*特定结构体定义*/
     40 struct Node
     41 {
     42     ElementType Element;
     43     Position Next;
     44 };
     45 
     46 bool IsEmpty( Stack S )
     47 {
     48     return S->Next ==NULL;
     49 }
     50 
     51 Stack CreateStack( void )
     52 {
     53     Stack S;
     54     
     55     S = ( Stack )malloc( sizeof( struct Node ) );
     56     if ( S == NULL )
     57     {
     58        printf( "No Space!!!
    " );
     59        exit( 1 );
     60     }
     61     S->Next = NULL;
     62     /*确保S是空栈*/ 
     63     MakeEmpty( S );
     64     
     65     return S;
     66 }
     67 
     68 void MakeEmpty( Stack S )
     69 {
     70     Position P;
     71     
     72     if ( S == NULL )
     73     {
     74        printf( "No use  CreateStack!!!
    " );
     75        exit( 1 );
     76     }
     77     else
     78         while ( ! IsEmpty( S ) )
     79             Pop( S );
     80 }
     81 
     82 void Pop( Stack S )
     83 {
     84     Position P;
     85     
     86     if ( IsEmpty( S ) )
     87     {
     88        printf( "Stack is empty!!!
    " );
     89        exit( 1 );
     90     }
     91     else
     92     {
     93         P = S->Next;
     94         S->Next = S->Next->Next;
     95         free( P );
     96     }
     97 }
     98 
     99 void Push( ElementType X, Stack S )
    100 {
    101     Position P;
    102     
    103     P = ( Stack )malloc( sizeof( struct Node ) );
    104     if ( P == NULL )
    105     {
    106        printf( "No Space!!!
    " );
    107        exit( 1 ); 
    108     }
    109     else
    110     {
    111         P->Element = X;
    112         P->Next = S->Next;
    113         S->Next = P;
    114     }
    115 }
    116 
    117 ElementType Top( Stack S )
    118 {
    119     if ( IsEmpty( S ) )
    120     {
    121        printf( "Stack is empty!!!
    " );
    122        exit( 1 );
    123     }
    124     else
    125         return S->Next->Element;
    126 }
    127 
    128 void DisposeStack( Stack S )
    129 {
    130     /*
    131     Position P;
    132     
    133     while ( S->Next != NULL )
    134     {
    135         P = S->Next;
    136         S->Next = S->Next->Next;
    137         free( P );
    138     }
    139    */
    140     MakeEmpty( S );
    141     /*释放栈头*/ 
    142     free( S );
    143 }
    144 
    145 /*打印栈,且把栈变为空栈*/
    146 void PrintfStack( Stack S )
    147 {
    148     while ( ! IsEmpty( S ) )
    149     {
    150         printf( "%3d", Top( S ) );
    151         Pop( S );
    152     }
    153     printf( "
    " );
    154 }
    155 
    156 
    157 //平衡符号,检查输入的算式是否正确只检查{},(),[]三种,例如(a+b){[d]c*d}{}是正确的,但是(a+b){{[d]c*d}{}不对
    158 void balance(char * ch, Stack S)
    159 {
    160     ElementType c;
    161     MakeEmpty(S);
    162     while ((c = *ch) != '')//一个一个读入字符,直到遇到字符数组结束符
    163     {
    164         printf("%c ", c);//打印出读入的字符
    165         switch (c)
    166         {
    167         //如果是开放符号{,(,[,就推入栈中,除此之外的任何符号都不推入栈中
    168         case '(':
    169         case '[':
    170         case '{': Push(c, S); break;       //然后停止switch
    171 
    172        //如果是封闭符号},),],分情况
    173         case ')':
    174             if (IsEmpty(S))//如果栈为空,说明字符数组不平衡
    175             {
    176                 fprintf(stderr, "The symbols not balance!
    ");
    177                 return;
    178             }
    179             else
    180             {
    181                 if (Top(S) == '(')//当读入")"时,而此时栈顶符号为"(",匹配
    182                 {
    183                     Pop(S);//就把栈顶元素出栈
    184                 }
    185                 else//否则就不平衡
    186                 {
    187                     fprintf(stderr, "The symbols not balance!
    ");
    188                     return;
    189                 }
    190             }
    191             break;
    192         //其他情况一样
    193         case ']':
    194             if (IsEmpty(S))
    195             {
    196                 fprintf(stderr, "The symbols not balance!
    ");
    197                 return;
    198             }
    199             else
    200             {
    201                 if (Top(S) == '[')
    202                 {
    203                     Pop(S);
    204                 }
    205                 else
    206                 {
    207                     fprintf(stderr, "The symbols not balance!
    ");
    208                     return;
    209                 }
    210             }
    211             break;
    212         case '}':
    213             if (IsEmpty(S))
    214             {
    215                 fprintf(stderr, "The symbols not balance!
    ");
    216                 return;
    217             }
    218             else
    219             {
    220                 if (Top(S) == '{')
    221                 {
    222                     Pop(S);
    223                 }
    224                 else
    225                 {
    226                     fprintf(stderr, "The symbols not balance!
    ");
    227                     return;
    228                 }
    229             }
    230             break;
    231         default:
    232             break;
    233         }
    234         ch++;
    235     }
    236     if (IsEmpty(S))
    237     {
    238         fprintf(stdout, "The Symbols Balance!
    ");
    239     }
    240     else
    241     {
    242         fprintf(stderr, "The Symbols Not Balance!
    ");
    243     }
    244 }
  • 相关阅读:
    PAIRING WORKFLOW MANAGER 1.0 WITH SHAREPOINT 2013
    Education resources from Microsoft
    upgrade to sql server 2012
    ULSViewer sharepoint 2013 log viewer
    Top 10 Most Valuable Microsoft SharePoint 2010 Books
    讨论 Setsockopt选项
    使用 Alchemy 技术编译 C 语言程序为 Flex 可调用的 SWC
    Nagle's algorithm
    Nagle算法 TCP_NODELAY和TCP_CORK
    Design issues Sending small data segments over TCP with Winsock
  • 原文地址:https://www.cnblogs.com/weixia-blog/p/7307406.html
Copyright © 2011-2022 走看看