Stack and Queue

R

raghu

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

Is this program correct? Please help..

Thanks a lot in advance.

Regards,
Raghu
 
R

Richard Heathfield

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

Is this program correct? Please help..


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

michael.kirby

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

Is this program correct? Please help..

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
 
M

Malcolm

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

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

CBFalconer

Malcolm said:
.... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top