Stack
A stack is a linear table that restricts insert or delete operations only at the end of a table. The head end is called the bottom of the stack. The end of the table is called the top of the stack.
(1) advanced post-out
(2)top = base means stack empty
(3) A stack-top pointer in a non-empty stack is always at the next position of the top element of the stack, as
(4) If base = NULL indicates that the stack structure does not exist
Implementation code of the stack
1#include <stdio.h>2#include <stdlib.h>3 #defineStack_init_size 1004 #defineStackincrement 105typedefintStatus;6 #defineOK 17 #defineERROR 08typedefstruct 9 {Ten Char*Base;/*base value is null before stack construction and after destruction*/ One Char*top;/*stack Top pointer*/ A intStackSize/*The currently allocated storage space, in element units*/ -}sqstack; - the //Create a stack - voidCreatestack (Sqstack *s) -{/*constructs an empty stack s*/ -(*s).Base= (Char*)malloc(stack_init_size*sizeof(Char)); + if(! (*s).Base) exit (ERROR);/*Storage Allocation Failure*/ -(*s). Top = (*s).Base; +(*s). StackSize =stack_init_size; A } at - - //into the stack - voidPush (Sqstack *s,CharX/*Insert element e as new element*/ - { - if((*s). Top-(*s).Base) >= (*s). stacksize) in { -(*s).Base= (Char*)realloc((*s).Base, ((*s). StackSize + stackincrement) *sizeof(Char)); to if(! (*s).Base) exit (ERROR);/*Storage Allocation Failure*/ +(*s). Top = (*s).Base+ (*s). stacksize; -(*s). StackSize + =stackincrement; the } ** ((*s). top) + + =x; $ }Panax Notoginseng - the //out of the stack + intPop (Sqstack *s) A{/*if the stack is not empty, delete the top element of S, return its value with E, and return the value*/ the if((*s). Top = = (*s).Base)returnERROR; + Else { -(*s). Top = (*s). Top-1; $ $ Charstr; -str = * (*s). top); -printf"%c", str); the } - returnOK;Wuyi } the - intMain () Wu { -Sqstack S; About $Createstack (&s); - - intnum, I; - CharA, C;//c to eat the Enter key Aprintf"Enter the number of elements in the stack: \ n"); +scanf"%d", &num); thec = GetChar ();//scanf input rules can be seen in the comments below -printf"input into stack element: \ n"); $ for(i =0; i < num; i++){ thescanf"%c", &a); thec =GetChar (); thePush (&S, a); the } -printf"the stack order is: \ n"); in for(i =0; i < num; i++){ thePop (&s); the } AboutSystem"Pause"); the return 0; the}
Input rules for scanf:
The
scanf function is most commonly used in the two input methods of%d integer input and the%c character input method.
Enter an integer in%d, type a number and press the Enter character, and the input is complete. When you enter characters in%c, it's a little bit more complicated. In general, when you enter characters with%c, when you are finished, you are accustomed to pressing the carriage return, when the carriage return becomes a problem.
Example 1:
scanf ("%c", &a);/* Enter a character at this time and press ENTER, then variable a will receive the input characters correctly */
scanf ("%c", &b);/* But variable B will receive a carriage return, The value is 10, or the ASCII code is 10*/
Example 2:
scanf ("%d", &x); /* Enter a serial number and return, the variable x will normally receive the input integer */
scanf ("%d", &y);/* variable y will not receive a carriage return, Requires the user to continue entering the number, and return to the normal received integer */
"Example 3":
scanf ("%d", &x); /* Enter a serial number and return, the variable x will normally receive the input integer */
scanf ("%c", &a);/* But variable a will receive a carriage return with a value of 10 , or the ASCII code is 10*/
.
"Example 4":
scanf ("%d", &x); /* Enter a serial number and return, and the variable x will normally receive the input integer */
scanf ("%d", &y); /* If you enter a letter instead of a number, say "s" and enter */
/* Then the variable y will not receive any character or number, its value remains the same, no change occurs.
scanf ("%c", &a); /* The variable a will receive the letter entered in the previous step */
/* The character stored in variable A will be "s" instead of a carriage return */
Summary: Can be used with char a = GetChar (), or char A; scanf ("%c", &a); (GetChar () can only enter a character type, and enter the return key to extract characters from the buffer sequentially.)
Data structure C language stack