Stack-Array Implementation

Source: Internet
Author: User

 

Using arrays to implement stacks avoids the use of pointers, but the defect is that the size of arrays must be determined in advance. In general, this is not a big problem. :

Array Implementation Stack:

First, define a structure. topofstack indicates the top of the stack. When topofstack is-1, it indicates the empty stack. Array array used to store stack Elements

When pushing, ++ topofstack then adds elements to the array. Direct topofstack --;

C language implementation:

#ifndef _stack_hstruct stack;typedef struct stack *Stack;typedef int ElementType;int isEmpty(Stack s);int isFull(Stack s);Stack CreateStack(int MaxElements);void Push(ElementType x,Stack s);ElementType Top(Stack s);void Pop(Stack s);ElementType TopAndPop(Stack s);#endif
#include<stdio.h>#include<stdlib.h>#include"stack_array.h"#define ElementTOS (-1)#define MinStackSize (5)struct stack{    int Capacity;    int TopOfStack;    ElementType *Array;};int isEmpty(Stack s){    return s->TopOfStack == ElementTOS;}int isFull(Stack s){    return s->TopOfStack == s->Capacity;}Stack CreateStack(int MaxElements){    Stack s;    if(MaxElements<MinStackSize)    {        printf("The stack is too small!\n");        return NULL;    }    s = (Stack)malloc(sizeof(struct stack));    if(s==NULL)    {        printf("out of space!\n");        return NULL;    }    s->Capacity = MaxElements-1;    s->TopOfStack = ElementTOS;    s->Array = (ElementType *)malloc(MaxElements*sizeof(ElementType));    return s;}void Push(ElementType x,Stack s){    if(!isFull(s))        s->Array[++s->TopOfStack] = x;    else         printf("The stack is Full!\n");}ElementType Top(Stack s){    if(!isEmpty(s))        return s->Array[s->TopOfStack];    else    {        printf("The stack is Empty!\n");        return 0;    }}void Pop(Stack s){    if(!isEmpty(s))        s->Array[s->TopOfStack--];    else         printf("The stack is Empty!\n");}ElementType TopAndPop(Stack s){    if(!isEmpty(s))        return s->Array[s->TopOfStack--];    else    {        printf("The stack is empty!\n");        return 0;    }}int main(){    Stack s;    s = CreateStack(6);    Push(1,s);    Push(2,s);    Push(3,s);    Push(4,s);    Push(5,s);    Push(6,s);    Push(7,s);        for(int i=0;i<6;i++)    {        printf("The top of stack is %d\n",Top(s));        Pop(s);    }    Pop(s);    return 0;}

 

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.