顺序栈

本文介绍了一种基于数组实现的顺序栈,并提供了初始化、判断空满状态、元素入栈和出栈等基本操作的代码示例。通过具体示例展示了顺序栈的操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

顺序栈通过数组实现,遵循先进先出

#include <stdio.h>  
#include <stdlib.h>  
#define TRUE 1  
#define FALSE 0  
#define Stack_Size 50  
typedef int StackElementType;  
typedef struct{  
    StackElementType elem[Stack_Size];  
    int top;  
}SeqStack;  
/* run this program using the console pauser or add your own getch, system("pause") or input loop */  
void InitStack(SeqStack *S){  
    S->top=-1;  
}  
int IsEmpty(SeqStack *S){  
    if(S->top==-1){  
        return TRUE;  
    }  
    else return FALSE;  
}  
int IsFull(SeqStack *S){  
    if(S->top==Stack_Size-1){  
        return TRUE;  
    }  
    else return FALSE;  
}  
int setStack(SeqStack *S){  
    int n,i;  
    if(S->top<Stack_Size-1){  
        
        for(i=0;i<5;i++){  
            S->top++;  
            S->elem[i]=i+1;  
        //  printf("进栈元素为:",S->elem[i]);   
        }  
    }  
    printf("进栈元素为:");  
    for(i=S->top;i>=0;i--){  
        printf("%d",S->elem[i]);   
    }  
    printf("\n");  
    return 0;  
}  
//进栈   
int Push(SeqStack *S,StackElementType x){  
    int i;   
    if(S->top<Stack_Size-1){  
        S->top++;  
        S->elem[S->top]=x;  
    }  
    printf("进栈后元素为:");  
    for(i=S->top;i>=0;i--){  
        printf("%d",S->elem[i]);   
    }  
    printf("\n");  
    return TRUE;  
}  
//出栈  
int Pop(SeqStack *S,StackElementType *x){  
    int i;   
    IsEmpty(S);  
    *x=S->elem[S->top];  
    S->top--;  
    printf("出栈后元素为:");  
    for(i=S->top;i>=0;i--){  
        printf("%d",S->elem[i]);   
    }  
    printf("\n");  
    return 0;   
}  
void menu(){  
    printf("");  
}  
int main() {  
    StackElementType x,a;  
    SeqStack client;  
    client.top=-1;  
    
    setStack(&client);  
    printf("输入x值:");   
    scanf("%d",&x);  
    Push(&client,x);  
    Pop(&client,&a);  
    return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值