-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path75.queue_intro.cpp
59 lines (52 loc) · 1.1 KB
/
75.queue_intro.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//works on principle of fifo
//has two ends - front and end
//insertion is done from end
//deletion is done from front
/**
* QUEUE ADT
*
* Data :
* 1. space for storing elements
* 2. front - for deletion
* 3. rear pointer - insertion
*
* Operations:
* 1.enqueue(x)
* 2.dequeue()
* 3.isEmpty()
* 4.isFull()
* 5.first()
* 6.last()
*
* Can be implemented using 2 physical data structures:
* Arrays and Linked List
*/
/**
* USING SINGLE POINTER
* array of fixed size
* one pointer - rear only
*
* process - initiate rear=-1, enqueue(x);rear++;
*
* Insertion - O(1)
*
* Disadvantage -
* cannot delete elements efficiently - delete first element -> shift all elements to left for filling holes - O(n)
*
*/
/**
* USING TWO POINTERS
* array of fixed size
* two pointers - front and rear
*
* process - rear=front=-1; if(enqueue(x)) rear++; if(dequeue()) front++; delete that element.
*
* Insertion - O(1)
* Deletion - O(1)
*
*
* queue is empty -> front == rear (front is pointting prior to first element)
* queue is full -> rear == size-1
* Disadvantage -
*
*/