449,140 Members | 1,298 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,140 IT Pros & Developers. It's quick & easy.

# Stack and Queue

 P: n/a // 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 Oct 25 '06 #1
4 Replies

 P: n/a 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[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.. 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) Oct 25 '06 #2

 P: n/a 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[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. 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 '06 #3

 P: n/a "raghu" =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. Oct 25 '06 #4

 P: n/a 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. Oct 25 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.