typedef struct { int* base; int* top; int stacksize; }SqStack;
时间: 2024-05-30 18:15:33 浏览: 12
This is a definition of a struct named SqStack, which represents a stack data structure. It contains three fields:
1. base: a pointer to the bottom of the stack, which is where the first element was added.
2. top: a pointer to the top of the stack, which is where the next element will be added.
3. stacksize: the maximum number of elements the stack can hold.
In C programming language, a struct is a user-defined data type that groups together variables of different data types under a single name. This allows the programmer to manipulate a group of related variables as a single entity.
相关问题
#include<stdio.h> typdef struct SqStack{ int *base; int *top; int stacksize; }SqStack void InitStack(SqStack*s){ (*S).base=(int*)malloc(MAXSIZE*sizeof(int)); s->top=S->base; s->stacksize=MAXSIZE; } void Push(SqStack*S,int e){ *(S->top)=e; S->top++; } int Pop(SqStack*S,int e){ S->top=S->top-1; e=*(S->top); return e; } int StackEmpty(SqStack*S){ if(S->top==S->base) return 1; else return 0; } int main(){ int N; int e; SqStack S; InitStack(&S); printf("Please input N:"); scanf("%d",&N); while(N){ Push(&S,N%8); N=N/8; } while(!StackEmpty(&S)){ e=Pop(&S,e); printf("%d",e); } return 0; }改下bug
在函数Pop中,参数列表中不需要传入int e,因为这个变量是用来接收栈顶元素的,应该改为:
```c
int Pop(SqStack*S){
S->top=S->top-1;
int e=*(S->top);
return e;
}
```
另外,函数调用Pop(&S,e)中第二个参数e是多余的,应该改为:
```c
e = Pop(&S);
```
完整代码如下:
```c
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct SqStack{
int *base;
int *top;
int stacksize;
}SqStack;
void InitStack(SqStack*s){
(*s).base=(int*)malloc(MAXSIZE*sizeof(int));
s->top=s->base;
s->stacksize=MAXSIZE;
}
void Push(SqStack*S ,int e){
*(S->top)=e;
S->top++;
}
int Pop(SqStack*S){
S->top=S->top-1;
int e=*(S->top);
return e;
}
int StackEmpty(SqStack*S){
if(S->top==S->base)
return 1;
else
return 0;
}
int main(){
int N;
int e;
SqStack S;
InitStack(&S);
printf("Please input N:");
scanf("%d",&N);
while(N){
Push(&S,N%8);
N=N/8;
}
while(!StackEmpty(&S)){
e=Pop(&S);
printf("%d",e);
}
return 0;
}
```
补全IsEmptyStack(SqStack *s), IsFullStack(SqStack *s) ,PushStack(SqStack *s,ElemType e) ,PopStack(SqStack *s), Count(SqStack *s) 给定栈的结构体,试按要求完成栈的判空、判满、出栈、入栈、统计栈元素个数函数功能,并在主函数中完成数组元素倒置。从键盘输入n(n<20),然后输入n个数。倒序输出数值。#include "stdio.h" #include "stdlib.h" #include "string.h" #define OK 1 #define ERROR 0 #define INIT_SIZE 10 #define INCREM 5 typedef int ElemType; typedef struct stack { ElemType *base; ElemType *top; int stacksize; }SqStack; int InitStack(SqStack *s); int PushStack(SqStack *s,ElemType e); ElemType PopStack(SqStack *s); int IsEmptyStack(SqStack *s); int IsFullStack(SqStack *s); int Count(SqStack *s); int main() { SqStack st; int n; ElemType a[21]; int i; InitStack(&st); scanf("%d",&n); if(n<=0) return ERROR; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { PushStack(&st,a[i]); } for(i=0;i<n;i++) { a[i]=PopStack(&st); } for(i=0;i<n;i++) printf("%d ",a[i]); return OK; } int InitStack(SqStack *s) { s->base=(ElemType *)malloc(sizeof(ElemType )*INIT_SIZE); if(!s->base) return ERROR; s->top=s->base; s->stacksize=INIT_SIZE; return OK; } int IsEmptyStack(SqStack *s) { } int IsFullStack(SqStack *s) { } int PushStack(SqStack *s,ElemType e) { } ElemType PopStack(SqStack *s) { } int Count(SqStack *s) { }
栈的结构体定义如下:
typedef int ElemType;
typedef struct {
ElemType data[20]; //栈的数组实现
int top; //栈顶指针
}SqStack;
//判空
int IsEmptyStack(SqStack *s){
if(s->top == -1) //栈顶指针为-1表示栈为空
return 1;
else
return 0;
}
//判满
int IsFullStack(SqStack *s){
if(s->top == 19) //栈顶指针为19表示栈已满
return 1;
else
return 0;
}
//入栈
int PushStack(SqStack *s, ElemType e){
if(IsFullStack(s)) //如果栈已满,返回0表示入栈失败
return 0;
else{
s->top++; //栈顶指针+1
s->data[s->top] = e; //将元素e压入栈中
return 1; //返回1表示入栈成功
}
}
//出栈
int PopStack(SqStack *s, ElemType *e){
if(IsEmptyStack(s)) //如果栈为空,返回0表示出栈失败
return 0;
else{
*e = s->data[s->top]; //将栈顶元素存入e中
s->top--; //栈顶指针-1
return 1; //返回1表示出栈成功
}
}
//统计栈元素个数
int Count(SqStack *s){
return s->top + 1; //栈顶指针+1即为栈中元素个数
}
//主函数
int main(){
int n, i, e;
SqStack s;
s.top = -1; //初始化栈顶指针为-1
printf("请输入n(n<20):");
scanf("%d", &n);
printf("请输入%d个数:\n", n);
for(i=0; i<n; i++){
scanf("%d", &e);
PushStack(&s, e); //将元素e入栈
}
printf("倒序输出数值:");
while(!IsEmptyStack(&s)){ //栈非空时循环
PopStack(&s, &e); //出栈并将栈顶元素存入e中
printf("%d ", e); //输出元素e
}
printf("\n");
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)