Deque (podwójnie zakończona kolejka) w Pythonie jest implementowana za pomocą modułu zbiory . Deque jest preferowane zamiast listy w przypadkach, gdy potrzebujemy szybszych operacji dołączania i wyskakiwania z obu końców kontenera, ponieważ deque zapewnia O(1) złożoność czasowa dla operacji dołączania i otwierania w porównaniu z listą, która zapewnia złożoność czasową O(n).
Rodzaje ograniczonego wejścia Deque
- Ograniczenie wejścia: Wejście jest ograniczone z jednej strony, a usuwanie jest dozwolone z obu końców. Output Restricted Deque: wyjście jest ograniczone na jednym końcu, ale wkładanie jest dozwolone na obu końcach.
Przykład: kod Pythona do zademonstrowania
Python3
from> collections>import> deque> > # Declaring deque> queue>=> deque([>'name'>,>'age'>,>'DOB'>])> > print>(queue)> |
>
>Wyjście
deque(['name', 'age', 'DOB'])>
Operacje na deque
Przykład 1: Efektywne dodawanie elementów
- append() :- Ta funkcja służy do wstawiania wartości w argumencie na prawym końcu deque. appendleft() :- Ta funkcja służy do wstawiania wartości w argumencie na lewym końcu 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)> |
>
połączenie ciągu Java
>Wyjście
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])>
Zapoznaj się z końcem analizy złożoności.
Przykład 2: Efektywne otwieranie przedmiotów
- pop() :- Ta funkcja służy do usuwania argumentu z prawego końca deque. popleft() :- Ta funkcja służy do usuwania argumentu z lewego końca deque.
Python3
# 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)> |
>
>Wyjście
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])>
Zapoznaj się z końcem analizy złożoności.
Przykład 3: Dostęp do elementów w deque
- indeks(ele, beg, koniec) :- Ta funkcja zwraca pierwszy indeks wartości podanej w argumentach, rozpoczynając wyszukiwanie od początku do końca indeksu. wstaw(i, a) :- Ta funkcja wstawia wartość wymienioną w argumentach(a) w indeksie(i) określonym w argumentach. usuń() :- Ta funkcja usuwa pierwsze wystąpienie wartości wymienionej w argumentach. count() :- Ta funkcja zlicza liczbę wystąpień wartości podanej w argumentach.
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)> |
>
>Wyjście
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])>
Zapoznaj się z końcem analizy złożoności.
Przykład 4: Rozmiar deque
- len(dequeue) :- Zwraca bieżący rozmiar usuwania z kolejki.
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> |
>
>Wyjście
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>
Zapoznaj się z końcem analizy złożoności.
Przykład 5: Przód i tył deque
- Deque[0] :- Możemy uzyskać dostęp do przedniego elementu deque za pomocą indeksowania za pomocą de[0]. Deque[-1] :- Możemy uzyskać dostęp do tylnego elementu deque za pomocą indeksowania za pomocą de[-1].
Python3
rstrip Pythona
# 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> |
>
>Wyjście
Current Deque: deque([1, 2, 3, 4, 5, 6]) Front element of the deque: 1 Back element of the deque: 6>
Zapoznaj się z końcem analizy złożoności.
Przykład 6: Różne operacje na deque
- Extend(iterable) :- Ta funkcja służy do dodawania wielu wartości na prawym końcu deque. Przekazany argument jest iterowalny. Extendleft(iterable): - Ta funkcja służy do dodawania wielu wartości na lewym końcu deque. Przekazany argument jest iterowalny. Kolejność jest odwrócona w wyniku lewego dołączenia. Reverse() :- Ta funkcja służy do odwracania kolejności elementów deque. obróć() :- Ta funkcja obraca deque o liczbę podaną w argumentach. Jeśli określona liczba jest ujemna, obrót następuje w lewo. W przeciwnym razie obrót jest w prawo.
Python3
# 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)> |
>
>Wyjście
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])>
Zapoznaj się z końcem analizy złożoności.
Analiza złożoności:
| Metody | Złożoność czasu | Przestrzeń pomocnicza |
|---|---|---|
| dodać() | O(1) | O(1) |
| dodatek po lewej() ciąg długości | O(1) | O(1) |
| Muzyka pop() | O(1) | O(1) |
| popleft() | O(1) | O(1) |
| indeks (ele, błaganie, koniec) | NA) | O(1) |
| wstaw (i, a) | NA) | O(1) |
| usunąć() | NA) | O(1) |
| liczyć() | NA) tablica zwrotna Java | O(1) |
| po prostu (usuń z kolejki) | O(1) | O(1) |
| Deque[0] | O(1) | O(1) |
| Deque[-1] | O(1) | O(1) |
| rozszerzać (iterowalny) | STRZAŁKA) | O(1) |
| przedłuż w lewo (iterowalny) | STRZAŁKA) | O(1) |
| odwracać() | NA) | O(1) |
| obracać się() | STRZAŁKA) | O(1) |