sqstack
sqstack.h
typedef int data_t;
typedef struct
{
data_t *data;
int maxlen;
int top;
} sqstack;
sqstack *stack_create(int len);
int stack_push(sqstack *s, data_t value);
int stack_empty(sqstack *s);
int stack_full(sqstack *s);
data_t stack_pop(sqstack *s);
data_t stack_top(sqstack *s);
int stack_clear(sqstack *s);
int statck_free(sqstack *s);
sqstack.c
#include <stdio.h>
#include "sqstack.h"
#include <stdlib.h>
#include <string.h>
sqstack *stack_create(int len)
{
sqstack *s;
if ((s = (sqstack *)malloc(sizeof(sqstack))) == NULL)
{
printf("malloc sqstack failed\n");
return NULL;
}
if ((s->data = (data_t *)malloc(len * sizeof(data_t))) == NULL)
{
printf("malloc data failed\n");
free(s);
return NULL;
}
memset(s->data, 0, len * sizeof(data_t));
s->maxlen = len;
s->top = -1;
return s;
}
int stack_push(sqstack *s, data_t value)
{
if (NULL == s)
{
printf("sqstack is null\n");
return -1;
}
if (s->top == s->maxlen - 1)
{
printf("stack is null\n");
return -1;
}
s->data[++s->top] = value;
return 0;
}
int stack_empty(sqstack *s)
{
if (NULL == s)
{
printf("sqstack is null\n");
return -1;
}
return (s->top == -1 ? 1 : 0);
}
int stack_full(sqstack *s)
{
if (NULL == s)
{
printf("sqstack is null\n");
return -1;
}
return s->top == s->maxlen - 1 ? 1 : 0;
}
data_t stack_pop(sqstack *s)
{
if (NULL == s)
{
printf("sqstack is null\n");
}
s->top--;
return s->data[s->top + 1];
}
data_t stack_top(sqstack *s)
{
if (NULL == s)
{
printf("sqstack is null\n");
}
return s->data[s->top];
}
int stack_clear(sqstack *s)
{
if (NULL == s)
{
printf("sqstack is null\n");
return -1;
}
s->top = -1;
return 0;
}
int statck_free(sqstack *s)
{
printf("stack free ...\n");
if (NULL == s)
{
printf("sqstack is null\n");
return -1;
}
if (NULL != s->data)
free(s->data);
free(s);
return 0;
}
demo.c
#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"
void test_create();
int main()
{
test_create();
return 0;
}
void test_create()
{
sqstack *s;
s = stack_create(100);
if (NULL == s)
{
return -1;
}
stack_push(s, 10);
stack_push(s, 20);
stack_push(s, 30);
stack_push(s, 40);
stack_push(s, 50);
// printf("%d",stack_empty(s));
while (!stack_empty(s))
{
printf("pop: %d\n", stack_pop(s));
}
statck_free(s);
}
linkstack
linkstack.h
typedef int data_t;
typedef struct node
{
data_t data;
struct node *next;
} listnode, *linkstack;
linkstack stack_create();
int stack_push(linkstack s, data_t value);
int stack_empty(linkstack s);
data_t stack_pop(linkstack s);
data_t stack_top(linkstack s);
linkstack statck_free(linkstack s);
linkstack.c
#include <stdio.h>
#include "linkstack.h"
#include <stdlib.h>
#include <string.h>
linkstack stack_create()
{
linkstack s;
s = (linkstack)malloc(sizeof(listnode));
if (NULL == s)
{
printf("malloc failed\n");
return NULL;
}
s->data = -1;
s->next = NULL;
return s;
}
int stack_push(linkstack s, data_t value)
{
linkstack p;
if (NULL == s)
{
printf("s is NULL\n");
return -1;
}
p = (linkstack)malloc(sizeof(listnode));
if (NULL == p)
{
printf("malloc failed\n");
return -1;
}
p->data = value;
p->next = s->next;
s->next = p;
return 0;
}
int stack_empty(linkstack s)
{
if (NULL == s)
{
printf("s is NULL\n");
return -1;
}
return s->next == NULL ? 1 : 0;
}
data_t stack_pop(linkstack s)
{
if (NULL == s)
{
printf("s is NULL\n");
return -1;
}
linkstack p;
data_t t;
p = s->next;
s->next = p->next;
t = p->data;
free(p);
p = NULL;
return t;
}
data_t stack_top(linkstack s)
{
if (NULL == s)
{
printf("s is NULL\n");
return -1;
}
if (NULL == s->next)
{
printf("s is NULL\n");
return -1;
}
return s->next->data;
}
linkstack statck_free(linkstack s)
{
if (NULL == s)
{
printf("s is NULL\n");
return -1;
}
linkstack p;
while (s != NULL)
{
p = s;
s = s->next;
printf("free: %d\n", p->data);
free(p);
}
return NULL;
}
demo.c
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
void test_create();
int main()
{
test_create();
return 0;
}
void test_create()
{
linkstack *s;
s = stack_create();
if (NULL == s)
{
return -1;
}
stack_push(s, 10);
stack_push(s, 20);
stack_push(s, 30);
stack_push(s, 40);
stack_push(s, 50);
// printf("%d",stack_empty(s));
while (!stack_empty(s))
{
printf("pop: %d\n", stack_pop(s));
}
statck_free(s);
}