用c语言写栈的入栈操作:void push(sqStack *s, int x)
时间: 2024-02-23 08:58:00 浏览: 107
以下是用C语言实现栈的入栈操作的代码:
```
void push(sqStack *s, int x) {
if (isFull(s)) {
printf("栈已满,无法插入元素!");
return;
}
*(s->top) = x;
s->top++;
}
```
在这段代码中,我们首先判断栈是否已满,如果已满,则无法插入元素,直接返回。否则,我们将元素x插入到栈顶top所指的位置,并将top指针向上移动一个位置,表示栈顶指针指向下一个位置,以便下一次插入元素。
相关问题
要求用C语言实现函数,借助如下自定义栈SqStack将一个正整数N转换为k(2<=k<=16)进制数并输出。输出时用大写字母A、B、C、D、E、F分别表示10、11、12、13、14、15。函数接口定义: void conversion(int N, int k);
以下是实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
typedef struct {
int *base;
int *top;
int stackSize;
} SqStack;
void InitStack(SqStack *S) {
S->base = (int *)malloc(STACK_INIT_SIZE * sizeof(int));
if (!S->base) {
exit(0);
}
S->top = S->base;
S->stackSize = STACK_INIT_SIZE;
}
void Push(SqStack *S, int e) {
if (S->top - S->base >= S->stackSize) {
S->base = (int *)realloc(S->base, (S->stackSize + STACK_INCREMENT) * sizeof(int));
if (!S->base) {
exit(0);
}
S->top = S->base + S->stackSize;
S->stackSize += STACK_INCREMENT;
}
*(S->top) = e;
S->top++;
}
int Pop(SqStack *S) {
int e;
if (S->top == S->base) {
return -1;
}
S->top--;
e = *(S->top);
return e;
}
void conversion(int N, int k) {
SqStack S;
InitStack(&S);
while (N) {
Push(&S, N % k);
N /= k;
}
while (S.top != S.base) {
int e = Pop(&S);
if (e >= 10) {
printf("%c", 'A' + e - 10);
} else {
printf("%d", e);
}
}
}
int main() {
int N = 17;
int k = 2;
conversion(N, k);
printf("\n");
return 0;
}
```
这里我们使用了一个自定义栈SqStack来存储转换后的k进制数。首先将N除以k的余数压入栈中,直到N为0为止。然后依次弹出栈中的元素并输出,如果元素大于等于10,就输出对应的大写字母。
将此c++代码转换为c语言代码#include<iostream> #include<cstdlib> #include<cstdio> #include<stdio.h> #include<string.h> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW - 2 #define MAXSIZE 100 typedef int Status; typedef int SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; } SqStack; Status InitStack(SqStack &s) { s.base = new SElemType[MAXSIZE]; if(!s.base) exit(OVERFLOW); s.top = s.base; s.stacksize = MAXSIZE; return OK; } void DestroyStack(SqStack &s) { delete []s.base; s.base = s.top = NULL; s.stacksize = MAXSIZE; } Status Push(SqStack &s, int x) { if((s.top-s.base)==s.stacksize)return ERROR; *s.top=x; s.top++; return OK; } int Pop(SqStack &s) { int x; if(s.base==s.top)return ERROR; s.top--; x=*s.top; return x; } void PrintStack(SqStack s) { for(SElemType *top = s.top - 1; top >= s.base; top--) { cout << (*top); if(top != s.base) cout << ' '; } cout << endl; } int main() { SqStack s; char op[10]; int x,y,temp,sum,len,i; InitStack(s); while(scanf("%s",op)&&strcmp(op,"@")) { if(!strcmp(op," ")) { scanf("%s",op); } else if(strcmp(op,"/")&&strcmp(op,"*")&&strcmp(op,"+")&&strcmp(op,"-")) { temp=1,sum=0; len=strlen(op); for(i=len-1;i>=0;i--) { sum=sum+(op[i]-'0')*temp; temp*=10; } Push(s,sum); } else if(!strcmp(op,"+")) { x=Pop(s); y=Pop(s); Push(s,y+x); } else if(!strcmp(op,"-")) { x=Pop(s); y=Pop(s); Push(s,y-x); } else if(!strcmp(op,"/")) { x=Pop(s); y=Pop(s); Push(s,y/x); } else if(!strcmp(op,"*")) { x=Pop(s); y=Pop(s); Push(s,y*x); } } PrintStack(s); DestroyStack(s); return 0; }
以下是将该代码转换为 C 语言代码后的结果:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
Status InitStack(SqStack *s) {
s->base = (SElemType *) malloc(MAXSIZE * sizeof(SElemType));
if (!s->base) {
exit(OVERFLOW);
}
s->top = s->base;
s->stacksize = MAXSIZE;
return OK;
}
void DestroyStack(SqStack *s) {
free(s->base);
s->base = s->top = NULL;
s->stacksize = MAXSIZE;
}
Status Push(SqStack *s, int x) {
if ((s->top - s->base) == s->stacksize) {
return ERROR;
}
*(s->top) = x;
(s->top)++;
return OK;
}
int Pop(SqStack *s) {
int x;
if (s->base == s->top) {
return ERROR;
}
(s->top)--;
x = *(s->top);
return x;
}
void PrintStack(SqStack s) {
for (SElemType *top = s.top - 1; top >= s.base; top--) {
printf("%d", *top);
if (top != s.base) {
printf(" ");
}
}
printf("\n");
}
int main() {
SqStack s;
char op[10];
int x, y, temp, sum, len, i;
InitStack(&s);
while (scanf("%s", op) && strcmp(op, "@")) {
if (!strcmp(op, " ")) {
scanf("%s", op);
} else if (strcmp(op, "/") && strcmp(op, "*") && strcmp(op, "+") && strcmp(op, "-")) {
temp = 1;
sum = 0;
len = strlen(op);
for (i = len - 1; i >= 0; i--) {
sum = sum + (op[i] - '0') * temp;
temp *= 10;
}
Push(&s, sum);
} else if (!strcmp(op, "+")) {
x = Pop(&s);
y = Pop(&s);
Push(&s, y + x);
} else if (!strcmp(op, "-")) {
x = Pop(&s);
y = Pop(&s);
Push(&s, y - x);
} else if (!strcmp(op, "/")) {
x = Pop(&s);
y = Pop(&s);
Push(&s, y / x);
} else if (!strcmp(op, "*")) {
x = Pop(&s);
y = Pop(&s);
Push(&s, y * x);
}
}
PrintStack(s);
DestroyStack(&s);
return 0;
}
```
主要的改动如下:
- 使用 `<stdio.h>` 和 `<stdlib.h>` 头文件代替 `<iostream>` 和 `<cstdlib>` 头文件;
- 去掉 `using namespace std;`;
- 将 `typedef struct` 改为 `typedef`;
- 将 `SqStack &` 改为 `SqStack *`,并在函数内使用指针访问结构体成员;
- 将 `cout` 改为 `printf`;
- 将 `strcmp` 改为 `strcmp` 函数;
- 将 `new` 和 `delete` 改为 `malloc` 和 `free`。
阅读全文