Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# working in data structures , pseudocode c ; Queues

Posted on 2003-02-28
Medium Priority
463 Views
I have two queues  Q1 , Q2 and I want to execute the contents into queue Q3. The queue are shown front ( left ) to rear ( right ).

Contents of Q1 and Q2

Q1: 42 30 41 31 19 20 25 14 10 11 12 15
Q2: 1 4 5 4 10 13

My algorithm I came up with is shown below ;

1 Q3 = createQueue
2 count  = 0
3 loop ( not empty Q1 and  not empty Q2)
1 count = count + 1
2 dequeue (Q1 , x )
3 dequeue (Q2 , y )
4 if ( y equal count )
1 enqueue (Q3 , x )

now I have tried several times with different solutions. I think the Not is confusing me. If you can help please show me how to work the problem to get the correct ans. of what the contents would be in Q3. I get the first number as 4 that stays ok but the rest never stay in the same position . also please explain the Not. It is a logical Not is it so. where, when if it becomes true it turns false? At least thats the way I read it in my C book.
0
Question by:jen-26

Expert Comment

ID: 8050538
jen-26 if you could explain what you are needing to do, maybee with some pseudocode that is a little more like c
I'm not real certain but it looks like you want to take a number out of Q1 according to an index in Q2.  If this is the case then i can see a few easier ways to do this.  Respond with a better explination and I will try to help you.  If I was write in my assumtion then you may consider taking each element out of Q2 and then using that as an index to accsess the corresponding element in Q1 to storein Q3.
0

LVL 1

Expert Comment

ID: 8051952
Do you have your ADT worked out? I mean all those queue functions like create queue, enquque, deqeue, empty work fine?

I think you do, because as you say, 4 comes as first number, and that's what should be happening:
I will try to re-write your loop, maybe will be clearer

while(Q1 and Q2 have some numbers in them){
count=count +1;
take one element(from left I assume)form Q1 and put in x
take one element from Q2 and put in y
if (y==count)put y into Q3
}

so as you see, when the counter will be equal 4, we will have in y fourth element of Q4, which is 4, so it goes to Q3
at the end of the loop you should be having only 4 in your Q3, because any other elament doesn't happen to be equal count when it is dequeued from Q2

if you are trying to do something else, explain more
0

LVL 1

Expert Comment

ID: 8051975
This is the iteration:

Q1: 42 30 41 31 19 20 25 14 10 11 12 15
Q2: 1 4 5 4 10 13

1: count=1, x=42, y=1,  y==count-false
2: count=2, x=30, y=4,  y==count-false
3: count=3, x=41, y=5,  y==count-false
4: count=4, x=31, y=4,  y==count-true, put 4 in Q3
5: count=5, x=19, y=10, y==count-false
6: count=6, x=20, y=13, y==count-false
Q2 becomes empty
end
0

Expert Comment

ID: 8052580
The solution given by Gula is almost right but 31 goes into Q3 instead of 4.(Note enqueue (Q3 , x )
0

Author Comment

ID: 8066418
I am a programmer for a Major oil co. (IT) and I am trying to learn Data Structures using Algorithm on my own at there request.However I am on my own.
This book uses a Approach to Pseudocode with  C.....
What I am tring to determine that imagine I had two Queue's and the data as listed , and the algorithm I am writing ( hope it is right ) what would be the contents of Queue 3 after the execution of the example shown.
The book is unclear as what else to do , the little on ADT isnt enough to help me.
Now since I am new to this Data Structure and have been programming for a number of years( with several languages learned).
I am not sure what one can do with this , CAN I put THIS to C code to test and run it (HOW) or can you only test it by hand?
I tried to contact the company where I purchased the book.  Written by "Gilberg and Forouzan was reccommended," but they will not sell the answer book, only to school,  if you know what I mean. Sure makes it hard to continue your upgrade .
I am tring to improve my skills in programming.
I came across your site and started to look thru the listing but didnt find what I needed. Is there a way to search for what I need as listed above? For I will be using your site as I use this book.
thank you
0

Author Comment

ID: 8085245

1 The solution given by Gula is almost right but 31 goes into Q3 instead of 4.(Note enqueue (Q3 , x ); Is this correct as stated by another person????

2I am not sure what one can do with this , CAN I put THIS to C code to test and run it (HOW) or can you only test it by hand?

Please read the first comment and let me know , I dont have that many points . If you dont know let me know , if there is a way to recover the last points or however the system works let me know.

0

LVL 1

Expert Comment

ID: 8085840
to say truth, I didn't clearly understand the last message you posted, so I was hoping someone who did understand would reply..

>>The solution given by Gula is almost right but 31 goes into Q3 instead of 4.(Note enqueue (Q3 , x ); Is this correct as stated by another person????

It's true, I missed that it was x, you see, if you had y there in enqueue (Q3 , x ); I mean, if it was enqueue (Q3 , y ); then the result you were getting would make sence, not that it is x... I don't really understand how you are getting 4 in there

few questions, just to get clearer what you want...

>>CAN I put THIS to C code to test and run it (HOW) or can you only test it by hand?

what do you mean by "THIS"? the peice of algorithm? or something like "this" that you can use in java, that would mean the class you are currently in...?

>>I have two queues  Q1 , Q2 and I want to execute the contents into queue Q3.

What do you mean by "execute"? Put all elements Q1 and Q2 have into Q3 or there is some logic that you want to follow to fill it up?

like you gave
Q1: 42 30 41 31 19 20 25 14 10 11 12 15
Q2: 1 4 5 4 10 13

what are you expecting Q3 to have after your algorithm?

0

LVL 1

Expert Comment

ID: 8085868
If you don't clearly understand queues, please post a massage..
I'm afraid to underestimate your knowelege, some people don't like that, ...but it is really hard to answer someone's questions without knowing how much they know. It would really help to know where are you at.
So, please explain if you are comfortable with the concept of queues, using enqueue and dequeue and if there are problems with C syntax..
For that algorithm above, if you have code, please post it, if you don't have it and don't know how to start, just say that...so people know where to start to help from..
thank you
0

Author Comment

ID: 8133403
Yes! I am comfortable with the concept of queues, using enqueue and dequeue and stacks ,there are no problems with C syntax.. some times I will get hung up on the logical ones (ex: not)

What I am looking for is : What would be the contents of Q3 after the above algorithm code is executed, using the values giving for Q1 and Q2? I am not sure what to expect.
I am tring to take the two queue's (Q1,Q2) and place them into a third queue (Q3).

The queue contents  are shown front ( left ) to rear ( right ).

Q1: 42 30 41 31 19 20 25 14 10 11 12 15
Q2: 1 4 5 4 10 13

Second : How to solve it  by hand and get a answer ?
YOU SHOWED ONE METHOD IS THAT CORRECT?

Third :  How to write the above algorithm  in C code?

I hope this is clearer.

Yes ! I have done both C & C++ for a number of years and at work I am tring to pick up Data Structure on my own. This part is new to me, so please excuse the confusion .

----------------
GULA

on your example you show it counts 6 times with just one number going to Q3 , which was 4( thats all ). Now we know it is 31. I am not sure how I kept coming up with 4 but if you came up with 4, some how I must of over looked the x or y.
0

LVL 20

Expert Comment

ID: 10024367
Nothing has happened on this question in over 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
PAQ, refund points (some interesting progress, but asker is not satisfied).

jmcg
EE Cleanup Volunteer
0

Accepted Solution

SpazMODic earned 0 total points
ID: 10059991
PAQed, with points refunded (50)

SpazMODic
EE Moderator
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month15 days, 23 hours left to enroll