this article is comprised of reference of book titled, “coding interview”.

First of all, I want you to read this article of geeksforgeeks site

The above site show you about difference between Stack base on Liked List and array.

If you designed data structure, First of all, You always have to desinged abstraction .

stack based on array

  • Here, I will follow abstraction of This book(coding interviewing).
struct ArrayStack {
    int top;
    int capacity;
    int *array;   // this part is different from 윤성우의 열혈강의 자료구조
};

struct ArrayStack * CreateStack() {
    struct ArrayStack *s = (ArrayStack *)malloc(sizeof(struct ArrayStack));
    if(!s)
        return NULL;
        
    s -> capacity = 1;
    s -> top = -1;
    s -> array = (int *)malloc(S->capacity * sizeof(int)); 
    if (!s-> array)
          return NULL;
  
    return s; 
}

int IsEmptyStack (struct ArrayStack *s) {
      return (s -> top == -1); // if s -> top is true,this function return 1 and if s -> top is false,  this function reture 0,
}

int IsFullStack(struct ArrayStack *s) {
      return (s->top == s -> capacity-1);
      // if s->top == s -> capacity-1 is true,this function return 1 and if s->top == s -> capacity-1 is false,  this function reture 0
}

// this function works to store data into stack. 
void Push(struct ArrayStack *s, int data) {
  // you have to ckeck whether the stack is FULL or not. 
    if (IsFullStack(s))
          printf( "Stack overflow");
    else  /// after checking it, you can store data into stack. 
        s -> array[++(s->top)]= data;
}

// this function works to get rid of data out of data. 
int Pop(struct ArrayStack *s) {
      // above all, You have to check whetehr the stack is empty or not
      if(IsEmptyStack(s)) {
          // s->top == -1 means stack is empty.
          printf("Stack is empty");
          return 0;
      }
      else 
          return (S -> array[(S -> top)--]
}

void DeletStack(struct ArrayStack *s) {
    if(s) {
        if (s -> array) free(s -> array);
        free(s);
    }
}

So, I will change the above stack into Dynamic Arraystack.

struct DynArrayStack{
    int top;
    int capacity;
    int *arrary;
}

struct DynArrayStack * CreateStacck() {
    struct DynArrayStack * sample = (DynArrayStack *)malloc(sizeof(struct DynArrayStack));
    if (!sample)
          return NULL;
    sample -> cpapcity = 1; 
    sample -> top = -1; 
    sample -> array = (int *)malloc(s -> capacity * sizeof(int));
    
    if ( ! sample -> array)
          return NULL;
    return sample;
}

int IsFullStack(struct DynArrayStack *sample) {
    return (sample -> tomp == sample -> capacity -1);
}

// this function make you enlarge the size of arraystack twice. 
int DoubleStack(struct DynArrayStack *s) {
    s -> capacity *=2;
    s -> array = (int *)realloc(s -> array, s -> capacity);
}

void Push(struct DynArrayStack *s, int x) {
    // in this case, key point is this function does not have overflow;
    if (IsFullStack(s))
        DoubleStack(s);
     s -> array[++(s->top)] = x;
}

int IsEmptyStack(struct DynArrayStack *s) {
    return (s -> top == -1);
}

int Top(struct DynArrayStack *s) {
    if (IsEmptyStack(s))
          return INT_MIN;
      
    return s->array[S -> top];
}

int Pop(struct DynArrayStack *s) {
    If(IsEmptyStack(s))
          return INT_MIN;
    return S -> array[s -> top--];
}

void DeleteStack(struct DynArrayStack *s ) {
    if (s) {
          if ( s -> array) 
              free(s->array);
          free(s);
    }

}