数据结构之-链表栈的实现及题目
Posted 技术笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构之-链表栈的实现及题目相关的知识,希望对你有一定的参考价值。
GitHub : https://github.com/hanxloop/c_dev_library
前几天码了链表和栈,栈有数组实现和链表实现,自己跑了书上的示例,能跑的通,开心,接口、实现和测试分离,实现后我会补充一些使用这些代码完成的练习题目。
0.错误提示
该文件打印程序运行时出错信息。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define Error( Str ) FatalError( Str ) 5 #define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )
1. 链表
1 /*list.c*/ 2 3 #include "list.h" 4 #include <stdlib.h> 5 #include "fatal.h" 6 7 struct Node { 8 ElementType Element; 9 Position Next; 10 }; 11 12 List MakeEmpty( List L ) { 13 if( L != NULL ) 14 DeleteList( L ); 15 16 L = malloc( sizeof( struct Node ) ); 17 if( L == NULL ) FatalError( "Out of memory!" ); 18 L->Next = NULL; 19 20 return L; 21 } 22 23 int IsEmpty( List L ) { 24 return L->Next == NULL; 25 } 26 27 int IsLast( Position P, List L ) { 28 return P->Next == NULL; 29 } 30 31 Position Find( ElementType X, List L ) { 32 Position P; 33 P = L->Next; 34 35 while( P != NULL && P->Element != X ) 36 P=Advance(P); //P = P->Next; OK 37 38 return P; 39 } 40 41 42 43 44 void Delete( ElementType X, List L ) { 45 Position P, TmpCell; 46 47 P = FindPrevious( X, L ); 48 49 if( !IsLast( P, L ) ) { 50 TmpCell = P->Next; 51 P->Next = TmpCell->Next; 52 free( TmpCell ); 53 } 54 } 55 56 Position FindPrevious( ElementType X, List L ) 57 { 58 Position P; 59 P = L; 60 61 while( P->Next != NULL && P->Next->Element != X ) 62 P = P->Next; 63 64 return P; 65 } 66 67 68 void Insert( ElementType X, List L, Position P ) { 69 Position TmpCell; 70 71 TmpCell = malloc( sizeof( struct Node ) ); 72 if( TmpCell == NULL ) 73 FatalError( "Out of space!!!" ); 74 75 TmpCell->Element = X; 76 TmpCell->Next = P->Next; 77 P->Next = TmpCell; 78 } 79 80 81 82 83 void DeleteList( List L ) { 84 Position P, Tmp; 85 P = L->Next; 86 L->Next = NULL; 87 88 while( P != NULL ) { 89 Tmp = P->Next; 90 //Tmp = Tmp->Next; OK 91 free( P ); 92 P = Tmp; 93 } 94 } 95 96 97 Position Header( List L ) { 98 return L; 99 } 100 101 Position First( List L ) { 102 return L->Next; 103 } 104 105 Position Advance( Position P ) { 106 return P->Next; 107 } 108 109 ElementType Retrieve( Position P ) { 110 return P->Element; 111 }
1 /*list.c*/ 2 3 #include "list.h" 4 #include <stdlib.h> 5 #include "fatal.h" 6 7 struct Node { 8 ElementType Element; 9 Position Next; 10 }; 11 12 List MakeEmpty( List L ) { 13 if( L != NULL ) 14 DeleteList( L ); 15 16 L = malloc( sizeof( struct Node ) ); 17 if( L == NULL ) FatalError( "Out of memory!" ); 18 L->Next = NULL; 19 20 return L; 21 } 22 23 int IsEmpty( List L ) { 24 return L->Next == NULL; 25 } 26 27 int IsLast( Position P, List L ) { 28 return P->Next == NULL; 29 } 30 31 Position Find( ElementType X, List L ) { 32 Position P; 33 P = L->Next; 34 35 while( P != NULL && P->Element != X ) 36 P=Advance(P); //P = P->Next; OK 37 38 return P; 39 } 40 41 42 43 44 void Delete( ElementType X, List L ) { 45 Position P, TmpCell; 46 47 P = FindPrevious( X, L ); 48 49 if( !IsLast( P, L ) ) { 50 TmpCell = P->Next; 51 P->Next = TmpCell->Next; 52 free( TmpCell ); 53 } 54 } 55 56 Position FindPrevious( ElementType X, List L ) 57 { 58 Position P; 59 P = L; 60 61 while( P->Next != NULL && P->Next->Element != X ) 62 P = P->Next; 63 64 return P; 65 } 66 67 68 void Insert( ElementType X, List L, Position P ) { 69 Position TmpCell; 70 71 TmpCell = malloc( sizeof( struct Node ) ); 72 if( TmpCell == NULL ) 73 FatalError( "Out of space!!!" ); 74 75 TmpCell->Element = X; 76 TmpCell->Next = P->Next; 77 P->Next = TmpCell; 78 } 79 80 81 82 83 void DeleteList( List L ) { 84 Position P, Tmp; 85 P = L->Next; 86 L->Next = NULL; 87 88 while( P != NULL ) { 89 Tmp = P->Next; 90 //Tmp = Tmp->Next; OK 91 free( P ); 92 P = Tmp; 93 } 94 } 95 96 97 Position Header( List L ) { 98 return L; 99 } 100 101 Position First( List L ) { 102 return L->Next; 103 } 104 105 Position Advance( Position P ) { 106 return P->Next; 107 } 108 109 ElementType Retrieve( Position P ) { 110 return P->Element; 111 }
testlist.c
2. 链栈
1 /* stackli.h */ 2 3 #ifndef STACK_H 4 #define STACK_H 5 6 struct Node; 7 typedef int ElementType; 8 typedef struct Node *PtrToNode; 9 typedef PtrToNode Stack; 10 11 int IsEmpty( Stack S ); 12 Stack CreateStack( void ); 13 void DisposeStack( Stack S ); 14 void MakeEmpty( Stack S ); 15 void Push( ElementType X, Stack S ); 16 ElementType Top( Stack S ); 17 void Pop( Stack S ); 18 19 #endif
1 /* stackli.c */ 2 3 #include "stackli.h" 4 #include "fatal.h" 5 #include <stdlib.h> 6 7 struct Node { 8 ElementType Element; 9 PtrToNode Next; 10 }; 11 12 13 int IsEmpty( Stack S ) { 14 return S->Next == NULL; 15 } 16 17 18 19 Stack CreateStack( void ) { 20 Stack S; 21 22 S = malloc( sizeof( struct Node ) ); 23 if( S == NULL ) 24 FatalError( "Out of space!!!" ); 25 S->Next = NULL; 26 MakeEmpty( S ); 27 return S; 28 } 29 30 void MakeEmpty( Stack S ) { 31 if( S == NULL ) 32 Error( "Must use CreateStack first" ); 33 else 34 while( !IsEmpty( S ) ) 35 Pop( S ); 36 } 37 38 39 void DisposeStack( Stack S ) { 40 MakeEmpty( S ); 41 free( S ); 42 } 43 44 45 void Push( ElementType X, Stack S ) { 46 PtrToNode TmpCell; 47 48 TmpCell = malloc( sizeof( struct Node ) ); 49 if( TmpCell == NULL ) 50 FatalError( "Out of space!!!" ); 51 else { 52 TmpCell->Element = X; 53 TmpCell->Next = S->Next; 54 S->Next = TmpCell; 55 } 56 } 57 58 59 60 ElementType Top( Stack S ) { 61 if( !IsEmpty( S ) ) 62 return S->Next->Element; 63 Error( "Empty stack" ); 64 return 0; 65 } 66 67 68 void Pop( Stack S ) { 69 PtrToNode FirstCell; 70 71 if( IsEmpty( S ) ) 72 Error( "Empty stack" ); 73 else { 74 FirstCell = S->Next; 75 S->Next = FirstCell->Next; 76 free( FirstCell ); 77 } 78 }
1 /* teststkl.c */ 2 3 #include <stdio.h> 4 #include "stackli.c" 5 6 int main( ) 7 { 8 Stack S; 9 int i; 10 11 S = CreateStack( ); 12 for( i = 0; i < 10; i++ ) 13 Push( i, S ); 14 15 while( !IsEmpty( S ) ) 16 { 17 printf( "%d\n", Top( S ) ); 18 Pop( S ); 19 } 20 21 DisposeStack( S ); 22 return 0; 23 }
3. 数组栈
1 /* stackar.h */ 2 3 #ifndef _Stack_h 4 #define _Stack_h 5 6 typedef int ElementType; 7 struct StackRecord; 8 typedef struct StackRecord *Stack; 9 10 int IsEmpty( Stack S ); 11 int IsFull( Stack S ); 12 Stack CreateStack( int MaxElements ); 13 void DisposeStack( Stack *S ); 14 void MakeEmpty( Stack S ); 15 void Push( ElementType X, Stack S ); 16 ElementType Top( Stack S ); 17 void Pop( Stack S ); 18 ElementType TopAndPop( Stack S ); 19 20 #endif
1 /* stackar.c */ 2 3 #include "stackar.h" 4 #include "fatal.h" 5 #include <stdlib.h> 6 7 #define EmptyTOS ( -1 ) 8 #define MinStackSize ( 5 ) 9 10 struct StackRecord { 11 int Capacity; 12 int TopOfStack; 13 ElementType *Array; 14 }; 15 16 17 int IsEmpty( Stack S ) { 18 return S->TopOfStack == EmptyTOS; 19 } 20 21 22 int IsFull( Stack S ) { 23 return S->TopOfStack == S->Capacity - 1; 24 } 25 26 27 Stack CreateStack ( int MaxElements ) { 28 Stack S; 29 30 if( MaxElements < MinStackSize ) 31 Error( "Stack size is too small" ); 32 33 S = malloc( sizeof( struct StackRecord ) ); 34 if( S == NULL ) 35 FatalError( "Out of space!!!" ); 36 37 S->Array = malloc( sizeof( ElementType ) * MaxElements ); 38 if( S->Array == NULL ) 39 FatalError( "Out of space!!!" ); 40 S->Capacity = MaxElements; 41 MakeEmpty( S ); 42 43 return S; 44 } 45 46 47 void MakeEmpty( Stack S ) { 48 S->TopOfStack = EmptyTOS; 49 } 50 51 void DisposeStack( Stack *S ) { 52 if( S != NULL ) { 53 free( (*S)->Array ); 54 free( *S ); 55 } 56 } 57 58 59 void Push( ElementType X, Stack S ) { 60 if( IsFull( S ) ) 61 Error( "Full stack" ); 62 else 63 S->Array[ ++S->TopOfStack ] = X; 64 } 65 66 67 68 ElementType Top( Stack S ) { 69 if( !IsEmpty( S ) ) 70 return S->Array[ S->TopOfStack ]; 71 else { 72 Error( "Empty stack" ); 73 return 0; /* Return value used to avoid warning */ 74 } 75 } 76 77 void Pop( Stack S ) { 78 if( IsEmpty( S ) ) 79 Error( "Empty stack" ); 80 else 81 S->TopOfStack--; 82 } 83 84 85 ElementType TopAndPop( Stack S ) { 86 if( !IsEmpty( S ) ) 87 return S->Array[ S->TopOfStack-- ]; 88 Error( "Empty stack" ); 89 return 0; 90 }
1 /*teststka.c */ 2 3 #include <stdio.h> 4 #include "stackar.c" 5 6 int main() { 7 Stack S; 8 int i; 9 10 S = CreateStack( 12 ); 11 for( i = 0; i < 10; i++ ) 12 Push( i, S ); 13 14 while( !IsEmpty( S ) ) { 15 printf( "%d\n", Top( S ) ); 16 Pop( S ); 17 } 18 19 DisposeStack( &S ); 20 return 0; 21 }
以上是关于数据结构之-链表栈的实现及题目的主要内容,如果未能解决你的问题,请参考以下文章