栈的基本操作的相关代码(C语言版)

虽然我的代码是用C语言写的,但是你们建立文件的时候记得创建C++文件哦!

废话不多说,直接上代码:

首先我们先建立起我们自己的头文件,并且将函数的声明放入头文件(在此我的头文件名称是:”Seqstack.h”)中(建立头文件的相关操作可参考我上一期文章)

#ifndef SEQSTACK_H_INCLUDED
#define SEQSTACK_H_INCLUDED

#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#define INIT_STACK_SIZE 10

typedef int Elemtype ;
typedef struct Seqstack
{
    int capacity;
    Elemtype*base;
    Elemtype top;
}Seqstack;
void Initstack(Seqstack*s);

bool isFull(Seqstack*s);
bool isEmpty(Seqstack*s);
bool Gettop(Seqstack*s,Elemtype *p);

void push(Seqstack*s,Elemtype x);
void pop(Seqstack*s);
void show_stack(Seqstack*s);

int length(Seqstack*s);

void clear(Seqstack*s);
void destroy(Seqstack*s);
#endif // SEQSTACK_H_INCLUDED

接下来就是相关函数的定义,我们将它放在c++源文件中:

#include"Seqstack.h"  //引入头文件
void Initstack(Seqstack*s)
{
    s->base = (Elemtype*)malloc(sizeof(Elemtype)*INIT_STACK_SIZE);
    assert(s->base != NULL);
    s->capacity =INIT_STACK_SIZE;
    s->top=0;
}
bool isFull(Seqstack*s)
{
    return s->top>=s->capacity;
}
bool isEmpty(Seqstack*s)
{
    return s->top==0;
}
bool Gettop(Seqstack*s,Elemtype *p)
{
    if(isEmpty(s))
    {
        printf("栈以为空,无法取栈顶的元素\n");
        return false;
    }
    *p=s->base[s->top-1];
    return true;
}
void push(Seqstack*s,Elemtype x)
{
    if(isFull(s))
    {
        printf("该栈已满,%d无法入栈\n",x);
        return;
    }
    s->base[s->top++]=x;
}
void show_stack(Seqstack*s)
{
    for(int i=s->top-1; i>=0; i--)
    {
        printf("%d\n",s->base[i]);
    }
    printf("\n");
}
void pop(Seqstack*s)
{
    if(isEmpty(s))
    {
        printf("该栈已空,不能出栈.\n");
        return;
    }
    s->top--;
}
int length(Seqstack*s)
{
    return s->top;
}
void clear(Seqstack*s)
{
    s->top=0;
}
void destroy(Seqstack*s)
{
    free(s->base);
    s->base=NULL;
    s->top=0;
    s->capacity=0;
}

最后我们接着在main函数中编译对函数的调用代码:

#include"Seqstack.h"
int main()
{
    int select=1;
    Seqstack st;
    Initstack(&st);
    Elemtype n;
    printf("-------------------------------------------------------\n");
    printf("# 1 push                      2 show                  #\n");
    printf("# 3 pop                       4 Gettop                #\n");
    printf("# 5 length                    6 clear                 #\n");
    printf("# 7 deatroy                                           #\n");
    printf("-------------------------------------------------------\n");
    while(1)
    {
        printf("请输入你要进行的操作(-1)结束:\n");
        scanf("%d",&select);
        printf("\n");
        switch(select)
        {
        case 1:
            for (int i=1; i<=5; i++)
            {
                push(&st,i);
            }
            break;
        case 2:
            printf("该栈的元素为;\n");
            show_stack(&st);
            break;
        case 3:
            pop(&st);
            break;
        case 4:
            Gettop(&st,&n);
            printf("%d出栈\n",n);
            break;
        case 5:
            printf("该栈的长度为%d\n",length(&st));
            break;
        case 6:
            clear(&st);
            break;
        case 7:
            destroy(&st);
            break;
        default:
            printf("输入操作有误,请重新输入:\n");
            break;
        }
    }
    return 0;
}

有些细节方面可能没有到位,希望大家大胆指出,毕竟我也是CS大一,谢谢大家支持!
 

猜你喜欢

转载自blog.csdn.net/weixin_46713492/article/details/123590322
今日推荐