Algorithm-Data Structure-Stack

Source: Internet
Author: User
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
**************************************** *************************/




# Include "stdio. h"




# Define STACK_SIZE100




Typedef int ElementType;

Typedef struct _ stacknode
{
ElementType data;
Struct _ stacknode * next;
} Stacknode;

Typedef struct _ stack
{
Stacknode * top, * bottom;
Int count;
} Stack;




/* = ======= */
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 );




/* = */
Void init (stack * s)
{
S-> top = s-> bottom = 0;
S-> count = 0;
}

Int IsEmpty (stack * s)
{
If (! S-> count)
Return 1;
Else
Return 0;
}

Int IsFull (stack * s)
{
If (s-> count = STACK_SIZE)
Return 1;
Else
Return 0;
}

Int GetStackSize ()
{
Return STACK_SIZE;
}

Int length (stack * s)
{
Return s-> count;
}

ElementType top (stack * s)
{
Return s-> top-> data;
}

Int push (stack * s, ElementType x)
{
Stacknode * node;

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 ));

Return 1;

}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.