Linear Data Structure
Linear Structures
Once an item is added, it stays in that position relative to the other elements that came before and came after it. * stacks * queues * deques * lists
Stack abstract data type
Satck()
return empty stackpush(item)
return nothingpop()
return the itempeek()
returns the top item from the stack but does not remove it.isEmpty()
size()
The implementation is:
1 | class Stack: |
Queue abstract data type
Queue()
return empty queueenqueue(item)
dequeue()
isEmpty()
size()
The implementation is: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16class Queue:
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)
deque
A deque, also known as a double-ended queue, is an ordered collection of items similar to the queue
Deque()
return empty dequeaddFront(item)
addRear(item)
removeFront()
return the itemremoveRear()
return the itemisEmpty()
size()
Lists
The Unordered List
A list is a collection of items where each item holds a relative position with respect to the others. More specifically, we will refer to this type of list as an unordered list.
List()
creates a new list that is empty. It needs no parameters and returns an empty list.add(item)
adds a new item to the list. It needs the item and returns nothing. Assume the item is not already in the list.remove(item)
removes the item from the list. It needs the item and modifies the list. Assume the item is present in the list.search(item)
searches for the item in the list. It needs the item and returns a boolean value.isEmpty()
tests to see whether the list is empty. It needs no parameters and returns a boolean value.size()
returns the number of items in the list. It needs no parameters and returns an integer.append(item)
adds a new item to the end of the list making it the last item in the collection. It needs the item and returns nothing. Assume the item is not already in the list.index(item)
returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list.insert(pos,item)
adds a new item to the list at position pos. It needs the item and returns nothing. Assume the item is not already in the list and there are enough existing items to have position pos.pop()
removes and returns the last item in the list. It needs nothing and returns an item. Assume the list has at least one item.pop(pos)
removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list.
Implementation Linked Lists 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66class 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
def setNext(self, newNode):
self.next = newNode
class UnorderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def add(self, item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def size(self):
count = 0
current = self.head
while current != None:
current = current.getNext()
count += 1
return count
def search(self, item):
current = self.head
found = False
while current != None:
if item == current.getData():
found = True
break
else:
current = current.getNext()
return found
def remove(self, item):
current = self.head
found = False
previous = None
while current != None:
if item == current.getData():
break
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
Modify the append method to be O(1)
The Ordered List
The structure of an ordered list is a collection of items where each item holds a relative position that is based upon some underlying characteristic of the item.
OrderedList()
creates a new ordered list that is empty. It needs no parameters and returns an empty list.add(item)
adds a new item to the list making sure that the order is preserved. It needs the item and returns nothing. Assume the item is not already in the list.remove(item)
removes the item from the list. It needs the item and modifies the list. Assume the item is present in the list.search(item)
searches for the item in the list. It needs the item and returns a boolean value.isEmpty()
tests to see whether the list is empty. It needs no parameters and returns a boolean value.size()
returns the number of items in the list. It needs no parameters and returns an integer.index(item)
returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list.pop()
removes and returns the last item in the list. It needs nothing and returns an item. Assume the list has at least one item.pop(pos)
removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list.
implement
1 | from Node import Node |
about the do-while loop in Python, there is no do-while loop in Python, so when can just do as this:
1 | while True: |
equals: 1
2
3do {
stuff()
} while (condition())