zoukankan      html  css  js  c++  java
  • [数据结构与算法] : 栈的链式实现

    头文件

     1 typedef int ElementType;
     2 
     3 #ifndef _STACKLI_H_
     4 #define _STACKLI_H_
     5 
     6 struct Node;
     7 typedef Node *PtrToNode;
     8 typedef PtrToNode Stack;
     9 
    10 int IsEmpty(Stack S);
    11 Stack CreateStack(void);
    12 void DisposeStack(Stack S);
    13 void MakeEmpty(Stack S);
    14 void Push(ElementType X, Stack S);
    15 ElementType Top(Stack S);
    16 void Pop(Stack S);
    17 
    18 #endif

    源文件

     1 #include "stackli.h"
     2 #include <malloc.h>
     3 #include <stdlib.h>
     4 
     5 struct Node
     6 {
     7     ElementType Element;
     8     PtrToNode Next;
     9 };
    10 
    11 int IsEmpty(Stack S)
    12 {
    13     return S->Next == NULL;
    14 }
    15 
    16 // 创建栈的头节点
    17 Stack CreateStack(void)
    18 {
    19     Stack S = (Stack)malloc( sizeof(struct Node) );
    20     if( S == NULL )
    21     {
    22         fprintf(stderr, "%s
    ", "Out of space!!!");
    23         exit(-1);
    24     }
    25     S->Next = NULL;
    26     return S;
    27 }
    28 
    29 // 销毁栈(包括头节点)
    30 void DisposeStack(Stack S)
    31 {
    32     MakeEmpty(S);
    33     free(S);
    34 }
    35 
    36 // 清空栈就是弹出所有数据
    37 void MakeEmpty(Stack S)
    38 {
    39     if(S == NULL)
    40     {
    41         fprintf(stderr, "%s
    ", "Must use CreateStack first");
    42         exit(-1);
    43     }
    44     else
    45         while( !IsEmpty(S) )
    46             Pop(S);
    47 }
    48 
    49 // 进栈, 插入到头节点之后
    50 void Push(ElementType X, Stack S)
    51 {
    52     PtrToNode TmpCell = (PtrToNode)malloc( sizeof(struct Node) );
    53     if(TmpCell == NULL)
    54     {
    55         fprintf(stderr, "%s
    ", "Out of space!!!");
    56         exit(-1);
    57     }
    58     else
    59     {
    60         TmpCell->Element = X;
    61         TmpCell->Next = S->Next;
    62         S->Next = TmpCell;
    63     }
    64 }
    65 
    66 // 返回栈顶元素, 注意S不能为空
    67 ElementType Top(Stack S)
    68 {
    69     if( IsEmpty(S) )
    70     {
    71         fprintf(stderr, "%s
    ", "Empty stack");
    72         return -1;
    73     }
    74     else
    75         return S->Next->Element;
    76 }
    77 
    78 // 出栈, 删除头节点的后继
    79 void Pop(Stack S)
    80 {
    81     if( IsEmpty(S) )
    82     {
    83         fprintf(stderr, "%s
    ", "Empty stack");
    84     }
    85     else
    86     {
    87         PtrToNode P = S->Next;
    88         S->Next = P->Next;
    89         free(P);
    90     }
    91 }

    测试文件

     1 #include <stdio.h>
     2 #include "stackli.h"
     3 
     4 main( )
     5 {
     6     Stack S;
     7     int i;
     8 
     9     S = CreateStack( );
    10     for( i = 0; i < 10; i++ )
    11         Push( i, S );
    12 
    13     while( !IsEmpty( S ) )
    14     {
    15         printf( "%d ", Top( S ) );
    16         Pop( S );
    17     }
    18 
    19     DisposeStack( S );
    20     return 0;
    21 }
  • 相关阅读:
    C#音频截取与原文匹配2:使用ffmpeg处理音频文件
    C#音频截取与原文匹配
    Redis报错: StackExchange.Redis.RedisServerException: Endpoint 39.105.22.111:7200 serving hashslot 12448 is not reachable at this point of time.
    kafka单机安装部署
    zookeeper部署
    mysql-5.7.15编译安装
    centos7安装sqlserver
    redisearch模块安装
    yum安装软件后保留rpm包
    shell读取配置文件
  • 原文地址:https://www.cnblogs.com/moon1992/p/7500057.html
Copyright © 2011-2022 走看看