2 - Linear Data Structures PDF
2 - Linear Data Structures PDF
2:
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Stack
Newest element
Oldest element
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Stack
3 Push
3
2 Push
2 2
1 1 1
Pop 3
2
Pop 2
1 1 1
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Stack
Operation Effect
Stack() Creates a new empty stack
pop() Removes the top item from the stack and returns it
peak() Returns the top item from the stack but does not remove it
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Stack
Example
Operation Stack content Returned value
s = Stack() []
s.isEmpty() [] True
s.push(2) [2]
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Stack
A possible Python implementation
class Stack:
def __init__(self):
self.items = []
def pop(self):
Remove and return the last
return self.items.pop()
element
def peek(self):
if len(self.items)>0: Return the last element, but
return self.items[-1] does not remove it
def size(self):
return len(self.items)
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Example: Balanced parentheses
For example:
“( () () )” is correct
“( ( ( ( ) ” is not correct
“( [ ] { } )” is correct
“( [ [ { } ) } ” is not correct
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Example: Balanced parentheses
def parChecker(symbols_string):
s = Stack()
index = 0
if s.isEmpty():
return True
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Queue
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Queue
Enqueue E D C B A
F E D C B A
Dequeue
F E D C B
A
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Queue
Operation Effect
Queue() Creates a new empty queue
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Queue
Example
q = Queue() []
q.isEmpty() [] True
q.enque(2) [2]
q.enque(‘hello’) [‘hello’, 2]
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Queue
class Queue:
def __init__(self):
self.items = []
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Queue
Comparison with Stack implementation
def peek(self):
if len(self.items)>0:
return self.items[-1]
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Example: simulate a print queue
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Example: simulate a printer
class Printer:
def __init__(self, max_queue_len):
self.printQueue = Queue()
self.max_queue_len = max_queue_len
def completeTask(self):
if not self.printQueue.isEmpty():
print("Task", self.printQueue.dequeue(), " printed")
myprinter = Printer(max_queue_len)
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Example: simulate a printer
2 1
2 1 Task 1 enqueued
Task 2 enqueued
Task 1 printed
Task 3 enqueued
2 1 Task 2 printed
Task 4 enqueued
Task 5 enqueued
Task 6 enqueued
Task 7 enqueued
The queue is full, task 8 refused
8 7 6 5 4 3 Task 3 printed
…
7 6 5 4 3
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Priority Queue
New element
F
E D C B A F E D C B A
Standard queue
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Priority Queue
Coming soon…
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Deque
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Deque
Operation Effect
Deque() Creates a new empty deque
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Deque
Example
d = Deque() []
d.isEmpty() [] True
d.addRear(2) [2]
d.addRear(‘hello’) [‘hello’, 2]
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Deque
class Deque:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def size(self):
return len(self.items)
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Example: Palindrome-checker
For example:
“radar” is a palindrome
“home ” is not a palindrome
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Example: Palindrome-checker
def palchecker(aString):
charDeque = Deque()
for ch in aString:
charDeque.addRear(ch)
return True
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Arrays
elements
10 4 12 75 30 1 27
indexes 0 1 2 3 4 5 6
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Arrays
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Lists
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Lists
Common operations of Unordered List ADT
Operation Effect
List() Creates a new empty list
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Lists
Common operations of Ordered List ADT
Operation Effect
OrderedList() Creates a new empty ordered list
Adds a new item to the list making sure that the order is
add(item)
preserved
remove(item) Remove the item from the list
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Lists
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List
Each node stores the data and a pointer to the next node
Head Tail
10 25 3 40 12
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List
10 25 3 40 12
10 25 3 40 12
10 25 3 40 12
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (unordered)
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (unordered)
class Node:
Node def __init__(self,initData):
self.data = initData
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newData):
self.data = newData
class UnorderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
….
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (unordered)
class UnorderedList:
def __init__(self):
self.head = None
…. add(self, item):
def
temp = Node(item)
temp.setNext(self.head)
self.head = temp
….
NOTE: a common variation of linked list keeps track of both head and tail position, in
that case it is equivalent to insert an element at the beginning or at the end of the list
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (unordered)
class UnorderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
…. size(self):
def
current = self.head
count = 0
while current is not None:
count = count + 1
current = current.getNext()
return count
….
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (unordered)
….
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (unordered)
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (unordered)
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (unordered)
class UnorderedList:
…
How to remove an element in the list
def remove(self, item):
current = self.head
previous = None
found = False
if found:
if previous is None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
Removing the first node …
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (ordered)
class OrderedList:
How to search an element in the list
…
return False
…
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Linked List (ordered)
class OrderedList:
temp = Node(item)
if previous is None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Lists – computational complexity
Unordered Ordered
Operation Python list
linked list linked list
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Implementing a Stack with a Linked List
TOP
26
31
54
77
Adding and removing an element from the Stack is equivalent to adding
and removing a node from the head of the Linked List 93
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Implementing a Stack with a Linked List
Implementation with Python List Implementation with Linked List
class Stack: class Node: class Stack:
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Implementing a Stack with a Linked List
Implementation with Python List Implementation with Linked List
class Stack: class Node: class Stack:
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Implementing a Stack with a Linked List
Implementation with Python List Implementation with Linked List
class Stack: class Node: class Stack:
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Implementing a Stack with a Linked List
Implementation with Python List Implementation with Linked List
class Stack: class Node: class Stack:
….
def __init__(self): def __init__(self, data):
self.items = [] self.data = data def push(self, data):
self.next = None if self.head is None:
def isEmpty(self): self.head = Node(data)
return self.items == [] else:
new_node = Node(data)
def size(self): new_node.next = self.head
return len(self.items) self.head = new_node
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Implementing a Stack with a Linked List
Implementation with Python List Implementation with Linked List
class Stack: class Node: class Stack:
….
def __init__(self): def __init__(self, data):
self.items = [] self.data = data def push(self, data):
self.next = None if self.head is None:
def isEmpty(self): self.head = Node(data)
return self.items == [] else:
new_node = Node(data)
def size(self): new_node.next = self.head
return len(self.items) self.head = new_node
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Implementing a Stack with a Linked List
Implementation with Python List Implementation with Linked List
class Stack: class Node: class Stack:
….
def __init__(self): def __init__(self, data):
self.items = [] self.data = data def push(self, data):
self.next = None if self.head is None:
def isEmpty(self): self.head = Node(data)
return self.items == [] else:
new_node = Node(data)
def size(self): new_node.next = self.head
return len(self.items) self.head = new_node
Piercarlo Dondi – Algorithms and Data Structures: Linear Data Structures – A.Y. 2022/23
Questions?