Implemention of data structure and algorithm in c language.?
See Github for the complete project:[Data_Structre_and_Algorithm]
linked_list_stack.h:
/*linked_list_stack.h:This is an implemention of linked_list_stack in C language.
*Characteristic:
* 1.Give an base pointer and top pointer when defining an linked_list_stack.
* 2.Support for push and pop operations.
* 3.no size limits.
*author:stdcoutzrh
*emai:stdcoutzrh@gmail.com
*/
#ifndef LINKED_LIST_STACK_H
#define LINKED_LIST_STACK_H
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
typedef struct node
{
int data;
struct node*next;
}node;
typedef struct linked_list_stack
{
int size;
struct node* base;
struct node* top;
}linked_list_stack;
linked_list_stack* ll_createStack(void);
void ll_destoryStack(linked_list_stack* linked_list_stack);
bool ll_isEmpty(linked_list_stack*linked_list_stack);
bool ll_isFull(linked_list_stack*linked_list_stack);
int ll_getSize(linked_list_stack*linked_list_stack);
void ll_print(linked_list_stack*linked_list_stack);
int ll_push(linked_list_stack*linked_list_stack,int element);
int ll_pop(linked_list_stack*linked_list_stack);
#endif //LINKED_LIST_STACK_H
linked_list_stack.c:
#include"linked_list_stack.h"
linked_list_stack* ll_createStack(void)
{
linked_list_stack* stack = (linked_list_stack*)malloc(sizeof(linked_list_stack));
if(!stack)
{
perror("malloc:");
exit(-1);
}
stack->size = 0;
stack->base = stack->top = NULL;
return stack;
}
void ll_destoryStack(linked_list_stack* linked_list_stack)
{
if(linked_list_stack->top!=NULL)
free(linked_list_stack->top);
if(linked_list_stack->base!=NULL)
free(linked_list_stack->base);
free(linked_list_stack);
}
bool ll_isEmpty(linked_list_stack*linked_list_stack)
{
return linked_list_stack->size == 0;
}
int ll_getSize(linked_list_stack*linked_list_stack)
{
return linked_list_stack->size;
}
void ll_print(linked_list_stack*linked_list_stack)
{
if(ll_isEmpty(linked_list_stack))
printf("no elements in stack!\n");
else
{
node*p=linked_list_stack->base;
for(int i=0;i<linked_list_stack->size;++i)
{
printf("stack[%d]:%d.\n",i,p->data);
p=p->next;
}
printf("current size : %d.\n",linked_list_stack->size);
}
}
int ll_push(linked_list_stack*linked_list_stack,int element)
{
node* node =(struct node*)malloc(sizeof(node));
if(!node)
{
perror("malloc:");
exit(-1);
}
node->data = element;
node->next = NULL;
if(ll_isEmpty(linked_list_stack))
{
linked_list_stack->top = linked_list_stack->base =node;
++linked_list_stack->size;
return element;
}
else
{
linked_list_stack->top->next=node;
linked_list_stack->top = node;
++linked_list_stack->size;
return element;
}
}
int ll_pop(linked_list_stack*linked_list_stack)
{
if(ll_isEmpty(linked_list_stack))
{
printf("pop failed! no elements in stack!\n");
return -1;
}
int value =linked_list_stack->top->data,i=2;
node*p = linked_list_stack->base;
node*q = linked_list_stack->top;
while(i<linked_list_stack->size)
{
p=p->next;
++i;
}
linked_list_stack->top = p;
free(q);
--linked_list_stack->size;
printf("pop value:%d.\n",value);
return value;
}
//test code
int main(int argc,char**argv)
{
linked_list_stack* my_stack = ll_createStack();
for(int i =0;i<10;++i)
ll_push(my_stack,i);
ll_print(my_stack);
ll_pop(my_stack);
ll_pop(my_stack);
ll_pop(my_stack);
ll_print(my_stack);
return 0;
}