数据结构 顺序栈

5 篇文章 0 订阅
这段代码展示了如何使用C语言实现顺序栈的基本操作,包括初始化、销毁、入栈、出栈、获取栈顶元素、打印栈内容以及判断栈的空满状态。通过一个结构体定义顺序栈,并提供了相关函数来完成栈的各种操作。在main函数中,进行了入栈、查看栈顶元素和出栈的演示。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define MAXSIZE 10

typedef int ElemType;


typedef struct{
    ElemType data[MAXSIZE];
    int top;//栈顶指针,-1表示空栈,maxsize-1表示满栈
}SeqStack,*PSeqStack;


// 顺序栈SS的初始化操作。
void InitStack(PSeqStack SS);

// 销毁顺序栈SS。
void DestroyStack(PSeqStack SS);

// 元素入栈,返回值:0-失败;1-成功。
int Push(PSeqStack SS, ElemType *ee);

// 元素出栈,返回值:0-失败;1-成功。
int Pop(PSeqStack SS, ElemType *ee);

// 求顺序栈的长度,返回值:栈SS中元素的个数。0空,-1失败
int Length(PSeqStack SS);

// 清空顺序栈。
void Clear(PSeqStack SS);

// 判断顺序栈是否为空,返回值:1-非空,0-空,-1失败。
int IsEmpty(PSeqStack SS);

// 判断顺序栈是否已满,返回值:1-已满,0-未满或失败。
int IsFull(PSeqStack SS);

// 打印顺序栈中全部的元素。
void PrintStack(PSeqStack SS);

// 获取栈顶元素,返回值:0-失败;1-成功。
// 只查看栈顶元素的值,元素不出栈。
int GetTop(PSeqStack SS, ElemType *ee);


int main()
{
    SeqStack SS;
    InitStack(&SS);

  printf("栈的长度是%d\n",Length(&SS));

  ElemType ee;     // 创建一个数据元素。

  printf("元素(1、2、3、4、5、6、7、8、9、10)入栈。\n");
  ee=1;  Push(&SS, &ee);
  ee=2;  Push(&SS, &ee);
  ee=3;  Push(&SS, &ee);
  ee=4;  Push(&SS, &ee);
  ee=5;  Push(&SS, &ee);
  ee=6;  Push(&SS, &ee);
  ee=7;  Push(&SS, &ee);
  ee=8;  Push(&SS, &ee);
  ee=9;  Push(&SS, &ee);
  ee=10; Push(&SS, &ee);

  printf("栈的长度是%d\n",Length(&SS));

  // 只查看栈顶元素的值,元素不出栈。
  if (GetTop(&SS,&ee)==1)  printf("栈顶的元素值为%d\n",ee);

  PrintStack(&SS);

  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);
  if (Pop(&SS,&ee)==1)  printf("出栈的元素值为%d\n",ee);

  return 0;

}

void InitStack(PSeqStack SS)
{
    Clear(SS);
}

void Clear(PSeqStack SS)
{
    if(SS==NULL)
    {
        return;
    }
    SS->top=-1;
    memset(SS->data,0,sizeof(ElemType)*MAXSIZE);
}

int Length(PSeqStack SS)
{
    if(SS==NULL)
    {
        return -1;
    }
    return SS->top+1;

}

void DestroyStack(PSeqStack SS)
{
    Clear(SS);
}

int IsEmpty(PSeqStack SS)
{
    if(SS==NULL)
        return -1;
    if(SS->top==-1)
        return 0;
    else return 1;
}

int IsFull(PSeqStack SS)
{
    if(SS==NULL) return -1;
    if(SS->top==MAXSIZE-1) return 1;
    else return 0;
}

int Push(PSeqStack SS,ElemType *ee)
{
    if(SS==NULL)
        return 0;
    if(IsFull(SS)==1)
        return 0;
    memcpy(&SS->data[SS->top+1],ee,sizeof(ElemType));
    SS->top++;
    return 1;

}

void PrintStack(PSeqStack SS)
{
    if(SS==NULL)
    {
        return;
    }
    if(SS->top==-1)
    {
        printf("栈为空\n");
        return;
    }
    printf("从栈顶到栈底依次为:\n");
    int kk=0;
    for(kk=SS->top;kk>-1;kk--)
    {
        printf("%d\n",SS->data[kk]);
    }
}

int Pop(PSeqStack SS,ElemType *ee)
{
    if(SS==NULL)
        return -1;
    if(SS->top==-1)
    {
        printf("栈为空不能再出栈\n");
        return 0;

    }
    SS->top--;
    memcpy(ee,&SS->data[SS->top+1],sizeof(ElemType));
    return 1;
}

int GetTop(PSeqStack SS, ElemType *ee)
{
  if ( (SS == NULL) || (ee == NULL) ) return 0;  // 检查空指针。

  if (IsEmpty(SS) == 0) { printf("栈为空。\n"); return 0; }

  memcpy(ee,&SS->data[SS->top],sizeof(ElemType));  // 用数组的下标访问。
  // memcpy(ee,SS->data+SS->top,sizeof(ElemType));    // 采用指针运算也可以。

  return 1;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中的数据结构顺序栈是一种使用数组来实现的栈结构。顺序栈的特点是先进后出,即最后进入的元素最先出栈。顺序栈可以通过数组的下标来实现元素的进栈和出栈操作。 在C语言中,可以通过定义一个数组和一个栈顶指针来实现顺序栈。栈顶指针指向栈中最后一个元素的位置。当栈为空时,栈顶指针指向-1。 以下是一种实现顺序栈代码示例: ```c #define MAX_SIZE 100 // 定义栈的最大容量 typedef struct { int data[MAX_SIZE]; // 用数组存储栈元素 int top; // 栈顶指针 } SeqStack; void InitStack(SeqStack *s) { s->top = -1; // 初始化栈顶指针为-1,表示栈为空 } int IsEmpty(SeqStack *s) { return s->top == -1; // 判断栈是否为空 } int IsFull(SeqStack *s) { return s->top == MAX_SIZE - 1; // 判断栈是否已满 } void Push(SeqStack *s, int element) { if (IsFull(s)) { printf("Stack is full.\n"); // 栈已满,无法继续入栈 return; } s->top++; // 栈顶指针加1 s->data[s->top = element; // 元素入栈 } int Pop(SeqStack *s) { if (IsEmpty(s)) { printf("Stack is empty.\n"); // 栈为空,无法进行出栈操作 return -1; } int element = s->data[s->top]; // 获取栈顶元素 s->top--; // 栈顶指针减1 return element; } int GetTop(SeqStack *s) { if (IsEmpty(s)) { printf("Stack is empty.\n"); // 栈为空,无法获取栈顶元素 return -1; } return s->data[s->top]; // 返回栈顶元素 } ``` 以上是一个简单的C语言实现顺序栈代码示例。通过调用相应的函数,可以实现顺序栈的初始化、判断栈是否为空、判断栈是否已满、元素入栈、元素出栈、获取栈顶元素等操作。需要注意的是,在使用顺序栈时,需要注意栈的容量是否足够,以避免栈溢出的情况发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值