C language: One attempt at a generic stack __c language

Source: Internet
Author: User

Try to do a generic stack with C and throw it out to communicate with you.

#include <stdio.h>/** * Make a struct for stacks.
 * I hope to make a generic struct of stacks.
 * I may have a try.	* * *//** Two variables: * 01.
 Pointer to Char. * 02.
 The position of the stack. * FROM 0 to MAX * 03.
 The length of the array of char which is malloc.;

* * typedef struct STACK *STACKSP;
	struct Stack {void *pt;
	size_t top;
size_t length;


}; 
 /** function judge whether stack is empty.
 * Argument SP is a pointer to Stack.
 * Should be aware this is equal to zero while the stack is empty.
 * * #define SIZE_T ifempty (Stacksp sp) sp->top/** Push an element into the top of the stack. */#define PUSH (VarType, STACKSP, value) \ if (stacksp->top = = stacksp->length) fprintf (stderr, "Error:stack is full
	.");\ ((VarType *) stacksp->pt)


[stacksp->top++] = value;
 /** Pop () function return a element from the top of the stack.
 * Warning:the element is also a pointer to void. * So your cannot referrence the variable by this pOinter, * you are to make some conventions. * * #define POPs (vartype, STACKSP) \ (stacksp->top = 0)?
 NULL: ((VarType *) stacksp->pt) [--stacksp->top])/** free of the space applied.  * * #define CLOSESTACK (VarType, Stacksp) if (stacksp->pt = = NULL) {\ free (VarType *) stacksp->pt); \ stacksp->top =


0;}
 /** Malloc a space for stack. * **/#define OpenStack (vartype,stacksp,op_length) \ stacksp->pt = malloc (sizeof (vartype) * op_length); \ stacksp- >length = Op_length;
 \ stacksp->top = 0;


Compile a few warning, obviously with C do generics or some irrational.

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.