Project structure:
main.cpp:
#include <iostream> #include<stdio.h> #include<stdlib.h> #include "function_for_LinkStack.h" using namespace std; int main() { TestLinkStack(); return 0; }
function_for_LinkStack.h:
FUNCTION_FOR_LINKSTACK_H_INCLUDED #ifndef #define FUNCTION_FOR_LINKSTACK_H_INCLUDED / * link stack operation is limited single list, the list can only be operated at a head the head stack pointer list is not required of the head node substantially in the absence of a full stack of empty stack pointer corresponding to the head null point insertion and deletion only on the execution stack link direction between the node :( head node) the stack -> bottom of the stack * / typedef char elemType; typedef struct StackNode { elemType data; // data field struct StackNode * Next; // pointer field } StackNode, * LinkStack; // initialize the link stack void InitStack (LinkStack & S); // judgment empty int StackEmpty (LinkStack S); // stack void the Push (LinkStack & S, elemType E); // popped void Pop (LinkStack & S); // pop the top element and acquires elemType Pop_with_elem (LinkStack & S); // Get the top element elemType getTop (LinkStack & S); // print elements in the stack (from top to bottom) void PrintStack (LinkStack S); // test void TestLinkStack (); #endif // FUNCTION_FOR_LINKSTACK_H_INCLUDED
function_for_LinkStack.cpp:
#include <stdio.h> #include <stdlib.h> #include " function_for_LinkStack.h " // initialize the link stack void InitStack (LinkStack & S) { // Constructs an empty stack, stack pointer blank S = NULL; } // determination empty int StackEmpty (LinkStack S) { IF (S == NULL) { return . 1 ; } the else { return 0 ; } } // stack void the push (LinkStack & S, elemType E) { LinkStack P= (LinkStack) the malloc ( the sizeof (StackNode)); // create a new node p-> Data = E; // data field set to E p-> Next = S; // the new node is inserted into the stack S = p; // modify the stack pointer } // pop void Pop (LinkStack & S) { // first determine whether the stack is empty iF (S == NULL) { Exit ( 0 ); } LinkStack P = S; // declare a new node points to the top S = -S-> Next; Free (P); // release temporary node } // Stack and acquires the top element elemType Pop_with_elem (LinkStack & S) { elemType E; // store the data field of the top element // first determine whether the stack is empty IF (S =! NULL) { Exit ( 0 ); } LinkStack P = S; // declare a new node points to the top S = -S-> Next; E = p-> data; Free (P); // release provisional nodes return E; // return data type field } // Get the top element elemType getTop (LinkStack & S) { IF (S! =NULL) { return -S-> Data; } } // print elements in the stack (from top to bottom) void PrintStack (LinkStack S) { LinkStack P = S; // declare a pointer points to the top of the while ! (P = NULL) { the printf ( " % C " , p-> data); // sequentially output node of each data field P = p-> Next; } } // test void TestLinkStack () { LinkStack S; // declaration node stack the printf ( " initialization: \ n- " ); InitStack (S); // push the printf ( " press-fitting elements: \ n- " ); the Push (S, ' A ' ); the Push (S, ' B ' ); the Push (S, ' C ' ); the Push (S, ' D ' ); the Push (S, ' E ' ); the printf ( " \ n-Get the top element: C% \ n- " , getTop (S)); the printf ( " \ n-D stack is empty% \ n-? " , StackEmpty (S)); printf ( " \ print stack the n-: \ the n- " ); PrintStack (S); printf ( " \ the n-pop: \ the n- " ); Pop (S); printf ( " \ obtain the n-top element:% c \ the n- " , getTop (S)); printf ( " \ the n-stack is empty? % D \ n- " , StackEmpty (S)); the printf ( " \ n-print stack: \ n- " ); PrintStack (S); the printf ( " \ n-then pushes a new element: \ n- " ); the Push (S, ' F ' ); the printf ( " \ n-D stack is empty% \ n-? " , StackEmpty (S)); the printf ( " \ n-print stack: \ n- "); PrintStack (S); the printf ( " \ n-Get the top element: C% \ n- " , getTop (S)); }
operation result: