HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Stack implementation using only one queue in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
stackoneusingimplementationqueueonly

Problem

We start with an empty queue. For the push operation we simply insert the value to be pushed into the queue.

The pop operation needs some manipulation. When we need to pop from the stack (simulated with a queue), first we get the number of elements in the queue, say n, and remove (n-1) elements from the queue and keep on inserting in the queue one by one. That is, we remove the front element from the queue, and immediately insert into the queue in the rear, then we remove the front element from the queue and then immediately insert into the rear, thus we continue upto (n-1) elements.

Then we will perform a remove operation, which will actually remove the nth element of the original state of the queue, and return. Note that the nth element in the queue is the one which was inserted last, and we are returning it first, therefore it works like a pop operation (Last in First Out).

```
#include
#include

/ Queue structure /

#define QUEUE_EMPTY_MAGIC 0xdeadbeef
typedef struct _queue_t
{
int *arr;
int rear, front, count, max;
} queue_t;

/ Queue operation function prototypes /
queue_t *queue_allocate (int n);
void queue_insert (queue_t * q, int v);
int queue_remove (queue_t * q);
int queue_count (queue_t * q);
int queue_is_empty (queue_t * q);

/ NOTE: Here is the stuff we are interested in /
/ Simulated stack operations START /

/* NOTE: passing the queue object, on which we will only operate the
* queue operations.
*/
void
stack_push (queue_t * q, int v)
{
queue_insert (q, v);
}

int
stack_pop (queue_t * q)
{
int i, n = queue_count (q);
int removed_element;

for (i = 0; i count;
}

queue_t *
queue_allocate (int n)
{
queue_t *queue;

queue = malloc (sizeof (queue_t));
if (queue == NULL)
return NULL;
queue->max = n;

queue->arr = malloc (sizeof (int) * n);
queue->rear = n - 1;
queue->front = n - 1;

return queue;
}

void
queue_insert (queue_t * q, int v)
{
if (q->count == q->max)
return;

q->rear = (q->rear + 1) % q->

Solution

-
Since the _t suffix is already reserved by POSIX, you should rename stack_t and queue_t. Using reserved names in your own implementation can cause name-clashing issues. More info about this can be found here.

-
If you're just printing unformatted text, use puts() instead of printf().

-
This is redundant:

case 0:
  printf ("\nQutting.\n");
  return 0;

default:
  printf ("\nQutting.\n");
  return 0;


Just combine them into one if they're supposed to do the same thing. I'd also use 3 instead of 0 to keep the case numbers in order.

case 3: default:
  printf ("\nQutting.\n");
  return 0;

Code Snippets

case 0:
  printf ("\nQutting.\n");
  return 0;

default:
  printf ("\nQutting.\n");
  return 0;
case 3: default:
  printf ("\nQutting.\n");
  return 0;

Context

StackExchange Code Review Q#56538, answer score: 2

Revisions (0)

No revisions yet.