Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.

Similar presentations


Presentation on theme: "Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists."— Presentation transcript:

1 Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists

2 Recap: Array-based Stack in Python

3 Recap: Queue in Python (1/2)

4 Recap: Queue in Python (2/2)

5 Assignment #5: Stacks and Queues
R-6.5 Implement a function that reverses a list of elements by pushing them onto a stack in one order, and writing them back to the list in reversed order. R-6.13 Suppose you have a deque D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty queue Q. Give a code fragment that uses only D and Q (and no other variables) and results in D storing the elements in the order (1,2,3,5,4,6,7,8). R-6.14 Repeat the previous problem using the deque D and an initially empty stack S. Find spans in an array. Given an array arr[], the SPAN s[i] of arr[i] is the maximum number of consecutive elements arr[j] immediately before arr[i] such that arr[j] <= arr[i]. (C++ codes: spans1.cpp, spans2.cpp ). Implement both the spans1 which runs in O(n2) time and the spans2 which runs in O(n) time in Python.

6 R6.5 reverses a list of elements by pushing them onto a stack in one order, and writing them back to the list in reversed order.

7

8 R-6.13 Suppose you have a deque D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty queue Q. Give a code fragment that uses only D and Q (and no other variables) and results in D storing the elements in the order (1,2,3,5,4,6,7,8).

9 R-6.14 Repeat the previous problem using the deque D and an initially empty stack S.

10 Find spans in an array Sequences 8/1/2018 4:38 AM

11 Find spans in an array Sequences 8/1/2018 4:38 AM

12 Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes, starting from a head pointer Each node stores element link to the next node next node elem head A B C D Linked Lists

13 The Node Class for List Nodes
Sequences 8/1/2018 4:38 AM The Node Class for List Nodes By default, Python represents each namespace with an instance of the built-in dict class (see Section 1.2.3) that maps identifying names in that scope to the associated objects. provide a class-level member named slots that is assigned to a fixed sequence of strings that serve as names for instance variables. By default, Python represents each namespace with an instance of the built-in dict class (see Section 1.2.3) that maps identifying names in that scope to the associated objects. provide a class-level member named slots that is assigned to a fixed sequence of strings that serve as names for instance variables. Linked Lists

14 Inserting at the Head Allocate a new node Insert new element
Have new node point to old head Update head to point to new node Linked Lists

15

16 Removing at the Head Update head to point to next node in the list
Allow garbage collector to reclaim the former first node Linked Lists

17

18 Inserting at the Tail Allocate a new node Insert new element
Have new node point to null Have old last node point to new node Update tail to point to new node Linked Lists

19

20 Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node Linked Lists

21 Stack as a Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time nodes t elements Linked Lists

22 Linked-List Stack in Python

23 Queue as a Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f elements Linked Lists

24 Linked-List Queue in Python
supporting worst-case O(1)-time for all operations Linked Lists

25 Application: Round Robin Schedulers
We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps: e = Q.dequeue() Service element e Q.enqueue(e) Queue Dequeue Enqueue Shared Service © 2013 Goodrich, Tamassia, Goldwasser Queues

26 Queue with a Circularly Linked List in Python


Download ppt "Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists."

Similar presentations


Ads by Google