线性表-栈-顺序存储
sqstack.h
typedef int data_type;
typedef struct {
data_type * data;
int maxlen;
int top;
}sqstack;
sqstack * stack_create(int len);
int stack_push(sqstack * s, data_type value);
int stack_empty(sqstack * s);
int stack_full(sqstack * s);
data_type stack_pop(sqstack * s);
data_type stack_top(sqstack * s);
int stack_clear(sqstack * s);
int stack_show(sqstack * s);
int stack_free(sqstack *s);
sqstack.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqstack.h"
sqstack * stack_create(int len){
sqstack * s;
s = (sqstack *)malloc(sizeof(sqstack));
if(s==NULL){
printf("%s\n", "malloc sqstack failed\n");
return NULL;
}
data_type * p;
p = (data_type *)malloc(len * sizeof(data_type));
if(p==NULL){
printf("%s\n", "malloc data failed\n");
// 数组内存申请失败,则栈内存也应该释放
free(s);
return NULL;
}
memset(p, 0, len *sizeof(data_type));
s->data = p;
s->maxlen = len;
s->top = -1;
return s;
}
int stack_push(sqstack * s, data_type value){
if(s==NULL){
printf("stack is NULL\n");
return -1;
}
if(s->top==s->maxlen-1){
printf("stack is full\n");
return -1;
}
s->top++;
s->data[s->top] = value;
return 0;
}
int stack_empty(sqstack * s){
if(s==NULL){
printf("stack is NULL\n");
return -1;
}
// 三目运算符
return (s->top == -1 ? 1 : 0);
}
int stack_full(sqstack * s){
if(s==NULL){
printf("stack is NULL\n");
return -1;
}
return (s->top == s->maxlen-1 ? 1 : 0);
}
data_type stack_pop(sqstack * s){
s->top--;
return s->data[s->top+1];
}
data_type stack_top(sqstack * s){
return s->data[s->top];
}
int stack_clear(sqstack * s){
s->top = -1;
return 0;
}
int stack_free(sqstack * s){
if(s==NULL){
printf("stack is NULL\n");
return -1;
}
if(s->data != NULL){
free(s->data);
}
free(s);
return 0;
}
int stack_show(sqstack * s){
if(s==NULL){
printf("stack is NULL\n");
return -1;
}
int i;
for (i = 0; i <= s->top; i++){
printf("%d\n", s->data[i]);
}
return 0;
}
test.c
#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"
int main(int argc, char const *argv[]){
sqstack * s;
s = stack_create(10);
stack_push(s, 10);
stack_push(s, 20);
stack_push(s, 30);
stack_show(s);
printf("top: %d\n",stack_top(s));
while(!stack_empty(s)){
printf("pop: %d\n", stack_pop(s));
}
return 0;
}
线性表-栈-链式存储
linkstack.h
typedef int data_type;
typedef struct node{
data_type data;
struct node * next;
}linkstack;
linkstack * stack_create();
int stack_push(linkstack * s, data_type value);
int stack_empty(linkstack * s);
data_type stack_pop(linkstack * s);
data_type stack_top(linkstack * s);
int stack_show(linkstack * s);
int stack_free(linkstack *s);
linkstack.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkstack.h"
linkstack * stack_create(){
linkstack * s;
s = (linkstack *)malloc(sizeof(linkstack));
if(s==NULL){
printf("%s\n", "malloc failed\n");
return NULL;
}
s->data = 0;
s->next = NULL;
return s;
}
int stack_push(linkstack * s, data_type value){
if(s==NULL){
printf("%s\n", "linkstack is NULL\n");
return -1;
}
linkstack * p;
p = (linkstack *)malloc(sizeof(linkstack));
if(p==NULL){
printf("%s\n", "malloc failed\n");
return -1;
}
p->data = value;
// p->next = NULL;
p->next = s->next;
s->next = p;
return 0;
}
int stack_empty(linkstack * s){
if(s==NULL){
printf("%s\n", "linkstack is NULL\n");
return -1;
}
return (s->next == NULL ? 1 : 0);
}
data_type stack_pop(linkstack * s){
if(s==NULL){
printf("%s\n", "linkstack is NULL\n");
return -1;
}
linkstack * p;
p = s->next;
data_type data;
data = p->data;
s->next = p->next;
free(p);
p = NULL;
return data;
}
data_type stack_top(linkstack * s){
if(s==NULL){
printf("%s\n", "linkstack is NULL\n");
return -1;
}
return s->next->data;
}
int stack_show(linkstack * s){
if(s == NULL){
printf("%s\n", "linkstack is null");
return -1;
}
linkstack * p;
p = s;
while(p->next != NULL){
printf("%d\n", p->next->data);
p = p->next;
}
puts("");
return 0;
}
int stack_free(linkstack *s){
if(s==NULL){
printf("%s\n", "linkstack is NULL\n");
return -1;
}
linkstack *p;
while(s!=NULL){
p = s;
s = s->next;
printf("free: %d\n", p->data);
free(p);
p = NULL;
}
return 0;
}
test.c
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
int main(int argc, char const *argv[]){
linkstack * s;
s = stack_create();
stack_push(s, 10);
stack_push(s, 20);
stack_push(s, 30);
stack_show(s);
printf("top: %d\n",stack_top(s));
while(!stack_empty(s)){
printf("pop: %d\n", stack_pop(s));
}
stack_free(s);
return 0;
}