I am writing a stack in C language today. I have encountered many problems and feel that my foundation is still quite poor. Now I will share it with you.
The program structure is as follows:
Basedata. h </P> <p> # ifndef base_data_h <br/> # define base_data_h </P> <p> typedef int status; </P> <p> # define OK 1 <br/> # define error 0 <br/> # define true 1 <br/> # define false 0 <br/> # define overflow-2 </P> <p> # endif <br/>Sqstack. h </P> <p> # include "basedata. H "<br/> # ifndef my_stack <br/> # define my_stack </P> <p> # define stack_init_size 20; <br/> # define stackincrement 10; </P> <p> typedef int sqstackelemtype; </P> <p> typedef struct {<br/> sqstackelemtype * base; <br/> sqstackelemtype * top; <br/> int stacksize; <br/>} sqlstack; </P> <p> Status initstack (sqlstack * P ); </P> <p> Status destroystack (sqlstack * P); </P> <p> Status clearstack (sqlstack * P ); </P> <p> Status stackempty (sqlstack P); </P> <p> Status stacklength (sqlstack P ); </P> <p> sqstackelemtype getstacktop (sqlstack * P); <br/> sqstackelemtype POP (sqlstack * P ); </P> <p> Status push (sqlstack * P, sqstackelemtype E); </P> <p> # endif <br/>
# Include <stdio. h> <br/> # include <stdlib. h> </P> <p> # include "sqstack. H "</P> <p> Status initstack (sqlstack * SP) <br/>{< br/> int initsize = sizeof (sqstackelemtype) * stack_init_size; </P> <p> (* SP ). base = (sqstackelemtype *) malloc (initsize); <br/> // allocate the initial storage space for the sequence stack, in 20 units </P> <p> If (! (* SP ). base) <br/> return error; <br/> // check whether the allocation is successful <br/> (* SP ). top = (* SP ). base; <br/> // the top and bottom of the stack point to the same position at the beginning, and the bottom of the stack <br/> (* SP ). stacksize = stack_init_size; <br/> // store the initial allocated size <br/> Return OK; <br/>}</P> <p> sqstackelemtype gettop (sqlstack P) <br/>{< br/> If (P. top = P. base) Return Error; <br/> return * (P. top)-1); <br/>}</P> <p> sqstackelemtype POP (sqlstack * P) <br/>{< br/> If (* P ). base = (* P ). top) <br/> return error; <br/> return * (-- (* P ). top); <br/>}</P> <p> Status push (sqlstack * P, sqstackelemtype E) <br/>{< br/>}</P> <p>