How to create, traverse, add data to and remove data from the data structures mentioned above. (NB this can be either using arrays and procedural programming or an object-oriented approach)
1.4.2 Data Structures
Practise exam questionsData Structures — Note 3 of 3
Candidates should have experience of implementing these structures in a variety of contexts, for example through a procedural program, through a different data structure and through an object-oriented approach. Candidates need to be able to read, trace and write code to implement features of these data structures. (Again we would recommend a general understanding backed up with practice implementing them, rather than trying to memorise code patterns).
You need to be able to read, trace and write code that creates and manipulates linked lists, stacks, queues, trees, graphs and hash tables. This can be done procedurally using arrays and pointers, or using an object-oriented approach.
MOD.array stack[10]
top = -1
procedure push(item)
if top == 9 then
print("Stack overflow")
else
top = top + 1
stack[top] = item
endif
endprocedure
function pop()
if top == -1 then
print("Stack underflow")
return null
else
item = stack[top]
top = top - 1
return item
endif
endfunctionSIZE = 5
queue = [None] * SIZE
front, rear, count = 0, -1, 0
def enqueue(item):
global rear, count
if count == SIZE:
print("Queue full")
return
rear = (rear + 1) % SIZE # wrap around
queue[rear] = item
count += 1
def dequeue():
global front, count
if count == 0:
print("Queue empty")
return None
item = queue[front]
front = (front + 1) % SIZE # wrap around
count -= 1
return itemclass Node:
def __init__(self, data):
self.data = data
self.next = None
def traverse(start):
current = start
while current is not None:
print(current.data)
current = current.next
def insert_after(node, data):
new_node = Node(data)
new_node.next = node.next # new node points to the old next
node.next = new_node # previous node points to the new nodeReady to test yourself?
Practise 1.4.2 questions