Lecture Notes On List, Stack and Queue
Lecture Notes On List, Stack and Queue
Algorithm
Find(52)
Insert(X, 3)
Delete(52)
A linked list
A1 A2 A3 A4 A5
X
Linked Lists
There are several places where you are likely to go
wrong:
(1) There is no really obvious way to insert at the front of the list from the definitions given;
(2) Deleting from the front of the list is a special case, because it changes the start of the list; careless
coding will lose the list;
(3) A third problem concerns deletion in general. Although the pointer moves above are simple, the
deletion algorithm requires us to keep track of the cell before the one that we want to delete.
Linked Lists
One simple change solves all three problems. We will
keep a sentinel node, referred to an a header or
dummy node.
Header A1 A2 A3 A4 A5
A1 A2 A3 A4 A5
It can also be done with doubly linked lists, the first cell’s
previous pointer points to the last cell.
A1 A2 A3 A4 A5
P1
An alternative is to use a singly linked list.
Each term in the polynomial is contained in one cell, and
the cells are sorted in decreasing order of exponents.
Stack ADT
A stack is a list with the restriction that insertions and
deletions can be performed in only one position,
namely, the end of the list, called the top.
Top 7
Function calls.
The Queue ADT
Like stacks, queues are lists.
5 2 7 1
Front Rear
Array Implementation of
Queues
To Enqueue an element X, we increment Size and
Rear, then set Queue[Rear]=X.
Enqueue (1) 1 2 4
Rear Front
Enqueue (3) 1 3 2 4
Rear Front
Dequeue, which 1 3 2 4
returns 2 Rear Front
Dequeue, which 1 3 2 4
returns 4 Front Rear
Dequeue, which 1 3 2 4
returns 1 Rear
Front
Dequeue, which
returns 3 and makes 1 3 2 4
the Queue empty Rear Front
Linked List
Implementation of Queues
Front Header
……
Rear /
Linked List
Implementation of Queues
Front
Empty Queue /
Reart
Front
Enqueue x x /
Reart
Front
Enqueue y x y /
Reart
Front
Dequeue x x y /
Reart
Example Applications
When jobs are submitted to a printer, they are
arranged in order of arrival.
Every real-life line is a queue. For instance, lines at
ticket counters are queues, because service is first-
come first-served.
A whole branch of mathematics, known as
queueing theory, deals with computing,
probabilistically, how long users expect to wait on a
line, how long the line gets, and other such
questions.
Homework of Chapter 3
Exercises:
3.2 (Don’t need to analyze the running time.)
3.3
3.4
3.5
3.21
3.25