Stack it up!

Which line of this implementation of a queue should be changed in order to obtain a stack?

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdlib.h>

struct thing {
int size; /* size of contents */
int bottom; /* first used location */
int top; /* first unused location */
int *elements; /* array of elements */
};

struct thing *thingCreate(int size)
{
struct thing *t;

t = malloc(sizeof(*t));

t->size = size;
t->bottom = t->top = 0;
t->elements = malloc(sizeof(int) * size);

return t;
}

void Push(struct thing *t, int value)
{
t->elements[t->top++] = value;
}

int Pop(struct thing *t)
{
return t->elements[t->bottom++];
 }

25 30 14 10

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Vijay Kumar
Jan 21, 2016

Both the insertion and deletion should be done at the top of the stack. It is the property of the queue, that the deletion will be done at bottom end which will be the first one queued. So line no 30 will be changed to top-- to pop the element out of the stack.

Aditya Kumar
Dec 27, 2015

30 is the right answer. Usually we use codes for stacking at the end.

but it is a pop function, it is used to delete stack !

hiroto kun - 4 years, 5 months ago

plz tell !

hiroto kun - 4 years, 5 months ago

2 pending reports

Vote up reports you agree with

×

Problem Loading...

Note Loading...

Set Loading...