[Library] C-implemented generic Stack

Source: Internet
Author: User

/*
Name: Stack. h
Copyright: 1.0
Author: aveon
Date: 02-10-04 19: 48
Description: stack of Generic Design
*/
# Ifndef avalon_stack_h
# Define avalon_stack_h
# Include <stdio. h>

# Ifndef avalon_bool
# Define avalon_bool
# Define true 1
# Define false 0
Typedef int bool;/* Custom bool type */
# Endif

Typedef struct stack * stackhandle;/* stack handle */

/*******
Interface
*******/
Stackhandle initstack (size_t size );
/* Construct an empty stack */
Bool gettop (stackhandle S, void * ELEM );
/* If the stack is not empty, use ELEM to return the top element of S stack and return true */
Bool push (stackhandle S, void * ELEM );
/* Insert the element. ELEM is the new stack top element */
Bool POP (stackhandle S, void * ELEM );
/* If the stack is not empty, delete the stack top element of S and return its value with ELEM,
And return true. ELEM. The value can also be null, then only the pop-up action */
Bool clearstack (stackhandle S );
/* Set S to empty stack */
Bool destroystack (stackhandle * s );
/* Destroy stack S */
Bool stackempty (stackhandle S );
/* Is the stack empty? */
Int stacklength (stackhandle S );
/* Stack length */

# Endif

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////

/*
Name: Stack. c
Copyright: 1.0
Author: aveon
Date: 02-10-04 19: 48
Description: stack of Generic Design
*/
# Include <stdio. h>
# Include <assert. h>
# Include <mem. h>

# Ifndef avalon_bool
# Define avalon_bool
# Define true 1
# Define false 0
Typedef int bool;
# Endif

/*******
Data Structure
*******/
Typedef struct node {
Struct node * Prior;/* front position */
Void * data ;/**/
} Node, * nodehandle;

Typedef struct stack {
Nodehandle base;/* stack bottom */
Nodehandle top;/* stack top */
Int size_of_stack;/* stack length */
Size_t size_of_data ;/**/
} Stack, * stackhandle;

/******
Internal functions
******/
Nodehandle allocnode (stackhandle S, void * ELEM)
{/* For internal use only for allocated space */
Nodehandle temp;
Size_t size;
Assert (null! = S & null! = ELEM );/**/
Size = s-> size_of_data;

Temp = (nodehandle) malloc (sizeof (node ));
Assert (null! = Temp );
(Temp-> data) = (void *) malloc (size );

Memcpy (temp-> data, ELEM, size );
Return temp;
}
Bool freenode (nodehandle * n)
{/* Delete a node */
Assert (null! = * N & null! = N );/**/
Free (* n)-> data );
Free (* n );
Return true;
}

/*******
External Interface
*******/
Stackhandle initstack (size_t size)
{/* Construct an empty stack */
Stackhandle S = (stackhandle) malloc (sizeof (stack ));
Assert (null! = S );/**/
S-> Top = s-> base = NULL;
S-> size_of_stack = 0;
S-> size_of_data = size;
Return S;
}
Bool gettop (stackhandle S, void * ELEM)
{/* If the stack is not empty, use ELEM to return the top element of S stack and return true */
If (S = NULL | S-> base = s-> top)
Return false;
Memcpy (ELEM, S-> top-> data, S-> size_of_data );
Return true;
}
Bool push (stackhandle S, void * ELEM)
{/* Insert element. ELEM is the new stack top element */
Nodehandle temp;
Assert (null! = S );
Assert (null! = ELEM );

Temp = allocnode (S, ELEM);/* allocate a node */
(S-> size_of_stack) ++;

Temp-> prior = s-> top;
S-> Top = temp;
Return true;
}
Bool POP (stackhandle S, void * ELEM)
{/* If the stack is not empty, delete the stack top element of S, return its value with ELEM, and return true.
ELEM can also be set to null. Only pop-up operations are performed */
Nodehandle temp;

If (0 = s-> size_of_stack)/* empty stack */
Return false;
Temp = s-> top-> prior;
(S-> size_of_stack )--;
If (null! = ELEM)/* If the parameter is null, it will pop up directly */
Gettop (S, ELEM );
Freenode (& (S-> top ));
S-> Top = temp;
Return true;
}
Bool clearstack (stackhandle S)
{/* Set S to an empty stack */
Assert (null! = S );
While (0! = S-> size_of_stack ){
Pop (S, null );
}
Return true;
}
Bool destroystack (stackhandle * s)
{/* Destroy stack S */
Assert (null! = S );
Assert (null! = * S );
If (0! = (* S)-> size_of_stack)
Clearstack (* s );
Free (* s );
* S = NULL;
Return true;
}
Bool stackempty (stackhandle S)
{/* Is the stack empty? */
Assert (null! = S );
If (0 = s-> size_of_stack) return true;
Return false;
}
Int stacklength (stackhandle S)
{/* Stack length */
Assert (null! = S );
Return S-> size_of_stack;
}





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.