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.

Source: Internet
Author: User

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>

 

 

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.