/ ************************************************** ******************************* REVISION log entry revision by: http://blog.9cbs.net/hongweijin revised on: 2004-10-12 21:33 : 31 Comments: Implementation of expressions with linked lists ***************************************************************************** ******************************* /
#include
#define elementtype int # Define Status Int
#define null 0 # Define true 1 # define false 0 TypeDef struct stacknode * ptrtonode;
Struct stacknode {elementtype element; ptrtonode next;
PtrToNode InitStack (); Status DestroyStack (PtrToNode); Status StackEmpty (PtrToNode); int StackLength (PtrToNode); Status GetTop (PtrToNode, ElementType &); Status Push (PtrToNode, ElementType); Status Pop (PtrToNode, ElementType &);
///// // Function Name: Main // Function Description: MAIN function Perform specific function function // parameter: void // return value: void //// void main (void) {PTRTONODE TOP; TOP = INITSTACK ();
ElementType Element; While (1) {Scanf ("% D", & Element); if (Element <0) Break; if (! Push (top, element)) {printf ("/ N stack error! / N" Return;}}
Printf ("/ NTHE Stacklength IS:% D / N", StackLength (TOP)); Gettop (Top, Element); Printf ("/ NTHE TOP NUMBER IS:% D / N", ELEMENT); Printf ("/ NSTACK IS EMPTY?:% D / N ", Stackempty (TOP)); While (1) {if (! POP (TOP (TOP, ELEMENT)) BREAK; Printf ("% D ", Element);}
Destroystack (TOP);
Printf ("/ NSTACK IS EMPTY?:% D / N", Stackempty (TOP));
///// / / Function Name: INitStack // Function Description: Construct an empty linked list stack // Parameter: PTRTONODE & Stack // Return Value: PTRTONODE //// PTRTONODE INITACK () {PTRTONODE Stack; / * Assignment One space is used for the initialization of the headrest * / stack = (ptrtonode) malloc (STACKNODE)); / * System allocates an error message, this is the programmer's obligation * / if (stack == null) {printf "/ N system initialization failed! / n"); Return False;} / * Initial Stack top element NEXT domain * / stack-> next = null; stack-> element = null; return stack;} ///// // Function Name: DestroyStack // Function Description: Destroy an existing stack // parameter: PTRTONODE & Stack // Return Value: status //// status destroystack (PTRTONODE Stack) {/ * Remove the auxiliary pointer used * / Ptrtonode client = null; while (stack-> next! = Null) {client = stack-> next-> next; free (stack-> next); stack = client;}
Return True;}
/////// function name: StackEmpty // Function: determining a stack is not empty @ parameters: PtrToNode stack // Return value: Status ///// Status StackEmpty (PtrToNode stack) {if (stack -> Next == null) Return True;
Return false;}
////// Function Name: stacklength // Function Description: Get the length of the stack // Parameters: PTRTONODE Stack // Return Value: int //// Int Stacklength (PTRTONODE Stack) {INT Client = 0;
While (stack! = null) {stack = stack-> next; client ;}
Return Client - 1; / * The head knot cannot be counted * /}
////// / Function Name: Gettop // Function Description: Get the stack of stack top elements to return // parameters with client: PTRTONODE Stack // Parameters: ElementType & Client // Return Value: status //// status gettop (PTRTONODE Stack, ElementType & Client) {if (stack-> next == null) Return False; / * Head Node is just used for initialization, no data * / client = stack-> next-> Element;
Return client;
////// Function Name: PUSH // Function Description: Pressing Elements in the Stack: PTRTONODE Stack // Parameters: ElementType Client // Return Value: Status //// Status Push (PTRTONode Stack , ElementType Client) {PTRTONODE TEMP = NULL; TEMP = (PTRTONODE) Malloc (STACKNODE)); if (Temp == Null) {Printf ("/ N System Initialization Failure! / N"); Return False;} Temp -> Next = stack-> next; stack-> next = Temp; Temp-> Element = Client; Return true;} ////// function name: POP // function Description: From a stack that is not empty One element // parameter: PTRTONODE Stack // Parameters: ElementType & Client // Return Value: Void /////
Status Pop (PtrToNode stack, ElementType & client) {PtrToNode tempNext = NULL; if (stack-> next == NULL) return FALSE; tempNext = stack-> next; stack-> next = stack-> next-> next; client = TempNext-> Element; Free (TempNext); Return True;}