Stack and Queue



// Program to implement both stack and queue on an array

int main(void)
{
int a[5],i;
for(i=0;i<5;i++)
scanf("%d",&a[i]); // here i'm filling (pushing) the elements
for(i=4;i>=0;i--)
printf("%d",a[i]); // here i'm poping out the elements
// now for queue
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
printf("%d",a[i]);
return;
}
My logic for stack is which ever item is stored in the last should be
taken out first and so on...so I have filled the array and printing
them from the last....Am I correct?????

As for the Queue FIFO logic.So the item which is stored first is
printed first and so on...

Is this program correct? Please help..

Thanks a lot in advance.

Regards,
Raghu

.



Relevant Pages

  • Re: Resources about program design in C
    ... an interest in the reasoning behind global variables in C programs. ... For example, sorting an array is a single, well-defined ... imagine a module that implements a stack ... static int stack_ptr; ...
    (comp.lang.c)
  • Re: what is a stack
    ... There's FILO and FIFO, they're both implemented as a regular old array (at ... Consider a stack of plates in a cafeteria, ... aka a queue. ...
    (comp.lang.java.programmer)
  • Re: Resources about program design in C
    ... an interest in the reasoning behind global variables in C programs. ... sorting an array and opening a network connection are two ... using an array for the stack contents and an integer for the stack ... extern int push; ...
    (comp.lang.c)
  • Re: Depth Fist Search and Breadth First Search and using array
    ... array to act as a Stack, but we can just use recursion. ... you can certainly do breadth first search without a queue. ...
    (comp.programming)
  • Re: can any one tell me how to implement stack with queue.
    ... can any one write program for implementing stack using one queue. ... NODEPTR tempP = root; ... int GetNumItems() { ...
    (comp.programming)