Skip to content

Commit

Permalink
add by jinshaohui for array stack
Browse files Browse the repository at this point in the history
  • Loading branch information
jinshaohui committed Oct 12, 2018
1 parent 1ec5a4e commit ebf7280
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
161 changes: 161 additions & 0 deletions c-cpp/08_stack/arrayStack/arrayStack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*************************************************************************
> File Name: arrayStack.c
> Author: jinshaohui
> Mail: [email protected]
> Time: 18-10-12
> Desc: 数组实现顺序栈
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"./arrayStack.h"

/*创建并初始化顺序栈*/
stArrayStack * arrayStack_create(int size)
{
stArrayStack *parrStack = NULL;

parrStack = (stArrayStack *)malloc(sizeof(stArrayStack));
if (parrStack == NULL)
{
return NULL;
}

parrStack->size = size;
parrStack->pos = -1;
parrStack->array = (int *)malloc(sizeof(int)*size);
if(parrStack->array == NULL)
{
free(parrStack);
return NULL;
}

return parrStack;
}
/*销毁顺序栈*/
void arrayStack_destory(stArrayStack * parrStack)
{
if(parrStack == NULL)
{
return;
}

if (parrStack->array != NULL)
{
free(parrStack->array);
}

free(parrStack);
return;
}
/*出栈*/
int arrayStack_pop(stArrayStack *parrStack)
{
int data = 0;

if(arrayStack_is_empty(parrStack))
{
return -1;
}
data = parrStack->array[parrStack->pos];
parrStack->pos--;

return data;
}
/*入栈*/
int arrayStack_push(stArrayStack *parrStack,int data)
{
if(arrayStack_is_full(parrStack))
{
return -1;
}

parrStack->pos++;
parrStack->array[parrStack->pos] = data;

return 0;
}

int arrayStack_push_new(stArrayStack*parrStack,int data)
{
int *ptmp = NULL;

/*如果栈不满,直接插入*/
if(!arrayStack_is_full(parrStack))
{
return arrayStack_push(parrStack,data);
}

/*如果栈已经满,申请内存*/
ptmp = (int *)malloc(2*parrStack->size*sizeof(int));
if (ptmp == NULL)
{
return -1;
}

memcpy(ptmp,parrStack->array,parrStack->size*sizeof(int));

free(parrStack->array);

parrStack->array = ptmp;
parrStack->size = 2*parrStack->size;
parrStack->pos++;
parrStack->array[parrStack->pos] = data;

return ;
}

void arrayStack_dump(stArrayStack *parrStack)
{
int i = 0;

if (arrayStack_is_empty(parrStack))
{
printf("\r\n arrayStack is empty.");
return;
}
printf("\r\narrayStack size = %d,pos= %d,",
parrStack->size,parrStack->pos);
for(i = 0; i <= parrStack->pos; i++)
{
printf("\r\narry[%d] = %d",i,parrStack->array[i]);
}
}

int main()
{
int i = 0;
int ret = 0;
stArrayStack * parrStack = NULL;

printf("\r\n create size = 4 arrayStack.");

parrStack = arrayStack_create(4);
if (parrStack == NULL)
{
printf("\r\n create size = 4 arrayStack faided.");
return 0;
}

for (i = 0; i < 5; i++)
{
ret = arrayStack_push(parrStack,i);
if(ret != 0)
{
printf("\r\n push size = %d arrayStack faided.",i);

}
}
arrayStack_dump(parrStack);

ret = arrayStack_push_new(parrStack,4);
if(ret != 0)
{
printf("\r\n push size = %d arrayStack faided.",4);
}
arrayStack_dump(parrStack);

arrayStack_destory(parrStack);

return;
}
23 changes: 23 additions & 0 deletions c-cpp/08_stack/arrayStack/arrayStack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*************************************************************************
> File Name: arrayStack.h
> Author: jinshaohui
> Mail: [email protected]
> Time: 18-10-12
> Desc:
************************************************************************/

#ifndef ARRAY_STACJ_H
#define ARRAY_STACJ_H

typedef struct _array_stack
{
int size;/*栈的大小*/
int pos;/*当前存储元素的个数,即栈顶元素下表*/
int *array;/*数据存储区*/
}stArrayStack;

#define arrayStack_size(arrayStack) (arrayStack->size)
#define arrayStack_is_empty(arrayStack) (arrayStack->pos == -1)
#define arrayStack_is_full(arrayStack) (arrayStack->pos == (arrayStack->size-1))

#endif
Binary file added c-cpp/08_stack/arrayStack/arrayStack.h.gch
Binary file not shown.

0 comments on commit ebf7280

Please sign in to comment.