473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Oct 25 '06 #1
4 6479
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
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

"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.
Oct 25 '06 #4
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>
Oct 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: unified | last post by:
Ok, I'm working on a program that is supposed to compare each letter of a string that is put into a stack and a queue. It is supposed to tell whether or not a word is a palindrome or not. (a...
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
4
by: deanfamily | last post by:
I have a rather pecurliar C++ assignment. I need to create a program (using a stack or queue) to verify if the grouping symbols in an arithmetic expression match. For example: {25 + (3 - 6) * 8}...
4
by: SP | last post by:
Hi, I've a Stack. Now I would use this to mantain a LIFO objects chain. My problem is that I want to limit the Stack dimension. If the Stack is full and I want to add a object, I eant to remove...
7
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways...
2
by: waceys | last post by:
hello everybody i need some body help me to make code for a linked list and arrays build stack and queue such that when i push an element in the stack i can dequeue it first from the queue and when...
3
by: mark4asp | last post by:
Stack of limited size containing unique items? Hi guys, How would I implement a stack of limited size containing unique items? For example. Suppose my stack has . I add 2 to it and it is now...
8
by: t | last post by:
The stack container adaptor uses deque as its default underlying sequential container. I don't understand why since we only add elements and remove elements from one side of a stack. Why isn't...
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.