DS4 PDF
DS4 PDF
DS4 PDF
Heap Memory
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
202
201
Limitations of Dynamic Array
Heap Memory (Horizontal View)
200
202
201
202
201
int *p=(int*)malloc(3*sizeof(int));
Limitations of Dynamic Array
Heap Memory (Horizontal View)
212
200
202
201
p
Consider a program segment:
212
200
202
201
p
Consider a program segment:
212
213
220
200
202
201
p q
Consider a program segment:
212
213
220
200
202
201
p q
Consider a program segment:
212
213
220
200
202
201
p q
Consider a program segment:
236
212
213
220
221
200
202
201
p q r
Consider a program segment:
236
212
213
220
221
200
202
201
p q r
Consider a program segment:
236
212
213
220
221
200
202
201
p r
Consider a program segment:
236
212
213
220
221
200
202
201
p r
Consider a program segment:
236
212
213
220
221
200
202
201
236
212
213
220
221
200
202
201
p
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
7 10
236
212
213
222
200
202
201
p q
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
7 5 10
236
212
213
222
200
202
201
p r q
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
7 5 10 9
233
212
213
222
200
202
201
p r q s
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
7 5 10 9
233
212
213
222
200
202
201
p r q s
To traverse the elements of the list, one should store the address of the
next element in the list with each element.
Solution: Linked List
Heap Memory (Horizontal View)
233
213
222
202
p
To traverse the elements of the list {7, 10, 5, 9}, one should store the
address of the next element (pointer to next element) in the list with
each element.
It is sufficient to know the address of (pointer to) the first node (also
called as head node) to retrieve all the elements of the list.
7 10 5 9
Head pointer Head node
• Sequence (list) of four items < a1,a2 ,a3 ,a4 > can be represented
by:
head represents
null
a1 a2 a3 a4
Pointer-Based Linked Lists
Node *next;
}; //end struct
• A node is dynamically allocated
Node *p;
p = malloc(sizeof(Node));
Pointer-Based Linked Lists
• The head pointer points to the first node in a
linked list
• If head is NULL, the linked list is empty
– head=NULL
• head=malloc(sizeof(Node))
A Sample Linked List
Traverse a Linked List
• Reference a node member with the ->
operator
p->item;
• A traverse operation visits each node in the
linked list
– A pointer variable cur keeps track of the current
node
for (Node *cur = head;
cur != NULL; cur = cur->next)
x = cur->item;
Traverse a Linked List
backward traversal
Doubly Linked Lists
cur
newPtr
newPtr->next = cur->next;
newPtr->prev = cur;
cur->next->prev = newPtr;
cur->next = newPtr;
Deleting a Node
• Delete a node from middle
head x1 x2 x4
x3
cur
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
free(cur);
Circular Linked Lists
• May need to cycle through a list repeatedly, e.g.
round robin system for a shared resource.
• What is it?
4x6 + 10x4 - 5x + 3
• Now what?
6 2 3 8 -3 18 0 0 23
0 2 0 2 4
Index
represents
exponents
Polynomial ADT (continued)
•This is why arrays aren’t good to represent
polynomials:
6 2 0 0 -3 0 ………… 0 16
0 1 2 3 4 5 20 21
WASTE OF SPACE!
Polynomial ADT (continued)
• Advantages of using an Array:
-2 0 22 … … -13 … ... … 0 6
0 1 2 3 ---------13 14 15 ---------------100 101 102
Node Representation
struct polynode
{
double coeff;
int expon;
polynode* next;
};
typedef struct polynode *polyptr;
P1 23 9 -18 7 41 6 163 4 -3 0
P2 4 6 10 4 -12 1 8 0
After addition
After addition
p3(x) = 23x9
Adding Two Polynomials (Linked Lists)
• How it works
After addition
After addition
After addition
After addition
After addition
After addition
After addition
p1 3 14 2 8 1 0 null
14 10 6
p2 8x 3x 10x
p2 8 14 -3 10 10 6 null
Adding Two Polynomials
3 14 2 8 1 0
p1
8 14 -3 10 10 6
p2
p1->expon == p2->expon
11 14
p3
Adding Two Polynomials
3 14 2 8 1 0
p1
8 14 -3 10 10 6
p2
p1->expon < p2->expon
11 14 -3 10
p3
Adding Two Polynomials
3 14 2 8 1 0
p1
8 14 -3 10 10 6
p2
p1->expon > p2->expon
11 14 -3 10 2 8
p3
Adding Two Polynomials
3 14 2 8 1 0
p1
8 14 -3 10 10 6
p2
p1->expon < p2->expon
11 14 -3 10 2 8 10 6
p3
Adding Two Polynomials
3 14 2 8 1 0
p1
8 14 -3 10 10 6
p2 = NULL
11 14 -3 10 2 8 10 6
1 0
14 10 8 6
p 3 11 x 3x 2 x 10 x x p3
• Home task
p 11 x 6 y 5 3 x 4 y 7 10 x 2 y xy
11 -3 10 1
6 5 4 7 2 1 1 1
Representing two-variable polynomial
Another way
p 11 x 6 y 5 3 x 4 x 7 10 x 2 y xy
y 1 2 3 4 5 6 7
x
1 1 0 0 0 0 0 0
2 10 0 0 0 0 0 0
3 0 0 0 0 0 0 0
4 0 0 0 0 0 0 -3
5 0 0 0 0 0 0 0
6 0 0 0 0 11 0 0
Sparse Matrix
Sparse Matrix Representation
In a sparse matrix, most entries are zeroes:
Sparse Matrix Representation
Many problems require large sparse matrices.
Mechanism:
Each node will need to know its row number and column
number and (non-zero) value.
An Example Node
For example, row 3 and column 4 (i.e., entry [3,4]) contains 42:
struct Node{
int row, col, value;
Node* NextInCol;
Node* NextInRow;
}
Row and Columns
• A row or a column is a linked list of non-zero
entries.
– If the row list (or column list) doesn’t exist, then all entries in
that row (or column) are 0.
• If the node you’re looking for doesn’t exist, then the value
of that entry is 0.