栈练习,后进先出,c实现,做了一些封装。
"stack.h"
#ifndef _STACK_H_
#define _STACK_H_
typedef struct StackNodeTag{
void *data;
struct StackNodeTag *next;
}StackNode, *pStackNode;
typedef void (*showDataFun)(void *data);
typedef struct StackTag{
//public
/*0: success ;
-1: node data size is mismatch
-2: stack is full
-3: memory malloc error*/
int (*push)(struct StackTag*, void*, size_t);
/*0: success
-1: stack is empty
*/
int (*pop)(struct StackTag*);
/*
Get but not delete the node
*/
int (*getTop)(struct StackTag*, void*, size_t);
/*1: full ; 0: NOT full*/
int (*isFull)(struct StackTag*);
/*1: empty; 0: NOT empty*/
int (*isEmpty)(struct StackTag*);
/*Show the stack*/
void (*show)(struct StackTag*);
int (*getSize)(struct StackTag*);
//private data, DO NOT modify them directly
pStackNode top;
int cursize; //the current size of the stack
int maxsize; //the max size of the stack
int datasize; //the size of node data
showDataFun showdata;
}Stack, *pStack;
pStack StackCreate(int stacksize, int datasize, showDataFun show);
int StackDelete(pStack);
#endif /*_STACK_H_*/
"stack.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "stack.h"
static int Stk_IsFull(pStack s)
{
assert(s != NULL);
return (s->cursize == s->maxsize);
}
static int Stk_IsEmpty(pStack s)
{
assert(s != NULL);
return (s->cursize == 0);
}
static int Stk_Push(pStack s, void *data, size_t sz)
{
assert(s != NULL);
assert(data != NULL);
if(sz != s->datasize)
return -1;
if(s->isFull(s))
return -2;
//new a node
pStackNode n = (pStackNode)malloc(sizeof(StackNode));
if(n == NULL){
perror("Push malloc\n");
return -3;
}
n->data = malloc(s->datasize);
if(n->data == NULL){
perror("data malloc\n");
free(n);
return -3;
}
memcpy(n->data, data, sz);
n->next = NULL;
//push it
if(s->top == NULL){
s->top = n;
s->cursize++;
}else{
n->next = s->top;
s->top = n;
s->cursize++;
}
return 0;
}
static int Stk_Pop(pStack s)
{
assert(s != NULL);
if(s->isEmpty(s))
return -1;
pStackNode tmp = s->top;
s->top = s->top->next;
s->cursize--;
//free the Node;
free(tmp->data);
free(tmp);
return 0;
}
static int Stk_GetTop(pStack s, void *data, size_t sz)
{
assert(s != NULL);
assert(data != NULL);
if(sz != s->datasize)
return -1;
if(s->isEmpty(s))
return -2;
memcpy(data, s->top->data, s->datasize);
return 0;
}
static void Stk_Show(pStack s)
{
assert(s != NULL);
pStackNode tmp;
printf("stack max size: %d, current size: %d\n", s->maxsize, s->cursize);
if(s->showdata != NULL){
tmp = s->top;
while(tmp != NULL){
s->showdata(tmp->data);
tmp = tmp->next;
}
}
printf("\n");
}
static int Stk_GetSize(pStack s)
{
return s->cursize;
}
pStack StackCreate(int stacksize, int datasize, showDataFun show)
{
assert(stacksize > 0);
assert(datasize > 0);
pStack this = (pStack)malloc(sizeof(Stack));
if(this == NULL){
perror("StackCreate, malloc\n");
return NULL;
}
//data
this->top = NULL;
this->cursize = 0;
this->maxsize = stacksize;
this->datasize = datasize;
this->showdata = show;
//operations
this->isEmpty = Stk_IsEmpty;
this->isFull = Stk_IsFull;
this->pop = Stk_Pop;
this->push = Stk_Push;
this->getTop = Stk_GetTop;
this->show = Stk_Show;
this->getSize = Stk_GetSize;
return this;
}
int StackDelete(pStack s)
{
assert(s != NULL);
while( !s->isEmpty(s) )
s->pop(s);
free(s); s = NULL;
return 0;
}
"main.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
typedef struct StudentTag{
char name[16];
int age;
int num;
}Student;
static Student gStu[] = {
{"Ray", 10, 1},
{"Sim", 11, 2},
{"Jack", 12, 3},
{"Rose", 13, 4},
{"Tim", 14, 5},
};
static void showStudent(void *data)
{
if(data == NULL)
return;
Student *stu = (Student *)data;
printf("study num: %d, name:%s, age:%d\n", stu->num, stu->name, stu->age);
}
int test()
{
pStack s = StackCreate(10, sizeof(Student), showStudent);
if(s == NULL){
exit(-1);
}
s->push(s, &gStu[0], sizeof(Student));
s->push(s, &gStu[2], sizeof(Student));
s->push(s, &gStu[1], sizeof(Student));
s->push(s, &gStu[3], sizeof(Student));
s->show(s);
s->pop(s);
s->pop(s);
s->show(s);
Student stu;
memset(&stu, 0, sizeof(Student));
s->getTop(s, &stu, sizeof(Student));
StackDelete(s);
return 0;
}
void showChar(void *data)
{
if(data == NULL)
return;
char *c = (char *)data;
printf("%c\n", *c);
}
int main()
{
char str[] = "{[(5+6)/(6+7)] * [(3+5)*(8+0)]}";
char *p = str;
pStack s = StackCreate(64, sizeof(char), showChar);
if(s == NULL)
exit(-1);
while( *p != '\0'){
switch(*p)
{
case '{':
case '[':
case '(':
s->push(s, p, 1);
s->show(s);
break;
case '}':
case ']':
case ')':
if(!s->isEmpty(s)){
s->pop(s);
s->show(s);
}else{
printf("%s - NOT Symmetrical\n", str);
goto out;
}
break;
default:
break;
}
p++;
}
if(s->isEmpty(s)){
printf("%s - Symmetrical\n", str);
}else{
printf("%s - NOT Symmetrical\n", str);
}
out:
StackDelete(s);
return 0;
}