Sorting two stacks by using structs -


there 3 stacks - a, b, c

stacks , b sorted (the number on top of stack biggest). stack c empty 5 operation allowed:

push, pop, top, is_empty, create

we need write function receives stacks , b, moves numbers in stacks , b stack c , stack c must sorted (biggest number on top).

i have algorithm :

compare top of top of b

pop least element , push stack c  repeat step 2 until of stack ( or b) becomes empty  move remaining elements non-empty stack c. have elements in c  in ascending order. (that least element @ top).  move elements c a. (contents in in descending order)  move elements b. (contents in b in ascending order)  move elements b c. 

and started write code there errors , don't know why !

the code :

#include <stdio.h>  #include <stdlib.h> #include <conio.h> #define max_members 10 typedef struct {     int num; }item;  typedef struct {     item a[max_members];     int top; }stack;  void create_stack(stack *s) {     s->top=-1; }  int is_empty(stack *s) {     return s->top==-1; }  int is_full(stack *s) {     return s->top==max_members-1; }  item pop(stack *s) {     return s->a[s->top--]; }  void (stack *s,item *item) {     s->a[++s->top]=*item; }  item top(stack *s) {     return s->a[s->top]; }  void sort (stack *a,stack *b,stack *c) {     while(!is_empty(&a)||!is_empty(&b))         if(top(&a)>top(&b))             push(&c,pop(&b));      if(!is_empty(&a))     {         while(!is_empty(&a))             push(&c,pop(&a));     }      else     {         while(!is_empty(&b))             push(&c,pop(&b));     }      while(!is_empty(&c))         push(&a,pop(&c));      while(!is_empty(&a))         push(&b,pop(&a));      while(!is_empty(&b))         push(&c,pop(&b));  }  void main(void) {     stack a,b,c;     create_stack(&a);     create_stack(&b);     create_stack(&c);     sort(&a,&b,&c); } 

you should pop highest element , push c.

to push elements onto c in reversed order need highest element @ bottom of c (this value must pushed first). if > b then: pop a, push c.

anyway, code doesn't seem safe. have @ following:

while(!is_empty(&a)||!is_empty(&b))     if(top(&a)>top(&b)) 

let's assume empty , b not a.top equals -1 , b.top in range 0 max_members - 1. since 1 of stacks not empty condition true. calling top(&a) following code executed:

return a[-1]; //error: index out of bounds

this how code resolved. is_empty(&a) returns true !is_empty(&a) returns false (false || true) returns true  while(false || true)     if( top(&a) > top(&b) ) 

anyway, doubt code compile.

void (stack *s,item *item) {     s->a[++s->top]=*item; } 

this should that:

void push(stack *s,item *item) {     s->a[++s->top]=*item; } 

also consider that:

void main(void) {     stack a,b,c;     create_stack(&a); //stack created, contains no elements     create_stack(&b); //stack b created, contains no elements     create_stack(&c); //stack c created, contains no elements     sort(&a,&b,&c); //sort elements of stack? } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -