标题:发一个stack头文件,自己写的,求意见
只看楼主
vinaleth
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-2-24
 问题点数:0 回复次数:2 
发一个stack头文件,自己写的,求意见
#include <assert.h>
#include <stdlib.h>

#ifndef _STACK_DEFINED

typedef struct
{
    int size;
    int max;
    int count;
    unsigned char* data;
    int top;
}stack;

stack init_stack(int size);
void cleanup_stack(stack* s);
void expand_stack(stack* s, int length);
void top_stack(stack s, void* d);
void push_stack(stack* s, void* d);
void pop_stack(stack* s, void* d);
int empty_stack(stack s);

stack init_stack(int size)
{
    stack s;
    s.size=size;
    s.max=0;
    s.count=0;
    s.data=0;
    s.top=-1;
    return s;
}

void cleanup_stack(stack* s)
{
    s->max=0;
    s->count=0;
    s->data=0;
    s->top=-1;
}

void expand_stack(stack* s, int length)
{
    void* v=realloc(s->data,(s->max+length)*s->size);
    assert(v);
    s->data=v;
    s->max+=length;
}

void top_stack(stack s, void* d)
{
    if(empty_stack(s))
        exit(0);
    memcpy(d,&(s.data[s.top*s.size]),s.size);
}

void push_stack(stack* s, void* d)
{
    if(s->count==s->max)
        expand_stack(s,10);
    memcpy(&(s->data[(s->top+1)*s->size]),d,s->size);
    s->count++;
    s->top++;
}

void pop_stack(stack* s, void* d)
{
    memcpy(d,&(s->data[s->top*s->size]),s->size);
    s->data[s->top*s->size]=0;
    s->top--;
    s->count--;
}

int empty_stack(stack s)
{
    return s.top==-1;
}

#define _STACK_DEFINED
#endif
stack.rar (871 Bytes)
搜索更多相关主题的帖子: include count 
2011-02-25 21:43
zhanhb
Rank: 2
等 级:论坛游民
帖 子:29
专家分:70
注 册:2010-12-21
得分:0 
用status代替void。。。
typedef int status;
2011-02-27 07:30
犬虫门心
Rank: 8Rank: 8
来 自:西安
等 级:蝙蝠侠
帖 子:209
专家分:753
注 册:2011-1-25
得分:0 
我给一个:
#ifndef _STACK_
#define _STACK_

#include<stdio.h>
#include<malloc.h>

#define TRUE 1
#define FALSE 0

typedef int MYTYPE;

typedef struct STACK
{
    MYTYPE *pd;
    int MaxSize;
    int Top;
}STACK;

STACK InitStack(int MaxSize);
void DestroyStack(STACK *t);
int isStackFull(STACK t);
int isStackEmpty(STACK t);
int Push(STACK *st, MYTYPE data);
int Pop(STACK *st, MYTYPE *data);
int ReadStackTop(STACK st, MYTYPE *data);

int ReadStackTop(STACK st, MYTYPE *data)
{
    int isEmpty;

    isEmpty = isStackEmpty(st);
    if(!isEmpty)
        *data = st.pd[st.Top - 1];

    return !isEmpty;
}

int Pop(STACK *st, MYTYPE *data)
{
    int isEmpty;

    isEmpty = isStackEmpty(*st);
    if(!isEmpty)
        *data = st->pd[--st->Top];

    return !isEmpty;
}

int Push(STACK *st, MYTYPE data)
{
    int isFull;

    isFull = isStackFull(*st);
    if(!isFull)
    {
        st->pd[st->Top++] = data;
    }

    return !isFull;
}

int isStackEmpty(STACK t)
{
    return t.Top <= 0;
}

int isStackFull(STACK t)
{
    return t.Top >= t.MaxSize;
}

void DestroyStack(STACK *t)
{
    free(t->pd);
    t->MaxSize = t->Top = 0;
    t->pd = NULL;
}

STACK InitStack(int MaxSize)
{
    STACK t;

    t.MaxSize = MaxSize;
    t.pd = (MYTYPE *)malloc(sizeof(MYTYPE) * MaxSize);
    t.Top = 0;

    return t;
}

#endif

当一名对得起学生学费的老师,一直是我的目标!我会更努力的!
2011-02-27 12:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-332783-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.139899 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved