推和弹出功能

时间:2015-06-03 08:36:39

标签: c arrays push pop

我试图使用整数和int数组复制推送和弹出函数。但是我无法在推送功能中找到数组的大小。我如何找到尺寸或者推动'数组中的新值

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;
    }
}

3 个答案:

答案 0 :(得分:3)

首先分配一个包含十个整数的数组,并将指针指定给(RTB_TypeDef *)(0x40080000UL)。然后,您重新分配 S,使其指向S。这不是一个可以取消引用的有效指针。取消引用空指针会导致未定义的行为

您需要单独保留堆栈的大小,并将其传递给函数。或者使用包含指针和大小的结构。

答案 1 :(得分:1)

我已经编写了以下代码!它似乎运行良好!它使用堆栈上溢/下溢控制实现堆栈管理。

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);
  • int pop2(StackType * stack);
  • int mayPop(StackType *stack);
  • StackError getError(StackType * stack);

代码使用以下基本堆栈操作:

  • stack init:sp =“stack dimension”。
  • push:stack [ - sp] = value;
  • pop:stack [sp ++] = value;
  • 堆栈溢出:(sp == 0)[当我们尝试推送值时]
  • 堆栈下溢:(sp ==“stack dimension”)[当我们尝试弹出一个值时]

代码:

#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;
}

答案 2 :(得分:0)

首先,很高兴看到你没有投出malloc的结果。

  1. int  
    main(int argc, char **argv){
    

    请放心,它对代码行为没有任何副作用,但正如我所看到的那样,大多数人都这样做,提高了可读性。应该是,

    int main(int argc, char **argv){  
    
  2. S = malloc(10*sizeof(int));
    S = NULL;
    

    应该是

    S = NULL;        
    S = malloc(10*sizeof(int));
    
  3. 我不理解你在尝试做什么:

    for(i = 0; S[i]; i++)
    

    可能应该是这样的,

    for(i = 0; i < MAX_LENGTH; i++)  //MAX_LENGTH should be some integer #defined somewhere in the code
    
  4. 除了这些明显的错误之外,您实际上可以考虑添加一项检查,以确保在函数while的{​​{1}}循环中,您不会超出{{1}的值比push()可容纳的内容更多。

    然后,而不是做

    size

    s中,我更愿意检查内存是否在if (S == NULL){ S[0] = n; } 之后分配。所以,

    push()

    这应该做你期待的事情:

    malloc

    我正在旅行时,代码未经测试