After some experiments, I feel that using a linked list to implement queues is better than using arrays to implement queues.
Comparison of inbound and outbound operations per second
Array queue
Enqueue |
37,037 |
Dequeue |
4,166,666 |
Linked List queue
Enqueue |
277,778 |
Dequeue |
666,667 |
The running time of the first n queues and then n queues is compared, in seconds.
Number of incoming/outgoing queues | running time of the array queue | running time of the linked list queue
1,000 |
0.01 |
0.01 |
10,000 |
0.04 |
0.04 |
100,000 |
2.7 |
0.4 |
1,000,000 |
|
4 |
In the last group, the array queue does not run for half a day.
Below is the code
Class arrayqueue:
Def _ init _ (Self ):
Self. Items = []
Def isempty (Self ):
Return self. Items = []
Def enqueue (self, item ):
Self. Items. insert (0, item)
Def dequeue (Self ):
Return self. Items. Pop ()
Def size (Self ):
Return Len (self. Items)
Class node:
Def _ init _ (self, initdata ):
Self. Data = initdata
Self. Next = none
Self. Prev = none
Class upload listqueue:
Def _ init _ (Self ):
Self. Head = none
Self. Rear = none
Self. size = 0
Def isempty (Self ):
Return self. size = 0
Def size (Self ):
Return self. Size
Def enqueue (self, item ):
Self. Size + = 1
Pre = self. Head
Self. Head = node (item)
Self. Head. Next = pre
If pre! = None: Pre. Prev = self. Head
Else: Self. Rear = self. Head
Def dequeue (Self ):
If self. size = 0: Return none
Self. Size-= 1
Rmdata = self. Rear. Data
If self. Rear. Prev! = None:
Self. Rear = self. Rear. Prev
Return rmdata
Array queue and linked list queue