+ 4

How can front print rear along with itself when they are not linked?

https://code.sololearn.com/c3ADUeMZW1Q3/?ref=app Consider the program above, where I try to implement queues using Linked Lists as taught in school. In the function Enqueue(): We assign the rear and the next of rear (rear->next) both with np. Why is that done? Also, front, is never relinked with rear anywhere in the program. Then how is it that it can access rear and its elements? Is rear just a temp pointer then?

12th Sep 2017, 6:27 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
7 Respostas
+ 16
front->next points to the rear when there are two elements in the queue. (obj1) -> (obj2) -> (NULL) i.e. front points to obj1, rear points to obj2, front->next is obj2, rear->next is NULL. So what happens when we want to add a new element, obj3 (assuming newnode) to the queue? We want to fill the next slot after obj2, which is NULL. Rear is pointing to obj2, so we use that. rear->next = newnode; Now, we have (obj1) -> (obj2) -> (obj3) -> (NULL) Everything seems perfect, but wait, rear is still pointing at obj2, but it is no longer the correct rear! This means that we have to make rear point to obj3. rear = newnode; Now, rear points to obj3. The job is done.
14th Sep 2017, 2:30 AM
Hatsy Rei
Hatsy Rei - avatar
+ 17
We don't do it explicitly as front->next = rear; Imagine when you have only one element. (obj1) -> (NULL) Both front and rear points to the same element, which is obj1. When you add a newnode (obj2) to the queue, what happens in the code is rear->next = newnode; Remember that at this point, rear and front points to the same thing, so we now have (obj1) -> (obj2) -> (NULL) The same thing happens as I have described, we now make rear point to obj2, the latest inserted element. rear = newnode;
14th Sep 2017, 2:36 AM
Hatsy Rei
Hatsy Rei - avatar
+ 13
It looks like something is off. EDIT: Nope, all is fine. void Enqueue(int data) { node *newnode = new node; newnode->data = data; newnode ->next = NULL; if(front==NULL) // no elements in queue. // both front and rear pointers point to the same element, i.e. the first element in the queue is also the last element in the queue. { front=newnode; rear=newnode; } // if there is more than zero elements. // the new element is inserted to the rear of the queue. // After the insertion, we want rear to point to the actual rear, which is the latest element we just added. else { rear->next = newnode; rear = newnode; } }
13th Sep 2017, 5:51 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
When did we assign front->next to rear?
14th Sep 2017, 2:32 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
@Hatsy Rei So, the front->next points to rear? And why do we point both rear and rear->next to the new node?
14th Sep 2017, 1:52 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
Is front linked with rear when we assign the first element to the queue? (i.e. Both point to a common source?) Then why is rear.next still kept as newptr instead of NULL?
12th Sep 2017, 6:31 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
@Hatsy Rei Now I get it! Thank You very much!
14th Sep 2017, 2:48 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar