Chapter 3
Chapter 3
Complexity: O(1)
Adding at kth position
Move all elements one position behind, from kth
position onwards;
Add the element at the kth position
Increment current size by 1;
Complexity: O(n)
Deleting at the End
A[0] A[0]-1;
Complexity: O(1)
Deleting at the k th position
Move all elements one position ahead, from
(k+1)th position onwards;
Decrement the current size by 1;
Complexity: O(n)
Accessing an Element at the
th
k position
A[k];
O(1) operation;
Linked List
• To avoid the linear cost of insertion and deletion,
we need to ensure the list not contiguously stored
• Otherwise, entire parts of the list will need to be
moved.
• A Linked List consists of a sequence of nodes
• A node in the linked list consists of two elements
o Value of the corresponding element in the list
o Pointer to the next element nextptr.
• In last element, the pointer is a null pointer nullptr
• A pointer to the first node referred to as head, and,
possibly, a pointer to the last node, the tail.
Head
3 5 2
10 NULL
Tail
struct ListNode {
int value;
ListNode *next;
};
Adding an element at the beginning
• Create a new node;
• Assign the value of the new element to the value part
in this node;
• Assign the head ptr content to this new node pointer
(i.e. make it point to the first list node);
• Make the head pointer point to the new node;
ListNode* newptr = new ListNode
newptr value = val;
newptr next = head; O(1)
head = newptr;
Adding an element at the end
• Create a new node;
• Assign the value of the new element to the value part
in this node;
• Assign to the ptr of the last node the address of the
new node (i.e. content of newptr);
• Make the tail pointer point to the new node;
Complexity: O(n)
Adding at the k th position
ListNode* newptr = new ListNode;
newptr value = val;
newptr >next = NULL;
ListNode* current = head;
for (count = 1; count < k-1 && current != 0; count++)
current = current next;
if (count == k-1 ) {
newptr next = current next;
current next = newptr;
return true;
} Complexity: O(n)
else
cout << “position out of range” ;
return;
Deleting a node at the kth position
ListNode* current = head;
ListNode* prevPtr;
for (count = 1; count < k-1 && current != 0; count++) {
prevPtr = current;
current = current next; }
if (count == k-1 ) {
prevPtr next = current next;
delete current;
return true;
}
else
return false; Complexity: O(n)
Delete a node at the kth position
Complexity depends on access complexity
Complexities?
Head
NULL 3 5 2
10 NULL
Tail
Advantages Of DLLs
• Reversing the doubly linked list is very easy.
• It can allocate or reallocate memory easily during its
execution.
• As with a singly linked list, it is the easiest data
structure to implement (not considering arrays).
• The traversal of this doubly linked list is bidirectional
which is not possible in a singly linked list.
• Deletion of nodes is easy as compared to a Singly
Linked List. A singly linked list deletion requires a
pointer to the node and previous node to be deleted but
in the doubly linked list, it only requires the pointer
which is to be deleted.
Disadvantages of DLLs
• It uses extra memory when compared to the
array and singly linked list.
• Since the elements in memory are stored
randomly, the elements are accessed
sequentially; no direct access is allowed.
Uses of DLLs
• Used by the browser to implement backward and forward
navigation of visited web pages (back and forward buttons)
• Used by various applications to implement undo and redo
functionality.
• Also in many operating systems, the thread scheduler (the
code that chooses what process needs to run at which time)
maintains a doubly-linked list of all processes running at
that time.
• Other data structures like stacks, Hash Tables, Binary
trees can also be constructed or programmed using a doubly-
linked list.
• Used in the navigation systems where front and back
navigation are required. (See also Search in AI)
Circular Lists
• The last node points to the first one
Potentially needed operations:
• Insertion / deletion: front, end, some specific
point
Circular Lists
Avantages:
• We can go to any node and traverse from any
node. We just need to stop when we visit the
same node again.
• As the last node points to the first node, going to
the first node from the last node just takes a
single step
Applications of
Circular Linked Lists
• Used in multiplayer games to swap between
players.
• On Operating Systems: Multiple running
applications can be placed in a CLL so the OS
keeps on iterating over these applications giving
them time slices. (Round Robin Scheduling)
• Music or media player: the use of CLLs allows
for a continuous playback of the playlist
Stacks
• A Stack is a list where you can access add or delete
elements at one end only.
• Stacks are called ``last in first out’’ (LIFO): the last
added element among the current ones can be accessed
or deleted.
• All of these operations take constant time.
o Deletion is called “pop’’
o Addition is called “push’’
• You can also test whether the stack is empty but
nothing else.
EXAMPLE
1
3 3
5 5 5
Get 1 Get 3 7
3 5
5 5
1 5
2 5 3
3 5 31
2 5 3
1 5 2 5 7
Stack Implementation using a
Singly Linked List
Need not know the maximum size
Need not have a special header.
Add/Access/Delete in the beginning, O(1)
One would not use a stack if Add / Access /
Delete operations are needed at some position
k different from the beginning (top).
Push 5, 3, 1, Pop, Pop, Push 7
5 NULL
3 5 NULL
1 3 5 NULL
3 5 NULL
5 NULL
7 5 NULL
Uses of Stacks: Symbol Matching
[ { [ ( )] } ]
[{]}()
{
[ [
Push [ Push {
Oops! Something
Get { wrong, was expecting [
[
Pop Expecting [
EXAMPLE
Check brace, bracket parentheses matching
[a+b{1*2}9*1]+(2-1)
Push [, Push {, Pop , Pop, Push (, Pop Get {
{
[ [ [
Get (
Get [
(
Pop Expect [ Push ( Pop Expect (
Evaluation of arithmetic expressions
A*B + C
A*(B + C)
A+B+C*D
A*D+B+C*E
To do the correct operation, the calculator needs
to know the priorities of the operators
We don’t need any priority nor parentheses if the
operation is expressed in postfix or reverse
polish notation.
Postfix Notation
Infix notation Postfix notation
A*B + C AB*C+
A*(B + C) ABC+*
A+B+C*D AB+CD*+
A*D+B+C*E AD*B+CE*+
4
5 5 5
Get 5
6
20 20
20 26
Get 2
2
7 7 7
26 26 26
Push 7 Push 2 Pop
Get 7 Get 14
14 26
26
26 Pop
Push 14
Pop
Get 26
40
Pop Push 40
A*B + C *AB+C
A+B)*C-(D-E)*(F+G) -*+ABC*-DE+FG