You are on page 1of 7

1

COEN352
Data structure and algorithms
Tutorial 3
Tutor: May El Barachi
elbar_m@encs.concordia.ca
http://users.encs.concordia.ca/~elbar_m/
2
Stacks
A stack is a container of objects that are inserted
and removed according to the LIFO principle
Real life example: Plate dispenser
Uses in computer engineering:
Browsers and the back button
Text editors and undo operation
Two main operations:
Push: Inserts an object on the top of the stack
Pop: Remove and return the top object on the stack
Array-based implementation of stack:
2
3
Queues
A queue is a container of objects that are inserted
and removed according to the FIFO principle
Real life example: waiting line in a shop
Uses in engineering:
Call center
Two main operations:
enqueue: Inserts an object at the rear of the queue
dequeue: Remove and return the object at the front of the
queue
Array-based implementation of queue:
4
Array implementations of stacks
and queues
Array implementations of stack and queue
are straightforward, but have one drawback:
The size N of the array must be fixed in advance. These
implementations are not very adaptable.
Need to explore an alternate implementation.
3
5
Singly linked lists
In its simplest form, a linked list is a collection of
nodes that form a linear ordering together.
Each node is a compound object that stores a data
member (called its element) and a pointer (called
next) to the next node in the list.
6
Consider a linked list that contains 3-character strings of common
airport abbreviations.
Moving from one node to another by following a next pointer is
known as link hopping or pointer hopping.
The first and last node is a linked list are called the head and tail of
the list, respectively.
There is also a HEAD and TAIL pointer variable.
Singly linked lists
4
7
More flexible type of linked list that allows moving forward and
backward in the list
Each node has two pointers, one pointing to the next element, and
the other to the previous element
We use two special nodes (header and trailer) to keep track of the
beginning and the end of the list
Doubly linked list
8
Exercise R- 4.11
Describe how to insert an element at the beginning of a singly linked list,
assuming that the list does not have a sentinel header node, and instead
uses a pointer variable head to point to the first node in the list.
Solution:
- Create a new node with next pointing to head.
- Set head pointing to the new node.
new A
head
new A
head
5
9
Exercise C- 4.2
Suppose we are given an n-element array A whose elements are taken from
the set {1,2,,n-1}. Describe a recursive algorithm for finding a repeated
element in A. What is the running time of your algorithm?
Solution:
- Initialize an array B with size n-1 and all zero elements.
- Scan array A from the index 0 towards n-1. For each element in A, check
whether the corresponding element in B is one.
- For example, if A[n-1]=7, then check whether B[6] is equal to 1. If yes,
then 7 is a repeated element in A; if no, assign 1 to B[6].
The running time complexity of this algorithm is O(n).
10
Code:
Algorithm FindRepeatedElement(n, B)
Input: Array A, Array B with size n-1, and all its elements are set to 0,
repeatedElement = -1
Output: repeatedElement
if n = 1
return repeatedElement
else
if (B[A[n-1]- 1] = 0)
B[A[n-1]- 1] = 1
FindRepeatedElement(n-1, B)
else
repeatedElement = A[n-1]
return repeatedElement
Exercise C- 4.2
6
11
Exercise C- 4.8
Describe , in pseudo-code, a non-recursive method for finding , by link
hopping, the middle node of a doubly linked list with header and trailer
sentinels, and an odd number of real nodes between them (must only use
link hopping). What is the running time of this method?
Solution:
Run a for loop, which begins at the two ends towards the middle.
Finally, they arrive at the same element, which is the middle element.
The running time of this method is O(n) .
12
Exercise C- 4.8
Describe , in pseudo-code, a non-recursive method for finding , by link
hopping, the middle node of a doubly linked list with header and trailer
sentinels, and an odd number of real nodes between them(must only use
link hopping). What is the running time of this method?
Algorithm: FindMiddleNode(L, tmp_h, tmp_t)
Input: A double linked list L with size n,
tmp_h = header next, tmp_t = trailer prev
Output: Middle node of L
for (i = 0; i < (n+1)/2; i++)
if L[tmp_h] = L[tmp_t]
return L[tmp_h]
else
tmp_h = tmp_h next
tmp_t = tmp_t prev
7
13
Exercise C- 4.9
Describe how to implement the queue ADT using two stacks. What is the running
time of the enqueue() and dequeue() functions in this case?
Solution:
To implement a queue using two stacks, stack1 and stack2, we can do the following:
on enqueue() : push the element onto stack1.
on dequeue() : pop each element from stack1 and push it onto stack2, until the
last element is reached. Temporaril y store this last element. Pop every- thing o of
stack2 and push them back onto stack1. Return the last element.
Enqueue takes 0(1) running time and dequeue takes 0(n) running time
14
Exercise C- 4.11
Give an algorithm for concatenating two doubl y linked lists L and M ,with
header and trailer sentinel nodes, into a single list L , as in the previous
exercise. What is the running time of this method?
Solution:
We have to use two temporary Node elements, temp1 and temp2.
- Initialize temp1 to be the trailer node of L and temp2 to be the header node of
M.
- Make the element of temp1 have its next field point to temp2 and set the
element of temp2 to have its prev field point to temp1.
- Set to be L and then set the trailer node of to be the trailer node of M.
This method takes constant, or O(1), amount of time.

You might also like