Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
Ordenando una matriz de int usando stack en c

Estoy tratando de ordenar una pila de elementos, pero la función se desborda y no sé por qué lo hace.

 #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; }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Hay varios problemas en el código:

  • en if (x > peek (s)) debe probar si la pila s no está vacía para evitar un comportamiento indefinido al acceder a s->array[-1] .

  • x debe definirse con tipo type .

  • debe liberar la pila temporal temp antes de salir de la función sortstack .

  • debe usar typedef int type; en lugar de #define type int

  • es idiomático definir macros como max en mayúsculas, se recomienda usar un nombre más descriptivo.

  • agregar declaraciones de assert ayuda a detectar condiciones de error inesperadas.

Aquí hay una versión modificada:

 #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; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!