Array queue and linked list queue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.