zoukankan      html  css  js  c++  java
  • 【数据结构与算法】第三章 栈c实现,使用链表结构

    栈的本质是一个表,但它限制插入和删除只能在一个位置上进行。这个特殊的位置是表的末端,叫做栈顶(top)。栈的基本操作有Push和Pop两种。这里有两种比较流行的栈的实现方式:一种是用链表实现,另一种是用数组实现。这里,我们先给出链表实现的c源码。

    stackList.h头文件定义如下。具体内容可参见《数据结构与算法分析:c语言描述》第三章。

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

    stackList.c

    1 /************************************
    2 * stack implemention by list
    3 * **********************************/
    4
    5 #include <stdio.h>
    6 #include <stdlib.h>
    7 #include "stackList.h"
    8
    9
    10 //判断S是否为空
    11 //若为空,返回1,若不为空,返回0
    12 int IsEmpty( Stack S )
    13 {
    14 return S->Next == NULL;
    15 }
    16
    17 //创造一个新栈
    18 //返回栈指针
    19 Stack CreateStack( void )
    20 {
    21 Stack S;
    22 S = ( Stack )malloc( sizeof( Node ) );
    23 if( S == NULL )
    24 perror( "Out of space!!!\n");
    25 S->Next = NULL;
    26 MakeEmpty( S );
    27 return S;
    28 }
    29
    30 //清空栈
    31 void MakeEmpty( Stack S )
    32 {
    33 if( S == NULL )
    34 perror( "Must use CreateStack first.\n" );
    35 else
    36 while( !IsEmpty( S ) )
    37 Pop( S );
    38
    39 printf("Make empty \n");
    40 }
    41
    42 //销毁栈
    43 void DisposeStack( Stack S )
    44 {
    45 MakeEmpty( S );
    46 free( S );
    47 printf("Dispose stack!\n");
    48 }
    49
    50 //压栈操作
    51 void Push( Stack S, ElementType X )
    52 {
    53 PtrToNode newCell;
    54 newCell = ( PtrToNode )malloc( sizeof( Node ) );
    55 if( newCell == NULL )
    56 perror( "Out of space!\n" );
    57 else
    58 {
    59 newCell->Element = X;
    60 newCell->Next = S->Next;
    61 S->Next = newCell;
    62 }
    63 }
    64
    65 //取栈顶元素
    66 ElementType Top( Stack S )
    67 {
    68 if( !IsEmpty( S ) )
    69 return S->Next->Element;
    70
    71 perror( "Stack is empty!\n" );
    72 return 0;
    73 }
    74
    75 //弹栈操作
    76 void Pop( Stack S )
    77 {
    78 PtrToNode topCell;
    79
    80 if( IsEmpty( S ) )
    81 perror( "Stack is empty!\n" );
    82 else
    83 {
    84 topCell = S->Next;
    85 S->Next = topCell->Next;
    86 free( topCell );
    87 }
    88 }
    89
    90 //打印栈的元素
    91 void PrintStack( Stack S )
    92 {
    93 PtrToNode P;
    94 for( P = S->Next; P != NULL; P = P->Next )
    95 printf("%5d", P->Element);
    96 printf("\n");
    97 }
    98
    99 /****************************************/
    100 int main()
    101 {
    102 ElementType data;
    103 Stack st;
    104 PtrToNode p;
    105
    106 if( !(st = CreateStack()) )
    107 perror("create stack failed.\n");
    108 else
    109 printf("create stack success\n");
    110
    111 for( data = 10; data < 20; data++ )
    112 Push( st, data );
    113
    114 printf("Push Operation: ");
    115 PrintStack( st );
    116
    117 printf("Top Element = %5d\n", Top( st ));
    118
    119 Pop( st );
    120 printf("Pop Operation: ");
    121 PrintStack( st );
    122
    123 MakeEmpty( st );
    124 DisposeStack( st );
    125 return 0;
    126 }

    用数组实现栈结构,这种方法更加流行一些。因为,栈在应用过程中,任一时刻栈元素的实际个数并不会太大。因此,声明一个数组足够大而不至于浪费太多的空间,做到这一点并不困难。下面贴出数组实现的栈代码:

    stackArray.h

    1 #ifndef _STACKARRAY_H
    2 #define _STACKARRAY_H
    3
    4 typedef int ElementType;
    5 typedef struct _StackRecord
    6 {
    7 int TopOfStack;
    8 int Capacity;
    9 ElementType *Array;
    10 }StackRecord;
    11 typedef StackRecord *Stack;
    12
    13 int IsEmpty( Stack S );
    14 int IsFull( Stack S );
    15 Stack CreateStack( int MaxElements );
    16 void DisposeStack( Stack S );
    17 void MakeEmpty( Stack S );
    18 void Push( Stack S, ElementType X );
    19 ElementType Top( Stack S );
    20 void Pop( Stack S );
    21 ElementType TopAndPop( Stack S );
    22 void PrintStack( Stack S );
    23
    24 #endif /* _STACKARRAY_H */

    stackArray.c

    1 /***************************************
    2 * Stack Implentation by Array
    3 * An popular method
    4 * Author: qiqi
    5 * Date: 2011-6-2
    6 * *************************************/
    7
    8 #include <stdio.h>
    9 #include <stdlib.h>
    10 #include "stackArray.h"
    11
    12 #define EmptyTOS ( -1 ) /* 当栈为空时,栈顶指针置为1 */
    13 #define MinStackSize ( 5 ) /* 栈最小为5 */
    14
    15 //判断是否为空
    16 int IsEmpty( Stack S )
    17 {
    18 return S->TopOfStack == EmptyTOS;
    19 }
    20
    21 //判断栈是否已满
    22 int IsFull( Stack S )
    23 {
    24 return S->TopOfStack == S->Capacity - 1;
    25 }
    26
    27 //创建一个栈
    28 Stack CreateStack( int MaxElements )
    29 {
    30 Stack S;
    31
    32 if( MaxElements < MinStackSize )
    33 {
    34 perror( "Stack size is too small!\n" );
    35 exit(1);
    36 }
    37
    38 S = ( Stack )malloc( sizeof( StackRecord ) );
    39 if( S == NULL )
    40 {
    41 perror( "Out of space!!!\n" );
    42 exit(1);
    43 }
    44
    45 S->Array = ( ElementType * )malloc( sizeof( ElementType )
    46 * MaxElements );
    47 if( S->Array == NULL )
    48 {
    49 perror( "Out of space!!!\n" );
    50 exit(1);
    51 }
    52
    53 S->TopOfStack = -1;
    54 S->Capacity = MaxElements;
    55
    56 return S;
    57 }
    58
    59 //销毁一个栈
    60 void DisposeStack( Stack S )
    61 {
    62 if( S != NULL )
    63 {
    64 free( S->Array );
    65 free( S );
    66 }
    67 }
    68
    69 //清空一个栈
    70 void MakeEmpty( Stack S )
    71 {
    72 S->TopOfStack = EmptyTOS;
    73 }
    74
    75 //压栈操作
    76 void Push( Stack S, ElementType X )
    77 {
    78 if( IsFull( S ) )
    79 {
    80 perror( "Stack is full!!!\n" );
    81 exit( 1 );
    82 }
    83 else
    84 S->Array[ ++S->TopOfStack ] = X;
    85 }
    86
    87 //取栈顶元素
    88 ElementType Top( Stack S )
    89 {
    90 if( !IsEmpty( S ) )
    91 return S->Array[ S->TopOfStack ];
    92 perror( "Stack is empty!!!\n" );
    93 return 0; /* return value used to avoid warning */
    94 }
    95
    96 //弹栈操作
    97 void Pop( Stack S )
    98 {
    99 if( !IsEmpty( S ) )
    100 {
    101 S->TopOfStack--;
    102 return;
    103 }
    104 else
    105 {
    106 perror( "Stack is empty!!!\n" );
    107 exit( 1 );
    108 }
    109 }
    110
    111 //取栈顶元素,并弹栈操作
    112 ElementType TopAndPop( Stack S )
    113 {
    114 if( !IsEmpty( S ) )
    115 return S->Array[ S->TopOfStack-- ];
    116 perror( "Stack is empty!!!\n" );
    117 return 0; /* return value used to avoid warning */
    118 }
    119
    120 //打印栈元素
    121 void PrintStack( Stack S )
    122 {
    123 int i;
    124 for( i = 0; i <= S->TopOfStack; i++ )
    125 printf( "%5d", S->Array[ i ] );
    126 printf( "\n" );
    127 }
    128
    129
    130 /****************************************/
    131 int main()
    132 {
    133 Stack st;
    134 ElementType data;
    135 int i;
    136
    137 //创建栈
    138 st = CreateStack( 10 );
    139 //向栈中压入元素
    140 for( data = 21; data < 29; data++ )
    141 Push( st, data );
    142 //打印栈中的元素
    143 printf( "Stack : " );
    144 PrintStack( st );
    145 //取栈顶元素
    146 printf( "Top of stack = " );
    147 printf( "%5d\n", Top( st ) );
    148 //弹出栈顶元素
    149 Pop( st );
    150 printf( "Pop stack: " );
    151 PrintStack( st );
    152 //取占栈顶元素,并打印之
    153 printf( "Top and pop stack: " );
    154 printf( "%5d\n", TopAndPop( st ) );
    155 printf( "Stack: " );
    156 PrintStack( st );
    157 MakeEmpty( st );
    158 DisposeStack( st );
    159 }

  • 相关阅读:
    WEB前端开发工具的初识
    Linux常见问题的处理方法(长期更新)
    eclipse+tomcat开发web项目
    Android适配--百分比的适配
    TCP通信丢包原因总结
    android 实现类似微信缓存和即时更新好友头像
    是否应该开启WebView硬件加速?
    Android通过浏览器打开app页面并且传递值
    设计模式(五)责任链模式
    Android 最新 Support V4 包大拆分有用吗?
  • 原文地址:https://www.cnblogs.com/qi09/p/2067357.html
Copyright © 2011-2022 走看看