Deque (Double Ended Queue) v Pythonu je implementován pomocí modulu sbírky . Deque je upřednostňován před seznamem v případech, kdy potřebujeme rychlejší operace připojení a pop z obou konců kontejneru, protože deque poskytuje O(1) časovou složitost pro operace připojení a pop ve srovnání se seznamem, který poskytuje časovou složitost O(n).
Typy omezeného vstupu Deque
- Input Restricted Deque : Vstup je omezen na jednom konci, zatímco mazání je povoleno na obou koncích. Output Restricted Deque: výstup je omezen na jednom konci, ale vložení je povoleno na obou koncích.
Příklad: Python kód k demonstraci
Python3
from> collections>import> deque> > # Declaring deque> queue>=> deque([>'name'>,>'age'>,>'DOB'>])> > print>(queue)> |
trojitá zima
>
>Výstup
deque(['name', 'age', 'DOB'])>
Operace na deque
Příklad 1: Efektivní připojení položek
- append() :- Tato funkce se používá k vložení hodnoty do jejího argumentu na pravý konec deque. appendleft() :- Tato funkce se používá k vložení hodnoty do jejího argumentu na levý konec deque.
Python3
# importing 'collections' for deque operations> import> collections> # initializing deque> de>=> collections.deque([>1>,>2>,>3>])> print>(>'deque: '>, de)> # using append() to insert element at right end> # inserts 4 at the end of deque> de.append(>4>)> # printing modified deque> print>(>'
The deque after appending at right is : '>)> print>(de)> # using appendleft() to insert element at left end> # inserts 6 at the beginning of deque> de.appendleft(>6>)> # printing modified deque> print>(>'
The deque after appending at left is : '>)> print>(de)> |
>
>Výstup
deque: deque([1, 2, 3]) The deque after appending at right is : deque([1, 2, 3, 4]) The deque after appending at left is : deque([6, 1, 2, 3, 4])>
Viz konec pro analýzu složitosti.
Příklad 2: Efektivní vytahování položek
- pop() :- Tato funkce se používá k odstranění argumentu z pravého konce deque. popleft() :- Tato funkce se používá k odstranění argumentu z levého konce deque.
Python3
foreach smyčkový strojopis
# importing 'collections' for deque operations> import> collections> # initializing deque> de>=> collections.deque([>6>,>1>,>2>,>3>,>4>])> print>(>'deque: '>, de)> # using pop() to delete element from right end> # deletes 4 from the right end of deque> de.pop()> # printing modified deque> print>(>'
The deque after deleting from right is : '>)> print>(de)> # using popleft() to delete element from left end> # deletes 6 from the left end of deque> de.popleft()> # printing modified deque> print>(>'
The deque after deleting from left is : '>)> print>(de)> |
>
>Výstup
deque: deque([6, 1, 2, 3, 4]) The deque after deleting from right is : deque([6, 1, 2, 3]) The deque after deleting from left is : deque([1, 2, 3])>
Viz konec pro analýzu složitosti.
Příklad 3: Přístup k položkám v deque
- index(ele, beg, end) :- Tato funkce vrací první index hodnoty uvedené v argumentech, počínaje hledáním od začátku do konce indexu. insert(i, a) :- Tato funkce vloží hodnotu uvedenou v arguments(a) do indexu(i) zadaného v argumentech. remove() :- Tato funkce odstraní první výskyt hodnoty uvedené v argumentech. count() :- Tato funkce počítá počet výskytů hodnoty uvedené v argumentech.
Python3
# Python code to demonstrate working of> # insert(), index(), remove(), count()> # importing 'collections' for deque operations> import> collections> # initializing deque> de>=> collections.deque([>1>,>2>,>3>,>3>,>4>,>2>,>4>])> # using index() to print the first occurrence of 4> print> (>'The number 4 first occurs at a position : '>)> print> (de.index(>4>,>2>,>5>))> # using insert() to insert the value 3 at 5th position> de.insert(>4>,>3>)> # printing modified deque> print> (>'The deque after inserting 3 at 5th position is : '>)> print> (de)> # using count() to count the occurrences of 3> print> (>'The count of 3 in deque is : '>)> print> (de.count(>3>))> # using remove() to remove the first occurrence of 3> de.remove(>3>)> # printing modified deque> print> (>'The deque after deleting first occurrence of 3 is : '>)> print> (de)> |
>
>Výstup
The number 4 first occurs at a position : 4 The deque after inserting 3 at 5th position is : deque([1, 2, 3, 3, 3, 4, 2, 4]) The count of 3 in deque is : 3 The deque after deleting first occurrence of 3 is : deque([1, 2, 3, 3, 4, 2, 4])>
Viz konec pro analýzu složitosti.
Příklad 4: Velikost deque
- len(dequeue) :- Vrátí aktuální velikost dequeue.
Python3
# Python Program to demonstrate> # how to find size of a Dequeue> from> collections>import> deque> # initializing deque> de>=> deque([>1>,>2>,>3>,>4>,>5>,>6>])> print>(>'Current Deque: '>, de)> # printing current size of deque> print>(f>'Size of Deque: {len(de)}'>)> # using pop() to delete element from right end> # deletes 6 from the right end of deque> de.pop()> # printing modified deque> print>(>'
The deque after deleting from right is: '>, end>=> '')> print>(de)> # printing current size of deque> print>(f>'Size of Deque: {len(de)}'>)> # This code is contributed by Susobhan Akhuli> |
>
>Výstup
Current Deque: deque([1, 2, 3, 4, 5, 6]) Size of Deque: 6 The deque after deleting from right is: deque([1, 2, 3, 4, 5]) Size of Deque: 5>
Viz konec pro analýzu složitosti.
Příklad 5: Přední a zadní strana deque
- Deque[0] :- K přednímu prvku deque můžeme přistupovat pomocí indexování pomocí de[0]. Deque[-1] :- K zadnímu prvku deque můžeme přistupovat pomocí indexování pomocí de[-1].
Python3
# Python Program to demonstrate> # accessing the front and back of a Deque> from> collections>import> deque> # initializing deque> de>=> deque([>1>,>2>,>3>,>4>,>5>,>6>])> print>(>'Current Deque: '>, de)> # Accessing the front element of the deque> print>(>'Front element of the deque:'>, de[>0>])> # Accessing the back element of the deque> print>(>'Back element of the deque:'>, de[>->1>])> # This code is contributed by Susobhan Akhuli> |
>
>Výstup
Current Deque: deque([1, 2, 3, 4, 5, 6]) Front element of the deque: 1 Back element of the deque: 6>
Viz konec pro analýzu složitosti.
Příklad 6: Různé operace na deque
- extend(iterable) :- Tato funkce se používá k přidání více hodnot na pravý konec deque. Předaný argument je opakovatelný. extendleft(iterable) :- Tato funkce se používá k přidání více hodnot na levý konec deque. Předaný argument je opakovatelný. Pořadí je obráceno v důsledku levého připojení. reverse() :- Tato funkce se používá k obrácení pořadí prvků deque. rotation() :- Tato funkce otočí deque o číslo zadané v argumentech. Pokud je zadané číslo záporné, dojde k rotaci doleva. Jinak je rotace doprava.
Python3
výchozí parametry java
# Python code to demonstrate working of> # extend(), extendleft(), rotate(), reverse()> # importing 'collections' for deque operations> import> collections> # initializing deque> de>=> collections.deque([>1>,>2>,>3>,])> # using extend() to add numbers to right end> # adds 4,5,6 to right end> de.extend([>4>,>5>,>6>])> # printing modified deque> print> (>'The deque after extending deque at end is : '>)> print> (de)> # using extendleft() to add numbers to left end> # adds 7,8,9 to left end> de.extendleft([>7>,>8>,>9>])> # printing modified deque> print> (>'The deque after extending deque at beginning is : '>)> print> (de)> # using rotate() to rotate the deque> # rotates by 3 to left> de.rotate(>->3>)> # printing modified deque> print> (>'The deque after rotating deque is : '>)> print> (de)> # using reverse() to reverse the deque> de.reverse()> # printing modified deque> print> (>'The deque after reversing deque is : '>)> print> (de)> |
>
>Výstup
The deque after extending deque at end is : deque([1, 2, 3, 4, 5, 6]) The deque after extending deque at beginning is : deque([9, 8, 7, 1, 2, 3, 4, 5, 6]) The deque after rotating deque is : deque([1, 2, 3, 4, 5, 6, 9, 8, 7]) The deque after reversing deque is : deque([7, 8, 9, 6, 5, 4, 3, 2, 1])>
Viz konec pro analýzu složitosti.
Analýza složitosti:
| Metody | Časová složitost | Pomocný prostor |
|---|---|---|
| připojit() | O(1) | O(1) shreya ghoshal |
| appendleft() | O(1) | O(1) |
| pop() | O(1) | O(1) |
| popleft() | O(1) | O(1) |
| index (ele, začátek, konec) | NA) | O(1) |
| vložit (i, a) | NA) | O(1) |
| odstranit() | NA) | O(1) |
| počet() | NA) | O(1) |
| jen(dequeue) | O(1) večeře vs čas večeře | O(1) |
| Deque[0] | O(1) | O(1) |
| Deque[-1] | O(1) | O(1) |
| rozšířit (opakovatelné) | ŠIPKA) | O(1) |
| prodloužit doleva (iterovatelný) | ŠIPKA) | O(1) |
| zvrátit() | NA) | O(1) |
| točit se() | ŠIPKA) | O(1) |