"raghu" <ra*********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
// 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
You are not really implementing these data structures.
What you need to do is to write functions which will pop and push, in the
case of the stack, and append and remove in the case of the queue.
Stacks are easier to implement.
Be generous and allow 100 integers maximum for the stack
int stack[100]; /* global stack */
Now we need a stack top. Initially is is zero
int stacktop = 0; /* the top of the stack */
Now implent
/*
push an integer onto the stack. Abort program with an error message if
the stack overflows.
*/
void push(int x)
{
}
/*
return the top item from the stack and remove it.
You decide what to do if the user tries to pop and empty stack - do you
want to abort or silently return zero ? Whatever you do, don't crash the
program by trying to access memory at location -1.
*/
int pop(void)
Once you've done that, we can start on the queue.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.