C Language implementation of stacks

Source: Internet
Author: User

Tag: Stack

#include <iostream>using namespace std; #define Stack_init_size 10#define stackincrement 10#define elemtype Inttypedef struct{elemtype *base;int top;size_t capacity;} Sqstack;bool isfull (Sqstack *st) {return st->top >= st->capacity;} BOOL IsEmpty (Sqstack *st) {return st->top = = 0;} BOOL Initstack (Sqstack *st) {st->base = (elemtype*) malloc (stack_init_size * sizeof (Elemtype)); st->top = 0;st-> capacity = Stack_init_size;return true;} Elemtype GetTop (Sqstack *st) {if (! IsEmpty (ST)) {return st->top;} cout<< "Stack empty!" <<endl;return-1;} void Push (Sqstack *st, elemtype Item) {if (! Isfull (ST)) {st->base[st->top++] = Item;} else{cout<< "Stack full!" <<endl;}} void Show (Sqstack *st) {for (int i=st->top-1; i>=0;-I.) {cout<<st->base[i]<< "";} Cout<<endl;} Elemtype Pop (Sqstack *st) {if (! IsEmpty (ST)) {St->top--;return st->base[st->top];} return-1;} int main () {Sqstack st;initstack (&st); int i = 0;for (; i<5; ++i) Push (&st,i); Pop (&AMP;ST); Show (&st); return 1;}

C Language implementation of stacks

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.