# Stack and Queue

Discussion in 'C Programming' started by raghu, Oct 25, 2006.

1. ### raghuGuest

// 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); // here i'm filling (pushing) the elements
for(i=4;i>=0;i--)
printf("%d",a); // here i'm poping out the elements
// now for queue
for(i=0;i<5;i++)
scanf("%d",&a);
for(i=0;i<5;i++)
printf("%d",a);
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...

Regards,
Raghu

raghu, Oct 25, 2006

2. ### Richard HeathfieldGuest

raghu said:

> // 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); // here i'm filling (pushing) the elements
> for(i=4;i>=0;i--)
> printf("%d",a); // here i'm poping out the elements
> // now for queue
> for(i=0;i<5;i++)
> scanf("%d",&a);
> for(i=0;i<5;i++)
> printf("%d",a);
> 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...
>

No, the program is not correct. It exhibits undefined behaviour because you
are calling variadic functions without a valid function prototype in scope.

Even if that is corrected, your program will still exhibit undefined
behaviour if it is presented with poorly-formed input.

Note, also, that you should return a value from main.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Richard Heathfield, Oct 25, 2006

3. ### Guest

raghu wrote:
> // 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); // here i'm filling (pushing) the elements
> for(i=4;i>=0;i--)
> printf("%d",a); // here i'm poping out the elements
> // now for queue
> for(i=0;i<5;i++)
> scanf("%d",&a);
> for(i=0;i<5;i++)
> printf("%d",a);
> 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...
>
>
> Thanks a lot in advance.

Generally speaking, i guess your definition of stack vs. queue is
correct. If you want to know if it is correct, why dont you just
create the program and run it? I did, and here are the results:

1
2
3
4
5
54321
1
2
3
4
5
12345

>
> Regards,
> Raghu

, Oct 25, 2006
4. ### MalcolmGuest

"raghu" <> wrote in message
news:...
> // 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); // here i'm filling (pushing) the elements
> for(i=4;i>=0;i--)
> printf("%d",a); // here i'm poping out the elements
> // now for queue
> for(i=0;i<5;i++)
> scanf("%d",&a);
> for(i=0;i<5;i++)
> printf("%d",a);
> 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...
>
>
> 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

Malcolm, Oct 25, 2006
5. ### CBFalconerGuest

Malcolm wrote:
>

.... snip ...
>
> 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.

If you want both implement a deque, with operations:

init()
push()
pop()
dequeue()
empty()
full()

The last two return booleans.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

CBFalconer, Oct 25, 2006