You are on page 1of 74

Data Structures And

Algorithms in C

© Dan Grosu, Thaer Jayyousi, Nariman TM Ammar 2009-2017, All rights reserved
Course Outline
■ C Review
■ Recursion
■ Algorithms and Analysis
■ Lists, Stacks, Queues
■ Trees
■ Hashing
■ Sorting

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
In this Chapter
*Linked lists
n Abstract
data type (ADT)
*Basic operations of linked lists
n Insert, find, delete, print, etc.
*Linked List implementation in C
*Pointer-based
*Running Time of basic operations
*Variations of linked lists
n Circular linked lists
n Doubly linked lists

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
In this Chapter
*Comparison between Linked list
Implementations
*Limitations of each
*modifications to reduce run times
*Case study: radix sort
*Cursor implementation of linked lists
*assuming no pointers exist!

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Lists
▪ A list refers to a set of items organized
sequentially
▪ The data is linearly ordered where the
programmer explicitly defines the ordering
▪ Most common Implementations
▪ Array (not the most optimal)
▪ Linked list (variations)

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Linked Lists
*A linked list is a series of connected nodes
*Each node contains at least
n A piece of data (any type)
n Pointer to the next node in the list

*Head: pointer to the first node


*The last node points to NULL

A node

data pointer
Head
A B C ∅ List
head
© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
A Simple Linked List Struct
*We use two structs: Node and List
*Declare Node struct for the nodes
n data: double-type data in this example
n next: a pointer to the next node in the list

struct Node{
double data; // data
struct Node* next; // pointer to next
};

Can’t use typedef. Self referencing struct

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
A Simple Linked List Struct
*Declare List, which contains
n head: a pointer to the first node in the list.
Since the list is empty initially, head is set to
NULL

typedef struct {
Node* head;
} List;

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
A Simple Linked List Struct
*Define Operations on List as functions:
n insertNode: insert a new node at a particular
position
n Insert into an empty list
n Insert in front
n Insert at back
n Insert in middle
n findNode: find a node with a given value
n deleteNode: delete a node with a given value
n findNode then delete
n printList: print all the nodes in the list

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
insertNode(List l,int value, int index)

* Insert a node with a data value previous


nodes
node at
index
remaining
nodes
after node at index.
* When index = 0, insert node as first
element; when index = 1, insert
node after first node, and so on) newNode
n If insertion fails (index is < 0 or >
length of the list), return.
* Steps:
* Locate (find) element at index
* Allocate memory for new node
* Point new node to its successor
* Point new node’s predecessor to
new node
© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Dynamic memory allocation
▪ Each node is allocated in memory with a call to
malloc()
▪ allocates requested number of bytes and
returns a pointer to the first byte of the
allocated space.
▪ The node memory continues to exist until it is
explicitly deallocated with a call to free().

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Dynamic memory allocation
▪ malloc()reserves a block of memory of
specified size and returns a pointer of type
void
▪ The return pointer can be type-casted to any
pointer type:
ptr = (type *) malloc (byte_size);
node =(struct node *) malloc(sizeof(struct node))

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Dynamic
Contd. memory allocation
• Examples
▪ Example:
p = (int *) malloc(100 * sizeof(int));
p = (int *) malloc(100 * sizeof(int));
▪ reserves a memory
– A memory space equivalent
space equivalent to 100 times to
the100
size of an int
times bytes
the size of an int bytes
is reserved.
– The address of the first byte of the allocated memory is
▪ The address
assigned of thepointer
to the first byte of the
p of type int. allocated
memory is assigned to the pointer p of type int:
p

400 bytes of space


Dept.
75
© Dan Grosu,of CSE,
Thaer IIT KGP
Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Dynamic memory allocation
▪ malloc() allocates a block of contiguous bytes.
▪ The allocation can fail if no sufficient contiguous
memory space is available.
▪ If it fails, malloc() returns NULL.

if ((p = (struct node *) malloc(sizeof(struct node)))==NULL){


printf (“\n Memory cannot be allocated”);
exit();
}

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Dynamic memory allocation
▪ The data items comprising a linked list need not
be contiguous in memory.
▪ They are ordered by logical links that are stored
as part of the data in the structure itself.
▪ The link is a pointer to another structure of the
same type.

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example
Suppose we want a linked list to store the
values 42 95 70 81 in this order
Linked lists
• We will clean up the representation as
follows:

• We do not specify the addresses


because the address are arbitrary. So
– the contents of the circle is the data.
– the next pointer is represented by an
arrow
FindNode(List l, int x)

n Search for a node with the value equal to x in


the list.
n If such a node is found, return
n its position or its address or simply that it is
found
n Otherwise, return 0 or NULL or that it is not
found.

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
int DeleteNode(List l,int
value)
* Delete a node with value from the list.
n If node is not found return.
*Steps
n Find the desirable node (call FindNode)
n Release the memory occupied by the found node
n Set the pointer of the predecessor of the found node to
the successor of the found node
head
*Special cases
n Delete first node
n Delete node in arbitrary k prev temp
n Delete node at end of the list

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Recall: List Implementations
▪ Most common Implementations
▪ Array (not the most optimal)
▪ Good: index is used for access/
manipulation.
▪ Linked list
▪ So far: Singly linked list (next pointer)
▪ Doubly linked list (next and prev pointers)
▪ Linked lists are more complex to code and
manage than arrays, but they have some
distinct advantages (next slide).

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Advantages of Linked Lists
over Arrays
* Dynamic: a linked list can easily grow and shrink in size.
* don’t need to know how many nodes will be in the list.
* nodes are created in memory as needed.
* In contrast, the size of an array is fixed at compile time.
* Easy and fast insertions and deletions
* To insert or delete an element in an array, we need to
* copy to temporary variables
* make room for new elements
* or close the gap caused by deleted elements.
* may require shifting of remaining elements
* With a linked list, no need to move other nodes. Only
need to reset some pointers.
© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Variations of Linked Lists
*Doubly linked lists
n Each node points to not only successor but the
predecessor
n There are two NULL: at the first and last nodes
n Advantage: given a node, it is easy to visit its
predecessor.
n Convenient to traverse lists backwards

∅ A B C ∅

Head

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Doubly Linked lists
▪ Doubly linked list implementation makes use of a
head as well as a tail node to the end of the list.
▪ Tail is similar to head, in that it is a node that
contains no value, and it always exists.
▪ When doubly linked list is initialized, head and tail
nodes are created.
▪ Data member head points to the header node, and
tail points to the tail node.
▪ The purpose of these nodes is to simplify the insert,
append, and remove operations by eliminating all
special-case code when the list is empty (how?)

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Doubly Linked lists
▪ Singly linked list allows for direct access from a
list node only to the next node in the list.
▪ Doubly linked list allows convenient access from
a list node to the next node and also to the
preceding node on the list.
▪ The doubly linked list node accomplishes this by
▪ storing two pointers:
▪ one to the node following it
▪ a second pointer to the node preceding it.

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Variations of Linked Lists cont.
*Circular linked lists
n The last node points to the first node of the list

A B C

Head
n How do we know when we have finished traversing
the list? (Tip: check if the next pointer of the current
node is equal to the head.)

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Locations and Run times
Consider amount of time required to perform
basic operations: find, insert node before or
after, or delete node at:
■ The first location (the front)
■ The last location (the back or nth)
■ an arbitrary (kth) location

■ How are these different for different


implementations?

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Operations at the kth entry of a List

Assuming find was performed.


Access an entry (find)

Delete the kth entry

Insert a new entry at k

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Singly linked lists

Accessing the kth entry (find) is O(n)


kth node
Insert Before Θ(1)
Insert After Θ(1)
Delete Θ(1)
Access Next Θ(1)
Access Previous Θ(n)

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Singly Linked list
Front/1st node kth node Back/nth node
Find Θ(1) Ο(n) Θ(n)*
Insert Before Θ(1) Ο(n)** Θ(n)
Insert After Θ(1) Ο(n)** Θ(n)*
Delete Θ(1) Ο(n)** Θ(n)
Access Next Θ(1) Ο(n)** N/A
Access Previous N/A Ο(n) Θ(n)

*We can speed things up if we add a tail pointer, we can reduce to Θ(1)
** accessing the kth entry—an O(n) operation + Θ(1) for insert,delete, replace

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Array vs. Singly Linked List

Access the kth Insert or delete at the


entry (find) Front kth entry Back
Singly linked lists O(n) Θ(1) Θ(1)* Θ(1) or Θ(n)
Arrays Θ(1) Θ(n) O(n) Θ(1)

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
B🤑nus: Due 18/3/2018
What about Doubly linked lists?
*show snippets of code

Accessing the kth entry is O(n)


kth node
Insert Before Θ(1)
Insert After Θ(1)
Delete Θ(1)
Access Next Θ(1)
Access Previous Θ(1)

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
B🤑nus: Due 18/3/2018
What about Doubly linked lists?
*show snippets of code

Front/1st node kth node Back/nth node


Find Θ(1) Ο(n)* Θ(1)
Insert Before Θ(1) Θ(1)* Θ(1)
Insert After Θ(1) Θ(1)* Θ(1)
Delete Θ(1) Θ(1)* Θ(1)
Access Next Θ(1) Θ(1)* n/a
Access Previous n/a Θ(1)* Θ(1)

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
C code on Ritaj
▪ See code examples on Ritaj for
▪ Full Singly Linked List implementation
▪ Dynamic memory allocation and sizeOf
▪ See extra material on pointers, arithmetic, etc.
as well as dynamic memory allocation and
management/organization

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Case study: bucket sort
• Consider sorting the following set of unique
integers in the range 0, ..., 31:
• 20 1 31 8 29 28 11 14 6 16 15
• 27 10 4 23 7 19 18 0 26 12 22
• Create a bit-vector with 32 entries
• Let’s call each entry in the bit vector a bucket

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Case study: bucket sort (cont.)
• For each number, set the bit of the
corresponding bucket to 1
• Now, just traverse the list and record only
those numbers for which the bit is 1 (true):
• 0 1 4 6 7 8 10 11 12 14 15
• 16 18 19 20 22 23 26 27 28 29 31

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Bucket Sort
• This approach makes assumptions about the
data being sorted
• constraint: items being sorted fall in a certain
range (0 to m-1)
• This assumption can reduce the run time
• The run time of such an algorithm is O(n+m):
– we make one pass through the items,
– we make one pass through the array and
extract the data items which are true

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Bucket Sort: with repetition
• Modification: what if there are repetitions in
the data?
• In this case, a bit vector is insufficient
• Two options, each bucket is either:
– a counter, or
– a list (linked)

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
B🤑nus: Due 18/3/2018
• Suppose we want to sort 10-digit numbers
where repetitions may occur
• We could use bucket sort, but how many
buckets would we require?
• Using counter?
• Using Linked list

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Bucket Sort: with repetition
• Sort the digits
03285375328235132853492351093523542
13
• We start with an array

of 10 counters, each initially

set to zero:

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Bucket Sort: with repetition
• Moving through the first 10 digits
03285375328235132853492351093523542
13
we increment the corresponding

buckets

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Bucket Sort: with repetition
• Moving through remaining digits
03285375328235132853492351093523542
13
we continue incrementing the
corresponding buckets

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Bucket Sort: with repetition
• We now simply read off the number of each
occurrence:
00111222222233333333334455555557888
99
• For example:
– there are seven 2s
– there are two 4s

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Running time
• By assuming that the data falls into a given range
m, we can achieve O(n+m) sorting run times
• Since we must access any item at least once, the
run time must be Θ(n+m)
• We must assume that the number of items being
sorted is comparable to the possible number of
values. The algorithm becomes Θ(n) when m=n
• If we are sorting n = 20 integers from 1 to one
million, bucket sort may be argued to be O(n),
however, in practice, it may be less so

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Case Study: Radix Sort
• Radix sort allows us to break a larger sorting
problem into multiple smaller sorting problems
• Consider the numbers: 16 31 99 59 27 90 10
26 21 60 18 57 17
• First sort the numbers based on their last
digit only, we get: 90 10 60 31 21 16 26 27
57 17 18 99 59
• Now sort according to the first digit:10 16
17 18 21 26 27 31 57 59 60 90 99
• The resulting sequence is a sorted list

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Decimal numbers
• Sort the following decimal numbers: 86 198 466 709 973
981 374 766 473 342
• First, interpret 86 as 086
• Next, create a list of 10 buckets:
0
1
2
3
4
5
6
7
8
9

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Decimal representation
of numbers
• Place according to the 3rd digit:
086 198 466 709 973 981 374 766 473 342
0
1 981
2 342
3 973 473
4 374
5
6 086 466 766
7
8 198
9 709

and remove: 981 342 973 473 374 086 466 766 198 709

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Decimal representation
of numbers
• Place according to the 2nd digit:
981 342 973 473 374 086 466 766 198 709
0 709
1
2
3
4 342
5
6 466 766
7 973 473 374
8 981 086
9 198

and remove: 709 342 466 766 973 473 374 981 086 198
© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Decimal representation
of numbers
• Place according to the 1st digit:
709 342 466 766 973 473 374 981 086 198
0 086
1 198
2
3 342 374
4 466 473
5
6
7 709 766
8
9 973 981
and remove: 086 198 342 374 466 473 709 766 973 981
© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Decimal representation
of numbers
• The numbers
086 198 342 374 466 473 709 766 973 981
are now in order
• This required n.d placements and removals
• In this case, d=3 and n = 10 numbers

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Binary representation of
numbers
• Sort the following base 2 numbers:
1111 11011 11001 10000 11010 101 11100 111 1011 10101

• First, interpret each as a 5-bit number:


01111 11011 11001 10000 11010 00101 11100 00111 01011 10101

• Next, create an array of two buckets:

0
1

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Binary representation of
numbers
• Place the numbers

01111 11011 11001 10000 11010 00101 11100 00111 01011
10101

into the buckets based on the 5th bit:


0 10000 11010 11100
1 01111 11011 11001 00101 00111 01011 10101

• Remove them in order:


10000 11010 11100 01111 11011 11001 00101 00111 01011 10101

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Binary representation of
numbers
• Place the numbers

10000 11010 11100 01111 11011 11001 00101 00111 01011 10101

into the buckets based on the 4th bit:


0 10000 11100 11001 00101 10101
1 11010 01111 11011 00111 01011

• Remove them in order:


10000 11100 11001 00101 10101 11010 01111 11011 00111 01011

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Binary representation of
numbers
• Place the numbers

10000 11100 11001 00101 10101 11010 01111 11011 00111
01011

into the buckets based on the 3rd bit:


0 10000 11001 11010 11011 01011
1 11100 00101 10101 01111 00111

• Remove them in order:


10000 11001 11010 11011 01011 11100 00101 10101 01111 00111

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Binary representation of
numbers
• Place the numbers

10000 11001 11010 11011 01011 11100 00101 10101 01111
00111

into the buckets based on the 2nd bit:


0 10000 00101 10101 00111
1 11001 11010 11011 01011 11100 01111

• Remove them in order:


10000 00101 10101 00111 11001 11010 11011 01011 11100 01111

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Binary representation of
numbers
• Place the numbers

10000 00101 10101 00111 11001 11010 11011 01011 11100
01111

into the buckets based on the 1st bit:


0 00101 00111 01011 01111
1 10000 10101 11001 11010 11011 11100

• Remove them in order:


00101 00111 01011 01111 10000 10101 11001 11010 11011 11100

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Example: Binary representation of
numbers
• The numbers
00101 00111 01011 01111 10000 10101 11001 11010 11011 11100

are now in order


• This required n.d placements and removals
• In this case, d=5 and n = 10 numbers

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Radix Sort algorithm
• Input: set N of numbers, b
– Suppose we are sorting n, base m numbers
– create an array of m-1 buckets
– For each number Ni
– For each digit dk,in Ni starting with the
least significant
– place the ith number in the bin
corresponding to the current digit dk
• Remove all digits in the order they were

placed into the bins in the order of the bins

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
RADIX-SORT Algorithm

RADIX-SORT(A,d, m)

1 for j←1 to n
2 for i←1 to d

3 do BUCKET-SORT(A,di, m) // place
A[j] in bin bm corresponding to digit
di

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Running time
▪ Given an array A of n d-digit numbers
▪ Each digit is in the range 0 to m-1 (so that it
can take on m possible values).
▪ There are d passes, each pass over n d-digit
numbers takes Θ(n + m) time for BUCKET-
SORT.
▪ RADIX-SORT sorts these numbers in
▪ Θ(d(n + m)) time

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Running time cont.
• Suppose that two n-digit numbers are equal for the first m
digits:
a = anan – 1an – 2...an – m + 1an – m...a1a0
b = anan – 1an – 2...an – m + 1bn – m...b1b0
where an – m < bn – m.
• Then, on pass n – m, a will be placed in a lower bin than b
• When they are taken out, a will precede b in the list
• For all subsequent iterations, a and b will be placed in the
same bin, and will therefore continue to be taken out in the
same order
• Therefore, in the final list, a must precede b

© Dan Grosu, Thaer Jayyousi, Douglas Harder, Nariman Ammar 2009-2017, All rights reserved
Cursor implementation of
Linked List
▪ So far: pointer-based implementation
▪ Two important aspects
▪ Part1: Data is stored in a collection of
structures, each structure contains data
and a pointer to the next structure.
▪ Part 2: A new node can be allocated in
memory via a call to malloc and released
by a call to free.

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Cursor implementation
▪ Simulate part 1 without pointers.
▪ represent the collection of structures as a
global array of structures
▪ the index of every array cell represents it’s
address
typedef int PtrToNode; // simulates a pointer
typedef PtrToNode List;
typedef PtrToNode Position;

struct Node{
int Element;
Position Next;
};

struct Node CursorSPace[SpaceSize]; //array of nodes


© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Cursor Implementation
▪ freelist
▪ To simulate part 2 without malloc and Slot Elem Next
free. 0 0 1
1 0 2
▪ keep a list of cells that are not in any 2 0 3
3 0 4
list.Call it the freelist 4 0 5
5 0 6
6 0 7
7 0 8
8 0 9
9 0 10
10 0 11
11 0 12
12 0 13
13 0 14
14 0 0

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Cursor Implementation
▪ To simulate malloc each allocation
should ensure that the next of slot 0 is i Elem
0 0
Next
2
the one that is available on the next 1 0 0
2 0 3
round 3 0 4
4 0 5
▪ When the next of slot 0 becomes 0 we 5 0 6
know we ran out of space! 6
7
0
0
7
8
Position CursorAlloc() 8 0 9
{ 9 0 10
10 0 11
Position p = CursorSPace[0].Next; 11 0 12
CursorSPace[0].Next=CursorSPace[p].Next; 12 0 13
if(p==0) 13 0 14
printf("Out of space\n" ); 14 0 0
return p;
}
© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Cursor Implementation
▪ freelist
▪ To simulate free each deallocation Slot Elem Next
should ensure that the next of slot 0 is 0 0 1
1 0 2
the one that is available on the next 2 0 3
round 3
4
0
0
4
5
▪ When the next of slot 0 becomes 0 we 5
6
0
0
6
7
know we ran out of space! 7 0 8
8 0 9
9 0 10
10 0 11
void CursorFree( Position p ) 11 0 12
{ 12 0 13
CursorSPace[p].Next=CursorSPace[0].Next; 13 0 14
CursorSPace[0].Next = p; 14 0 0
}
}

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example: ▪ freelist
i Elem Next
▪ inialize the list 0 0 2
1 0 0
2 0 3
List list = initializeList(); 3 0 4
4 0 5
5 0 6
▪ 2 becomes next candidate slot 6 0 7
7 0 8
for insertion 8 0 9
9 0 10
10 0 11
11 0 12
12 0 13
13 0 14
14 0 0

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example: ▪ freelist so far
Slot Elem Next
▪ after 3 calls to insert 0 0 5
1 0 2
2 10 3
insertAtBack(list,10); 3 20 4
4 30 0
insertAtBack(list,20); 5 0 6
insertAtBack(list,30); 6 0 7
7 0 8
▪ 5 becomes next candidate 8 0 9
9 0 10
10 0 11
11 0 12
12 0 13
13 0 14
14 0 0

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example: ▪ freelist so far
Slot Elem Next
▪ initialize another list 0 0 6
1 0 2
2 10 3
List list2 = initializeList(); 3 20 4
4 30 0
5 0 0
6 0 7
7 0 8
8 0 9
9 0 10
10 0 11
11 0 12
12 0 13
13 0 14
14 0 0

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example: ▪ freelist so far
Slot Elem Next
▪ after another 4 calls to insert 0 0 10
1 0 2
2 10 3
insertAtBack(list,40); 3 20 4
4 30 6
insertAtBack(list2,50); 5 0 7
insertAtBack(list,60); 6 40 8
7 50 0
insertAtBack(list,70); 8 60 9
9 70 0
10 0 11
11 0 12
12 0 13
13 0 14
14 0 0

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example:
▪ after another 4 calls to insert
▪ List 1 ▪ List 2
Slot Elem Next Slot Elem Next
1 | 0 | 2 | 5 | 0 | 7 |
2 | 10 | 3 | 7 | 50 | 10 |
3 | 20 | 4 | 10 | 80 | 11 |
4 | 30 | 6 | 11 | 90 | 0 |
6 | 40 | 8 |
8 | 60 | 9 |
9 | 70 | 12 | insertAtBack(list2,80);
12 | 100 | 0 |
insertAtBack(list2,90);
insertAtBack(list,100);

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example: ▪ FULL freelist
Slot Elem Next
▪ after another 4 calls to insert 0 0 0
1 0 2
2 10 3
insertAtBack(list,40); 3
4
20
30
13
6
insertAtBack(list2,50); 5 0 7
insertAtBack(list,60); 7
6 40
50
8
10
insertAtBack(list,70); 8 60 9
9 70 12
10 80 11
11 90 0
12 100 14
13 101 4
14 102 0

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example:
▪ after another 2 calls to insert anode at
position 3 and another at the end of list 1.
List 1 List 2 FULL freelist
Slot Elem Next Slot Elem Next Slot Elem Next
1 0 2 5 | 0 | 7 | 0 0 0
2 10 3 7 | 50 | 10 | 1 0 2
3 20 13 10 | 80 | 11 | 2 10 3
4 30 6 11 | 90 | 0 | 3 20 13
6 40 8
4 30 6
8 60 9
5 0 7
9 70 12
12 100 14 Out of Space! 7
6 40
50
8
10
13 101 4
8 60 9
14 102 0
9 70 12
10 80 11
insert(list,101,3); 11 90 0
insertAtBack(list,102); 12 100 14
13 101 4
14 102 0
© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
Example:
▪ after deleting node 70 (list 1)

List 1 List 2 FULL freelist


Slot Elem Next Slot Elem Next Slot Elem Next
1 0 2 5 | 0 | 7 | 0 0 9
2 10 3 7 | 50 | 10 | 1 0 2
3 20 13 10 | 80 | 11 | 2 10 3
4 30 6 11 | 90 | 0 | 3 20 13
6 40 8
4 30 6
8 60 12
5 0 7
9 0 0
12 100 14 9 is next available slot 7
6 40
50
8
10
13 101 4
8 60 12
14 102 0
9 0 0
10 80 11
delete(list,70); 11 90 0
12 100 14
13 101 4
14 102 0
© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved
C code on Ritaj
▪ See code example on Ritaj for
▪ Cursor Impelementation of Linked List
▪ as well as output file

© Dan Grosu, Thaer Jayyousi, Nariman Ammar, Douglas Harder 2009-2017, All rights reserved

You might also like