Simple implementation of the Stack (1)-array implementation

Source: Internet
Author: User

Introduction

Stack is a widely used linear data structure that allows for insertion or deletion only at one end of a table, so that the stack can also be called a linear table with limited operation . In the stack, the one end that allows insertions or deletions is called the top of the stack (top) and the other end of the insert and delete is called the bottom of the stack (bottom);

as Follows:

This article simply implements the stack and its basic operations with an array.

The code is as Follows:
#define MaxSize-struct{    int  data[maxsize];     int top;} seqstack;
View Code

Note: This assumes that the data stored in the stack is an integer (int)

Basic Operations 1. Initialization of stacks
void init (seqstack* s) {    s->top =-1;     // }
View Code2. Incoming stack
void int x) {    If(s->top<maxsize-1)   //       s->data[++ (s->top)] = x;}
View Code3. The Stack
int  Pop (seqstack* s) {    if(s->top>-1)   //       return (s->data[s->top--]);}
View Code4. Emptying the Stack
void Clear (seqstack* s) {    s->top =-1;}
View Code5. Determine if the stack is empty
bool isEmpty (seqstack* s) {    if(s->top==-1) {          Returntrue;    } Else {        returnfalse;    }}
View Code6. Calculate the length of the stack
int length (seqstack* s) {    return (s->top+1);}
View Code

To Test:
intMainvoid) {seqstack s; Init (&s); Push (&s,1); Push (&s,2); Push (&s,3); printf ("%d\n", Length (&s)); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } printf ("%d\n", Pop (&s)); printf ("%d\n", Pop (&s)); printf ("%d\n", Pop (&s)); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } push (&s,3); Clear (&s); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); }            return 0;}
View CodeTest Results:

Full code
#include <stdio.h>#defineMaxSize 100typedefstruct{    intdata[maxsize]; inttop;} seqstack;voidInit (seqstack*s);BOOLIsEmpty (seqstack*s);voidPush (seqstack* s,intx);intPop (seqstack*s);voidClear (seqstack*s);intLength (seqstack*s);intMainvoid) {seqstack s; Init (&s); Push (&s,1); Push (&s,2); Push (&s,3); printf ("%d\n", Length (&s)); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } printf ("%d\n", Pop (&s)); printf ("%d\n", Pop (&s)); printf ("%d\n", Pop (&s)); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } push (&s,3); Clear (&s); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); }            return 0;}voidInit (seqstack*S) {s->top =-1;//the array subscript starts at 0, so this indicates that the stack is empty}BOOLIsEmpty (seqstack*S) {    if(s->top==-1){        return true; }Else{        return false; }}voidPush (seqstack* s,intx) {    if(s->top<maxsize-1)//stack is not full to enter the stacks->data[++ (s->top)] =x;}intPop (seqstack*S) {    if(s->top>-1)//stack is not empty to be out of stack      return(s->data[s->top--]); }voidClear (seqstack*S) {s->top =-1;}intLength (seqstack*S) {    return(s->top+1);}
View Code

Simple implementation of the Stack (1)-array implementation

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.