Implemention of data structure and algorithm in c language.?
See Github for the complete project:[Data_Structre_and_Algorithm]
array_stack.h:
/*array_stack.h:This is an implemention of array_stack in C language.
*Characteristic:
* 1.Give an initial length when defining an array_stack
* 2.Support for dynamic expansion
* 3.Support for push and pop operations
*author:stdcoutzrh
*emai:stdcoutzrh@gmail.com
*/
#ifndef ARRAY_STACK_H
#define ARRAY_STACK_H
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
typedef struct array_stack
{
int length;
int size;
int* arr;
}array_stack;
array_stack* as_createStack(int length);
void as_destoryStack(array_stack* array_stack);
bool as_isEmpty(array_stack*array_stack);
bool as_isFull(array_stack*array_stack);
int as_getSize(array_stack*array_stack);
void as_print(array_stack*array_stack);
int as_push(array_stack*array_stack,int element);//if stack is full ,push failed.
int as_pushAppend(array_stack*array_stack,int element); //if stack is full,stack will expand 2 times as before.
int as_pop(array_stack*array_stack);
#endif //ARRAY_STACK_H
array_stack.c:
#include"array_stack.h"
array_stack* as_createStack(int length)
{
array_stack* stack = (array_stack*)malloc(sizeof(array_stack));
if(!stack)
{
perror("malloc:");
exit(-1);
}
stack->length = length;
stack->size = 0;
stack->arr = (int*)malloc(sizeof(int)*length);
if(!stack->arr)
{
perror("malloc:");
exit(-1);
}
printf("current stack size:%d.\n",stack->size);
printf("current stack length:%d.\n",stack->length);
return stack;
}
void as_destoryStack(array_stack* array_stack)
{
if (array_stack->arr != NULL)
{
free(array_stack->arr);
}
free(array_stack);
}
bool as_isEmpty(array_stack*array_stack)
{
return array_stack->size == 0;
}
bool as_isFull(array_stack*array_stack)
{
return array_stack->size == array_stack->length;
}
int as_getSize(array_stack*array_stack)
{
return array_stack->size;
}
void as_print(array_stack*array_stack)
{
if(as_isEmpty(array_stack))
{
printf("no elements in stack\n");
}
else
{
for(int i=0;i<array_stack->size;++i)
printf("stack[%d]:%d,\n",i,array_stack->arr[i]);
printf("current stack size:%d.\n",array_stack->size);
printf("current stack length:%d.\n",array_stack->length);
}
printf("\n");
}
int as_push(array_stack*array_stack,int element)
{
if(as_isFull(array_stack))
{
printf("push failed,stack is full.\n");
return -1;
}
else
{
array_stack->arr[array_stack->size] = element;
++array_stack->size;
return element;
}
}
int as_pushAppend(array_stack*array_stack,int element)
{
if(as_isFull(array_stack))
{
int *p = array_stack->arr;
printf("stack is full,expand stack now!.\n");
array_stack->length *=2;
array_stack->arr = (int*)malloc(sizeof(int)*array_stack->length);
if(!array_stack->arr)
{
perror("malloc:");
exit(-1);
}
memmove(array_stack->arr,p,array_stack->size*sizeof(int));
array_stack->arr[array_stack->size] = element;
++array_stack->size;
free(p);
return element;
}
else
{
array_stack->arr[array_stack->size] = element;
++array_stack->size;
return element;
}
}
int as_pop(array_stack*array_stack)
{
int value;
if(as_isEmpty(array_stack))
{
printf("pop failed,stack is empty.\n");
return -1;
}
else
{
value = array_stack->arr[array_stack->size-1];
--array_stack->size;
printf("pop value:%d\n",value);
return value;
}
}
//test code
int main(void)
{
array_stack* my_stack = as_createStack(5);
for(int i=0;i<10;i++)
{
as_pushAppend(my_stack,i);
}
as_print(my_stack);
as_pop(my_stack);
as_print(my_stack);
return 0;
}