The following describes how to use C and C ++ to implement a chain stack (Linked List Implementation) and how to encapsulate data abstraction:
C language implementation:
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
# Include <stdio. h> # Include <stdlib. h> # Include <assert. h>Struct Link { Int data; Struct link * next; }; Struct Stack { Struct link * head; Int size; }; Void stackinit (struct stack * stack) { Stack-> head = NULL; Stack-> size = 0; } Void stackpush (struct stack * stack, const int data) { Struct link * node; Node = (struct link *) malloc (sizeof (struct link )); Assert (node! = NULL ); Node-> DATA = data; Node-> next = stack-> head; Stack-> head = node; ++ Stack-> size; } Int stackempty (struct stack * stack) { Return (Stack-> size = 0 ); } Int stackpop (struct stack * stack, int * Data) { If (stackempty (stack )) { Return 0; } Struct link * TMP = stack-> head; * Data = stack-> head-> data; Stack-> head = stack-> head-> next; Free (TMP ); -- Stack-> size; Return 1; } Void stackcleanup (struct stack * stack) { Struct link * TMP; While (Stack-> head) { TMP = stack-> head; Stack-> head = stack-> head-> next; Free (TMP ); } Stack-> size = 0; } Int main (void) { Struct stack; Stackinit (& stack ); Int I; For (I = 0; I <5; I ++) { Stackpush (& stack, I ); } While (! Stackempty (& stack )) { Stackpop (& stack, & I ); Printf ("% d", I ); } Printf ("\ n "); Return 0; } |
C ++ implementation:
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
# Include <iostream> Using namespace STD;Class Stack { PRIVATE: Struct Link { Int data _; Link * Next _; Link (INT data, link * Next): Data _ (data), next _ (next) { } }; Public: Stack (): Head _ (0), size _ (0) { } ~ Stack () { Link * TMP; While (Head _) { TMP = head _; Head _ = head _-> next _; Delete TMP; } } Void push (const int data) { Link * node = new link (data, head _); Head _ = node; ++ Size _; } Bool empty () { Return (size _ = 0 ); } Bool POP (Int & Data) { If (empty ()) { Return false; } Link * TMP = head _; Data = head _-> data _; Head _ = head _-> next _; Delete TMP; -- Size _; Return true; } PRIVATE: Link * head _; Int size _; }; // Avoid Name Conflict // Type Expansion // Data encapsulation, which can protect internal data structures from external damages
Int main (void) { Stack stack; // abstract data type Int I; For (I = 0; I <5; I ++) { Stack. Push (I); // This = & Stack } While (! Stack. Empty ()) { Stack. Pop (I ); Cout <I <""; } Cout <Endl; Return 0; } |
The output is consistent. Compared with different writing methods, you can understand some differences between the two languages. Of course, this is just an obvious aspect.
Refer:
C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications