Algorithm-Data Structure-Stack-general Linux technology-Linux programming and kernel information. The following is a detailed description. /*************************************** *************************
Title: stack. c
Author: Zhang Long
Time: 200508
**************************************** *************************/
/* = ======= */
Void init (stack * s );
Int IsEmpty (stack * s );
Int IsFull (stack * s );
Int GetStackSize ();
Int length (stack * s );
ElementType top (stack * s );
Int push (stack * s, ElementType x );
Int pop (stack * s );
If (IsFull (s )){
Printf ("the stack is full, no new data can be inserted. \ n ");
Return 0;
}
If (! S-> bottom ){
Node = s-> bottom = s-> top = (stacknode *) malloc (sizeof (stacknode ));
Node-> data = x;
}
Else {
Node = (stacknode *) malloc (sizeof (stacknode ));
Node-> data = x;
S-> top-> next = node;
S-> top = node;
}
S-> count ++;
Return;
}
ElementType pop (stack * s)
{
Int I, j;
ElementType data;
Stacknode * node;
If (IsEmpty (s )){
Printf ("the stack is empty, no data is popped up \ n ");
Return 0;
}
Data = s-> top-> data;
Free (s-> top );
S-> count --;
J = s-> count;
If (j = 1 ){
S-> top = s-> bottom;
S-> top-> next = 0;
}
Else {
For (I = 0; I
If (I = 0 ){
Node = s-> bottom;
}
Else {
Node = node-> next;
}
}
S-> top = node;
S-> top-> next = 0;
}
Return data;
}
/* = =============== */
Int main (int argc, char * argv [])
{
Int I, j;
Stack * mystack;
Mystack = (stack *) malloc (sizeof (stack ));
Init (mystack );
For (I = 0; I <100; I ++ ){
J = I + 3;
Push (mystack, j );
Printf ("% d", top (mystack ));
}
Printf ("\ n ");
Printf ("the size of stack is % d \ n", GetStackSize ());
Printf ("the current length of stack is % d \ n", length (mystack ));
For (I = 0; I <50; I ++ ){
Printf ("% d", pop (mystack ));
}
Printf ("\ n ");
Printf ("the size of stack is % d \ n", GetStackSize ());
Printf ("the current length of stack is % d \ n", length (mystack ));
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service