时光季 发表于 2014-11-9 19:00:37

c语言实现栈的基本操作

C语言如何实现栈的基本操作、求源代码。

风之残月 发表于 2014-11-10 21:35:44

翻书吧,书上都有

shisecao 发表于 2014-11-14 22:30:11

好好学习,天天向上

三哥自尊心强 发表于 2014-11-21 14:41:25

这个应该很多的。栈的基本操作的东西网上比较多。

tlwangxd 发表于 2014-12-10 09:17:31

关注

赵晓玉 发表于 2014-12-14 22:33:21

看小甲鱼的视频有讲的 上机打出的代码

BeatificDevin 发表于 2014-12-15 09:17:39

typedef struct sqStack
{
    ElemType *top;
    ElemType *base;
    int initSize;
}sqStack;
typedef struct sqStack *LinkStack;

//初始化
void InitStack(sqStack *s)
{
    s->base = (LinkStack)malloc(Stack_Init_Size * sizeof(ElemType));
    if(!s->base)
    {
      printf("存储空间分配失败······\n");
      return;
    }
    s->top = s->base;
    s->initSize = Stack_Init_Size;
}
//进栈
void Push(sqStack *s,ElemType *e)
{
    if(s->top - s->base >= s->initSize - 1)
    {
      s->base = (LinkStack)realloc(s->base,(s->initSize + Increase_Size) * sizeof(ElemType));
      //第一个s->base是增加后的存储空间块的地址,第二个是增加空间之前的存储空间块地址,后面是增加过的存储空间块的大小
      if(!s->base)
      {
            printf("增加存储空间失败······\n");
            return;
      }
      s->initSize = Increase_Size + Stack_Init_Size;
    }
    *(s->top) = *e;
    (s->top)++;
}
//出栈
void Pop(sqStack *s,ElemType *e)
{
    if(s->top == s->base)
    {
      printf("栈已空,无法进行出栈操作······\n");
      return;
    }
    s->top--;
    *e = *s->top;
}
//求栈的长度
int StackLen(sqStack s)
{
    return (s.top - s.base);
}

BeatificDevin 发表于 2014-12-15 09:18:37

http://www.cnblogs.com/devinblog/
最近在博客上也写了一点程序,你可一看看,希望对你有帮助

赵晓玉 发表于 2014-12-15 22:41:26

小甲鱼有视频的自己去看下

xueying 发表于 2014-12-29 19:53:26

mark

xueying 发表于 2014-12-31 10:24:46

mark

xueying 发表于 2015-1-8 20:33:21

//2014.10.02        顺序栈的实现方式
#include<iostream>
#include<math.h>
#include"assert.h"
using namespace std;

#define INCREMENT_SIZE 2
typedef struct STACK
{
        int stacksize;
        int* pstop;                        //栈顶指针p表示指针s表示栈
        int* psbottom;
}STACK;

void init_stack(STACK& s)
{
       
        s.stacksize = INCREMENT_SIZE;
        //注意加1的原因:s.psbottom不存放数据本身要占据一个字节的空间
        s.pstop = s.psbottom =(int*)malloc(sizeof(int)*(s.stacksize+1));
        if(NULL ==         s.pstop)
        {
                cout<<"为栈分配存储空间失败";
                return;
        }
       
}



bool push(STACK& s,int val)
{
       
        int k = s.pstop-s.psbottom;
        if((s.pstop-s.psbottom)>=s.stacksize)
        {
                //注意加1的原因:s.psbottom不存放数据本身要占据一个字节的空间
                s.psbottom = (int*)realloc(s.psbottom ,sizeof(int)*(s.stacksize+INCREMENT_SIZE+1));
                if(NULL == s.psbottom)
                {
                        cout<<"栈已满,为将要压栈的元素分配内存失败"<<endl;
                        return false;
                }
                s.pstop = s.psbottom+s.stacksize;
                s.stacksize+=INCREMENT_SIZE;
        }
                s.pstop++;
                *s.pstop = val;
       
                return true;
}

bool pop(STACK& s,int& val)
{
        if(s.psbottom == s.pstop)
        {
                cout<<"栈为空出栈失败"<<endl;
                return false;
        }

        val = *s.pstop--;
        return true;
}
void dis_stack(STACK& s)
{
        if(s.pstop == s.psbottom)
        {
                cout<<"栈为空"<<endl;
                return ;
        }
       
        int* p = s.pstop ;
        while(p != s.psbottom)
        {
                cout<<*p<<endl;
                p--;
        }
}


bool get_top(STACK&s, int& val)
{
        if(s.psbottom == s.pstop)
        {
                cout<<"栈为空"<<endl;
                return false;
        }

        val = *s.pstop;
        return true;
}

STACK stack;
int main(void)
{
        init_stack(stack);
        push(stack,23);
        push(stack,78);
        push(stack,26);
        push(stack,90);
        push(stack,45);       
        push(stack,45);
        push(stack,78);
        push(stack,123);       
        dis_stack(stack);
        int val = 0;
        cout<<endl;
        pop(stack,val);
        cout<<val<<endl;
        cout<<endl;
        pop(stack,val);
        cout<<val<<endl;
        cout<<endl;
        dis_stack(stack);

        int top_val;
        get_top(stack,top_val);
        cout<<endl;
        cout<<top_val<<endl;
    return 0;
}
页: [1]
查看完整版本: c语言实现栈的基本操作