简体   繁体   English

在 c 中使用堆栈对 int 数组进行排序

[英]Sorting an array of int using stack in c

I am trying to sort a stack of elements but the function overflow and I don't know why it do that.我正在尝试对一堆元素进行排序,但 function 溢出了,我不知道为什么要这样做。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define type int   //type of element in the stack
#define  max 100

typedef struct {
    int top;
    type array[max];
} stack;

stack *initialize () {
    stack *s = malloc (sizeof (stack));
    s->top = 0;
    return s;
}

void push (stack *s, type x) {
    s->array[s->top++] = x;
}

type pop (stack *s) {
    return s->array[--s->top];
}

type isfull (stack *s) {
    return s->top >= max;
}

type isempty (stack *s) {
    return !s->top;
}

type peek (stack *s) {
    return s->array[s->top - 1];
}

void sortstack (stack *s) { //sorting the stack
    stack *temp = initialize();
    int x, flag;
    do {
        flag = 0;
        while (!isempty (s)) {
            x = pop (s);
            if (x > peek (s)) {
                push (temp, pop (s));
                push (s, x);
                flag = 1;
            } else push (temp, x);
        }
        while (!isempty (temp)) push (s, pop (temp));
    } while (flag);
}

int main() {
    stack *s = initialize();
    push (s, 2);
    push (s, 4);
    push (s, 4);
    push (s, 7);
    push (s, 9);
    push (s, 18);
    sortstack (s);
    while (!isempty (s)) printf ("%d  ", pop (s));
    
    return 0;
}

There are multiple problems in the code:代码中存在多个问题:

  • in if (x > peek (s)) you should test if the stack s is not empty to avoid undefined behavior accessing s->array[-1] .if (x > peek (s))中,您应该测试堆栈s是否不为空,以避免访问s->array[-1]的未定义行为。

  • x should be defined with type type . x应该定义为类型type

  • you should free the temporary stack temp before leaving the function sortstack .在离开 function sortstack之前,您应该释放临时堆栈temp

  • you should use typedef int type;你应该使用typedef int type; instead of #define type int而不是#define type int

  • it is idiomatic to define macros such as max in uppercase, using a more descriptive name is recommended.以大写形式定义诸如max之类的宏是惯用的,建议使用更具描述性的名称。

  • adding assert statements helps catch unexpected error conditions.添加assert语句有助于捕获意外错误情况。

Here is a modified version:这是修改后的版本:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int type;   //type of element in the stack
#define STACKSIZE 100

typedef struct {
    int top;
    type array[STACKSIZE];
} stack;

stack *initialize(void) {
    stack *s = malloc(sizeof(stack));
    assert(s != NULL);
    s->top = 0;
    return s;
}

void discard(stack *s) {
    free(s);
}

void push(stack *s, type x) {
    assert(s->top < STACKSIZE);
    s->array[s->top++] = x;
}

type pop(stack *s) {
    assert(s->top > 0);
    return s->array[--s->top];
}

type isfull(stack *s) {
    return s->top >= max;
}

type isempty(stack *s) {
    return !s->top;
}

type peek(stack *s) {
    assert(s->top > 0);
    return s->array[s->top - 1];
}

void sortstack(stack *s) { //sorting the stack
    stack *temp = initialize();
    int flag;
    do {
        flag = 0;
        while (!isempty(s)) {
            type x = pop(s);
            if (!isempty(s) && x > peek(s)) {
                push(temp, pop(s));
                push(s, x);
                flag = 1;
            } else {
                push(temp, x);
            }
        }
        while (!isempty(temp)) {
            push(s, pop(temp));
        }
    } while (flag);
    discard(temp);
}

int main() {
    stack *s = initialize();
    push(s, 2);
    push(s, 4);
    push(s, 4);
    push(s, 7);
    push(s, 9);
    push(s, 18);
    sortstack(s);
    while (!isempty(s)) {
        printf("%d  ", pop(s));
    }
    printf("\n");
    discard(s);
    
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM