C 推送和弹出功能
我试图用整数和整数数组复制push和pop函数。但是,在push函数中查找数组的大小时,我遇到了麻烦。如何找到大小或将新值“推”到数组中C 推送和弹出功能,c,arrays,C,Arrays,我试图用整数和整数数组复制push和pop函数。但是,在push函数中查找数组的大小时,我遇到了麻烦。如何找到大小或将新值“推”到数组中 typedef int data_t; int main(int argc, char **argv){ int *S, i; S = malloc(10*sizeof(int)); S = NULL; push(1,S); push(3,S); for(i = 0; S[i]; i++){ prin
typedef int data_t;
int
main(int argc, char **argv){
int *S, i;
S = malloc(10*sizeof(int));
S = NULL;
push(1,S);
push(3,S);
for(i = 0; S[i]; i++){
printf("%d\n",S[i]);
}
return 0;
}
void
push(data_t n,int *S ){
int size = 0, i = 0;
if (S == NULL){
S[0] = n;
}
else{
while(S[i] != NULL){
size++;
i++;
}
S[size] = n;
}
}
首先分配一个由十个整数组成的数组,并将指针分配到
S
。然后重新分配S
,使其指向NULL
。这不是可以取消引用的有效指针。取消对空指针的引用会导致未定义的行为
您需要单独保持堆栈的大小,并将其传递给函数。或者使用同时包含指针和大小的结构。首先,很高兴看到您没有强制转换malloc的结果
int
main(int argc, char **argv){
请确保它不会对代码行为产生任何副作用,但正如我看到的大多数人这样做,它提高了可读性。应该是,
int main(int argc, char **argv){
S = malloc(10*sizeof(int));
S = NULL;
应该是
S = NULL;
S = malloc(10*sizeof(int));
for(i = 0; S[i]; i++)
也许这应该是这样的
for(i = 0; i < MAX_LENGTH; i++) //MAX_LENGTH should be some integer #defined somewhere in the code
在push()。所以
S = malloc(10*sizeof(int));
if (S == NULL)
{
//Error handling supposed to be done if no memory is allocated
}
这应该满足您的期望:
#include <stdio.h>
#include <stdlib.h>
typedef int data_t;
int top;
void push(data_t,int*);
int pop(int*);
int
main(int argc, char **argv)
{
int *S = NULL, i;
top = -1;
S = malloc(10*sizeof(int));
if(S == NULL)
{
printf("Memory Allocation failed");
//other error handling implementation if any
}
push(1,S);
push(2,S);
push(3,S);
for(i = 0; i <= top; i++){
printf("%d\n",S[i]);
}
printf("\n Popping:\n");
pop(S);
for(i = 0; i <= top; i++){
printf("%d\n",S[i]);
}
return 0;
}
void
push(data_t n,int *S )
{
//Check if Stack is full
if (top == 10)
{
printf("Stack Full");
//other implementation
}
else
{
++top;
S[top] = n;
}
}
int
pop(int *S)
{
//Check if Stack is empty
if (top == -1)
{
printf("Stack Empty");
//other implementation
}
else
{
return S[top--];
}
}
#包括
#包括
typedef int data_t;
int top;
无效推送(数据,整数*);
int-pop(int*);
int
主(内部argc,字符**argv)
{
int*S=NULL,i;
top=-1;
S=malloc(10*sizeof(int));
如果(S==NULL)
{
printf(“内存分配失败”);
//其他错误处理实现(如有)
}
推(1,S);
推(2,S);
推(3,S);
对于(i=0;i我已经动态编写了以下代码!它似乎运行良好!它使用堆栈溢出/下溢控件实现了堆栈管理
main
包含演示所有堆栈函数使用的代码:
int initStack(StackType*stack,size\t numOfElement);
int freeStack(StackType*stack);
int-push(StackType*stack,int-value);
int-mayPush(StackType*stack);
int-pop(StackType*stack,int*value);
intpop2(StackType*stack);
int-mayPop(StackType*stack);
StackError getError(StackType*stack);
该代码使用以下基本堆栈操作:
- 堆栈初始化:sp=“堆栈维度”
- 推送:堆栈[--sp]=值
- pop:堆栈[sp++]=值
- 堆栈溢出:(sp==0)[当我们尝试推送值时]
- 堆栈下溢:(sp==“堆栈维度”)[当我们尝试弹出一个值时]
守则:
#include <stdio.h>
#include <malloc.h>
typedef enum {
NO_ERROR,
MEMORY_ERROR,
STACK_OVERFLOW,
STACK_UNDERFLOW
} StackError;
typedef struct {
int * stack;
size_t numOfElem;
size_t sp; //stack pointer
StackError err;
} StackType;
int initStack(StackType * stack, size_t numOfElement);
int freeStack(StackType * stack);
int push(StackType * stack, int value);
int mayPush(StackType *stack);
int pop(StackType * stack, int * value);
int pop2(StackType * stack);
int mayPop(StackType *stack);
StackError getError(StackType * stack);
int initStack(StackType * stack, size_t numOfElement)
{
if ( (stack->stack=malloc(sizeof(*stack->stack)*numOfElement))==NULL ) {
stack->err=MEMORY_ERROR;
return stack->err;
}
stack->err=NO_ERROR;
stack->numOfElem=numOfElement;
stack->sp=numOfElement; //The stack is void!
return stack->err;
}
int freeStack(StackType * stack)
{
if (stack->stack==NULL){
stack->err=MEMORY_ERROR;
return stack->err;
}
stack->err=NO_ERROR;
free(stack->stack);
stack->stack=NULL;
return stack->err;
}
int push(StackType * stack, int value)
{
if (stack->stack==NULL) {
stack->err=MEMORY_ERROR;
return stack->err;
}
if (!stack->sp) {
stack->err=STACK_OVERFLOW;
return stack->err;
}
stack->err=NO_ERROR;
stack->stack[--stack->sp]=value;
return stack->err;
}
int pop(StackType * stack, int * value)
{
if (stack->stack==NULL) {
stack->err=MEMORY_ERROR;
return stack->err;
}
if (stack->sp>=stack->numOfElem) {
stack->err=STACK_UNDERFLOW;
return stack->err;
}
stack->err=NO_ERROR;
*value=stack->stack[stack->sp++];
return stack->err;
}
int pop2(StackType * stack)
{
int value;
pop(stack,&value);
return value;
}
int mayPush(StackType *stack)
{
return (stack->stack!=NULL && stack->sp>0)?1:0;
}
int mayPop(StackType *stack)
{
return (stack->stack!=NULL && stack->sp<stack->numOfElem)?1:0;
}
StackError getError(StackType * stack)
{
return stack->err;
}
int main(void)
{
StackType stack;
int res,i,j;
size_t max=20;
if ( (res=initStack(&stack, max))!=NO_ERROR ) {
printf("Error: %d\n",res);
return res;
}
//Fill the stack;
printf("Pushing: ");
i=0;
while(mayPush(&stack)) {
push(&stack,++i);
printf("%d ",i);
}
puts("");
//Try to push another element into the stack
res=push(&stack,i);
if (res!=NO_ERROR) {
printf("Push error: %d\n",res);
}
//Read all the stack
printf("Popping: ");
while(mayPop(&stack)) {
printf("%d ",pop2(&stack));
}
puts("");
//Try to pop another element into the stack form 1
res=pop(&stack,&i);
if (res!=NO_ERROR) {
printf("Pop error: %d\n",res);
}
//Try to pop another element into the stack form 2
i=pop2(&stack);
res=getError(&stack);
if (res!=NO_ERROR) {
printf("Pop error: %d\n",res);
}
//Fill an half of the stack
printf("Pushing: ");
for(i=1;i<=(int)max/2;i++) {
push(&stack,i);
printf("%d ",i);
}
puts("");
//Get some value from the stack
printf("Popping: ");
for(i=1;i<=(int)max/4;i++) {
printf("%d ",pop2(&stack));
}
puts("");
//Put some value in the stack (also generates errors)
for (j=0;j<3;j++) {
printf("Pushing: ");
for(i=1;i<=(int)max/3;i++) {
printf("%d ",i*3+j);
if ( (res=push(&stack,i*3+j))!=NO_ERROR ) {
printf("Push error: %d\n",res);
}
}
puts("");
}
//Get some value from the stack (also generates errors)
printf("Popping: ");
for(i=0;i<(int)max+2;i++) {
if ( (res=pop(&stack,&j))!=NO_ERROR ) {
printf("\nPop error: %d",res);
} else {
printf("%d ",j);
}
}
puts("");
puts("Deallocating the stack!");
freeStack(&stack);
printf("Pushing: ");
if ( (res=push(&stack,415))!=NO_ERROR ) {
printf("Push error: %d\n",res);
}
puts("Re-Deallocating the stack!");
if ( (freeStack(&stack))!=NO_ERROR ) {
printf("freeStack Error: %d\n",res);
}
return 0;
}
#包括
#包括
类型定义枚举{
没有错误,
内存错误,
堆栈溢出,
烟囱下溢
}堆栈错误;
类型定义结构{
int*堆栈;
大小(单位);;
size\u t sp;//堆栈指针
堆栈错误;
}堆垛式;
int initStack(StackType*stack,size\t numOfElement);
int freeStack(堆栈类型*堆栈);
int push(堆栈类型*堆栈,int值);
int mayPush(堆栈类型*堆栈);
int-pop(StackType*stack,int*value);
int pop2(堆栈类型*堆栈);
int mayPop(堆栈类型*堆栈);
StackError getError(StackType*stack);
int initStack(StackType*stack,size\t numOfElement)
{
if((stack->stack=malloc(sizeof(*stack->stack)*numOfElement))==NULL){
堆栈->错误=内存错误;
返回堆栈->错误;
}
堆栈->错误=无错误;
堆栈->numOfElem=numOfElement;
stack->sp=numOfElement;//堆栈无效!
返回堆栈->错误;
}
int freeStack(堆栈类型*堆栈)
{
如果(堆栈->堆栈==NULL){
堆栈->错误=内存错误;
返回堆栈->错误;
}
堆栈->错误=无错误;
自由(堆栈->堆栈);
stack->stack=NULL;
返回堆栈->错误;
}
int push(堆栈类型*堆栈,int值)
{
如果(堆栈->堆栈==NULL){
堆栈->错误=内存错误;
返回堆栈->错误;
}
如果(!堆栈->sp){
堆栈->错误=堆栈溢出;
返回堆栈->错误;
}
堆栈->错误=无错误;
堆栈->堆栈[--堆栈->sp]=值;
返回堆栈->错误;
}
int-pop(StackType*stack,int*value)
{
如果(堆栈->堆栈==NULL){
堆栈->错误=内存错误;
返回堆栈->错误;
}
如果(堆栈->sp>=堆栈->numOfElem){
堆栈->错误=堆栈下溢;
返回堆栈->错误;
}
堆栈->错误=无错误;
*值=堆栈->堆栈[堆栈->sp++];
返回堆栈->错误;
}
int pop2(堆栈类型*堆栈)
{
int值;
pop(堆栈和值);
返回值;
}
int mayPush(堆栈类型*堆栈)
{
返回(stack->stack!=NULL&&stack->sp>0)?1:0;
}
int mayPop(堆栈类型*堆栈)
{
返回(stack->stack!=NULL&&stack->spnumfelem)?1:0;
}
StackError getError(StackType*stack)
{
返回堆栈->错误;
}
内部主(空)
{
堆垛式堆垛;
int res,i,j;
最大尺寸=20;
if((res=initStack(&stack,max))!=无错误){
printf(“错误:%d\n”,res);
返回res;
}
//填满堆栈;
printf(“推送:”);
i=0;
while(可推送和堆栈)){
推送(&堆栈,++i);
printf(“%d”,i);
}
认沽权(“”);
//尝试将另一个元素推入堆栈
res=推送(和堆栈,i);
if(res!=无错误){
printf(“推送错误:%d\n”,res);
}
//读取所有堆栈
printf(“弹出:”);
while(mayPop和stack)){
printf(“%d”,pop2(&堆栈));
}
认沽权(“”);
//尝试将另一个元素弹出到堆栈表单1中
res=pop(&stack,&i);
if(res!=无错误){
printf(“弹出错误:%d\n”,res);
}
//尝试将另一个元素弹出到堆栈表单2中
i=pop2(&堆栈);
res=getError(&stack);
if(res!=无错误){
printf(“弹出错误:%d\n”,res);
}
//填满一堆的一半
printf(“推送:”);
对于(i=1;iS=malloc(10*sizeof(int));S=NULL;
我认为您需要复习C编程的基础知识。您可以立即覆盖变量,以便
#include <stdio.h>
#include <malloc.h>
typedef enum {
NO_ERROR,
MEMORY_ERROR,
STACK_OVERFLOW,
STACK_UNDERFLOW
} StackError;
typedef struct {
int * stack;
size_t numOfElem;
size_t sp; //stack pointer
StackError err;
} StackType;
int initStack(StackType * stack, size_t numOfElement);
int freeStack(StackType * stack);
int push(StackType * stack, int value);
int mayPush(StackType *stack);
int pop(StackType * stack, int * value);
int pop2(StackType * stack);
int mayPop(StackType *stack);
StackError getError(StackType * stack);
int initStack(StackType * stack, size_t numOfElement)
{
if ( (stack->stack=malloc(sizeof(*stack->stack)*numOfElement))==NULL ) {
stack->err=MEMORY_ERROR;
return stack->err;
}
stack->err=NO_ERROR;
stack->numOfElem=numOfElement;
stack->sp=numOfElement; //The stack is void!
return stack->err;
}
int freeStack(StackType * stack)
{
if (stack->stack==NULL){
stack->err=MEMORY_ERROR;
return stack->err;
}
stack->err=NO_ERROR;
free(stack->stack);
stack->stack=NULL;
return stack->err;
}
int push(StackType * stack, int value)
{
if (stack->stack==NULL) {
stack->err=MEMORY_ERROR;
return stack->err;
}
if (!stack->sp) {
stack->err=STACK_OVERFLOW;
return stack->err;
}
stack->err=NO_ERROR;
stack->stack[--stack->sp]=value;
return stack->err;
}
int pop(StackType * stack, int * value)
{
if (stack->stack==NULL) {
stack->err=MEMORY_ERROR;
return stack->err;
}
if (stack->sp>=stack->numOfElem) {
stack->err=STACK_UNDERFLOW;
return stack->err;
}
stack->err=NO_ERROR;
*value=stack->stack[stack->sp++];
return stack->err;
}
int pop2(StackType * stack)
{
int value;
pop(stack,&value);
return value;
}
int mayPush(StackType *stack)
{
return (stack->stack!=NULL && stack->sp>0)?1:0;
}
int mayPop(StackType *stack)
{
return (stack->stack!=NULL && stack->sp<stack->numOfElem)?1:0;
}
StackError getError(StackType * stack)
{
return stack->err;
}
int main(void)
{
StackType stack;
int res,i,j;
size_t max=20;
if ( (res=initStack(&stack, max))!=NO_ERROR ) {
printf("Error: %d\n",res);
return res;
}
//Fill the stack;
printf("Pushing: ");
i=0;
while(mayPush(&stack)) {
push(&stack,++i);
printf("%d ",i);
}
puts("");
//Try to push another element into the stack
res=push(&stack,i);
if (res!=NO_ERROR) {
printf("Push error: %d\n",res);
}
//Read all the stack
printf("Popping: ");
while(mayPop(&stack)) {
printf("%d ",pop2(&stack));
}
puts("");
//Try to pop another element into the stack form 1
res=pop(&stack,&i);
if (res!=NO_ERROR) {
printf("Pop error: %d\n",res);
}
//Try to pop another element into the stack form 2
i=pop2(&stack);
res=getError(&stack);
if (res!=NO_ERROR) {
printf("Pop error: %d\n",res);
}
//Fill an half of the stack
printf("Pushing: ");
for(i=1;i<=(int)max/2;i++) {
push(&stack,i);
printf("%d ",i);
}
puts("");
//Get some value from the stack
printf("Popping: ");
for(i=1;i<=(int)max/4;i++) {
printf("%d ",pop2(&stack));
}
puts("");
//Put some value in the stack (also generates errors)
for (j=0;j<3;j++) {
printf("Pushing: ");
for(i=1;i<=(int)max/3;i++) {
printf("%d ",i*3+j);
if ( (res=push(&stack,i*3+j))!=NO_ERROR ) {
printf("Push error: %d\n",res);
}
}
puts("");
}
//Get some value from the stack (also generates errors)
printf("Popping: ");
for(i=0;i<(int)max+2;i++) {
if ( (res=pop(&stack,&j))!=NO_ERROR ) {
printf("\nPop error: %d",res);
} else {
printf("%d ",j);
}
}
puts("");
puts("Deallocating the stack!");
freeStack(&stack);
printf("Pushing: ");
if ( (res=push(&stack,415))!=NO_ERROR ) {
printf("Push error: %d\n",res);
}
puts("Re-Deallocating the stack!");
if ( (freeStack(&stack))!=NO_ERROR ) {
printf("freeStack Error: %d\n",res);
}
return 0;
}