First, correct the error of the previous stackpop code.
E_state stackpop (linkstack * stack, elelmenttype * Data) {If (Stack-> top! = Stack-> bottom) {// first point to the first element. lstacknode * Next = stack-> bottom; <strong> <span style = "font-size: 18px; color: # ff6666; "> * Data = stack-> top-> data; </span> </strong> // find the precursor of the top element of the stack while (next-> next! = Stack-> top) {next = Next-> next;} Free (Stack-> top); next-> next = NULL; stack-> Top = next; // you forgot to add stack-> Len --; return e_state_ OK;} else {return e_state_error ;}}
The following example shows how to convert the hexadecimal value to another hexadecimal value:
// Convert int _ tmain (INT argc, _ tchar * argv []) {linkstack stack; stackinit (& stack); // The number int number to be converted; // a few hexadecimal int base; printf ("enter a 10 hexadecimal number:"); scanf ("% d", & number ); printf ("Enter the number of hexadecimal conversions:"); scanf ("% d", & base); int oldnumber = number; while (number! = 0) {stackpush (& stack, Number % base); number = Number/base;} int pop; printf ("% d to % d :", oldnumber, base); While (stackpop (& stack, & pop )! = E_state_error) {printf ("% d", pop);} printf ("\ n"); stackdestory (& stack); Return 0 ;}
View data structure write code (10) stack application (1) Numerical Conversion