GATE CSE
First time here? Checkout the FAQ!
x
0 votes
266 views
Which of the following statement is/are correct?

1. If we use only Front pointer in queue, then insertion takes O(n) time while deletion in O(1) time.

2. If we use only Rear pointer in queue, then insertion is O(1) and deletion is not possible.
asked in DS by Loyal (2.6k points)   | 266 views
Tushar.... What type of Data structure you are assuming in thus scenario?
it should be linked list

2 Answers

0 votes

What u are talking about is not implemented in simple queue.

so both are false.

1..If we use only Front pointer in queue, then insertion takes O(n) time while deletion in O(1) time.  flase.

Deletion in O(1) time thats ok but if 1st element insert then u can delete second in O(1) time.

2..If we use only Rear pointer in queue, then insertion is O(1) and deletion is not possible.

insertion not possible b/c u can go in left direction in simple queue.

 

answered by Veteran (47.5k points)  
0 votes

Both 1st and 2nd statements are Correct only if we are using Simple List for queue implementation.

But only 1st statement is Correct if we are using Circular List for queue implementation.

In Simple list, if only FRONT is available we can perform delete operation in O(1) time and insert operation (using an extra pointer temp) in O(n) time.

In Simple list, if only REAR is available we can only perform insertion operation in O(1) time but no delete operation.

Its because, in simple list, Front/Rear pointer only moves in forward direction. Never backward. 

BUT In Circular list, If only FRONT is available again we can perform delete operation in O(1) time and insert operation (using an extra pointer temp) in O(n) time.

While If only REAR is available then both Insertion and deletion can be performed in O(1) time in Circular list.

Example : While using REAR in Circular list.

Insertion : 

newNode --> next = rear --> next;

rear --> next = newNode;

rear = newNode;

Deletion :

temp = rear --> next;

rear --> next = temp --> next;

free(temp);

 

 

 

answered by (487 points)  
edited by


Top Users Jun 2017
  1. Bikram

    3704 Points

  2. Hemant Parihar

    1502 Points

  3. junaid ahmad

    1432 Points

  4. Arnab Bhadra

    1416 Points

  5. Niraj Singh 2

    1391 Points

  6. Debashish Deka

    1246 Points

  7. Rupendra Choudhary

    1194 Points

  8. rahul sharma 5

    1158 Points

  9. Arjun

    956 Points

  10. srestha

    950 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 19 - 25
  1. Bikram

    1960 Points

  2. Niraj Singh 2

    1386 Points

  3. junaid ahmad

    502 Points

  4. Debashish Deka

    414 Points

  5. sudsho

    410 Points


23,373 questions
30,079 answers
67,405 comments
28,396 users