Lecture 4: Linked Lists,
Stacks and Queues
Instructor: Dr. Wenjia Li
Implementing A Linked List
To create a doubly linked list as seen below
Sentinel nodes at head and tai
Lecture 3: The List ADT
Instructor: Dr. Wenjia Li
A list is a dynamic ordered tuple of homogeneous
Ao, A1, A2, , AN-1
where Ai is the i-th element of the list
The position of element Ai is i; positions range from 0
Wild wood apartment scenario:
Complexes- Complex locations, Type of complexes, occupancy
Apartment details Type, Location of apartment , Rent, nature of lease
Manager details for each apartment apartment, location, sti
After thinking a little bit more about the project I decided to change the description of the project a little
bit. I wanted to use this project in order to support an application that Im building outside of class. The
application will be a stock
Transaction: action, or series of actions, carried out by user or
application, which reads or updates contents of database.
Transaction support: transforms database from one consistent state to
another, although consistency may be violated during transact
Show that in the recurrence
= ! +
We assume n = c2*n and we guess T(n) c*n2 2n:
T(n)=max c q! 2q + c n q 1 ! 2 + 2 + 2 + (n)
= cmax q! + (n q 1)! 2 + 2 + (n)
We have proved that in Exercise 7.4.3 that q2 + (n-q-1)2
Show that quicksort's best-case running time is (nlgn).
Let T (n) be the best-case time for the procedure QUICKSORT on an input of size n.
We have the recurrence:
T(n)= min(T(q)+T(nq1)+ (n)
We guess T (n) cn lg, Thus:
T(n) min (c
Show that the running time of QUICKSORT is (n2)when the array A contains
distinct elements and is sorted in decreasing order.
In the textbook, since we choose the last element as the pivot, all other elements are
greater than the pivot in the proces
What is the running time of QUICKSORT when all elements of array A have the
Answer: It is (n2)
Since one of the partitions is always empty.
So T(n) = T(n 1) + ()
We have proved that T(n) = (! ) in earlier exercise.
Thus, the running time
Show that the expression q2 + (n-q-1)2 achieves a maximum over q= 0,1, n-1 when
q=0 or q=n-1.
f(q)= q2 + (n-q-1)2
f(q) = 2q2(nq1)=4q2n+2
f(q)=0 when q=!n!. f(q) is also continious.
q:f(q)>0, which means that f(q)=0 is a local m
6-2 Analysis of d-ary heaps
A d-ary heap is like a binary heap, but (with one possible exception) non-leaf nodes have d
children instead of 2 children.
a. How would you represent a d-ary heap in an array?
A d-ary heap can be represented in a 1-dimensional
Is the array with values cfw_23, 17, 14, 6, 13, 10, 1, 5, 7, 12 a max-heap?
After we represent this array to a heap. As we can see in the graph above, this array is not a maxheap because there is one violation between 6 and 7.
Where in a max-heap might the smallest element reside, assuming that all
elements are distinct?
The smallest element in a max-heap is in any of the leaves.
According to the definition of a max-heap property:
In a max-heap, the max-heap property is t