4a. Linked Lists
4a. Linked Lists
Linked Lists
Data Structures & Algorithms
CONTENT
• Linked lists: allocating memory for each element separately and only when
necessary.
Array
• Advantages: • Advantages:
– Easy to use – Arbitrary size
– A good choice for a small list
– No shift required
– O(1) access time
• Disadvantages: • Disadvantages:
– Fixed size – Necessity to allocate next
– Memory wasting – O(N) access time
– Still space and time wasting in
dynamic array
Data Structures & Algorithms 6
…Linked List Basics
a b c d
mat
• Implementation
–Declaration –Traverse a list
typedef struct node *pnode; void traverseList(pnode ptr){
typedef struct node {
char data [4]; pnode p = ptr;
pnode next; printf(“The list contains: “);
}; for ( ; p ; p = p->next)
–Creation printf(“%s\n”, p->data);
pnode ptr =NULL; }
–Testing
#define IS_EMPTY(ptr) (!(ptr))
#define IS_FULL(ptr) (!(ptr))
• The link field of the last node points to the first node in the list
a x1 x2 x3
x1 x2 x3 a
X1 X2 X3 ptr
(2)
(1)
Data Structures & Algorithms 19
…Circularly linked list
X X
• Doubly linked list with a head node points to the first node in the list
and to the last node in the list
head node
ptr
• Different uses
• Doubly linked circular list with head node
• Insertion into an empty doubly linked circular list
node node
newnode
• Different uses
–Doubly linked circular list with head node
• Insertion into a doubly linked circular list
head node
• Different uses
–Doubly linked circular list with head node
• Deletion from a doubly linked circular list
head node
(1)
llink item rlink
(2)
• Declarations
• Each term as a node containing typedef struct poly_node *poly_pointer;
coefficient , exponent, as well as typedef struct poly_node {
int coef;
a pointer to the next term int expon;
poly_pointer link;
coef expon link };
poly_pointer a , b, c;
Data Structures & Algorithms 29
Data Structures & Algorithms 29
…Polynomial representation
• Polynomials representation - Example
14 8
a = 3x + 2x +1
a
3 14 2 8 1 0 null
14 10 6
b = 8x - 3x + 10 x
b
8 14 -3 10 10 6 null
1. a->expon == b->expon
If the exponents of the two terms are equal, we add the two coefficients and
create a new term c for the result
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14
c
a->expon == b->expon
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10
c
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 2 8
c
for (; a; a = a->link)
attach(a->coef, a->expon, &rear);
for (; b; b = b->link)
attach(b->coef, b->expon, &rear);
rear->link = NULL;
temp = front;
front = front->link;
free(temp);
Delete extra initial node.
return front;
} // end function
• Erasing polynomials
• Equivalence relations
• A relation over a set, S, is said to be an equivalence relation over S iff
it is symmetric, reflexive, and transitive over S.
• Reflexivity: x=x
• Symmetry: if x=y, then y=x
• Transitivity: if x=y and y=z, then x=z
• Example
0=4, 3=1, 6=10, 8=9, 7=4, 6=8, 3=5, 2=11, 11=0
ð three equivalent classes:
{0,2,4,7,11}; {1,3,5}; {6,8,9,10}
Data Structures & Algorithms 41
…Equivalence relations
• Algorithm to find Equivalence Classes
void equivalence() {
initialize data structures;
while (there are more pairs) {
read the next pair <i,j>;
process this pair;
}
initialize the output;
do {
output a new equivalence class;
} while (not done);
}
Data Structures & Algorithms 42
…Equivalence relations
• More detailed Algorithm to find Equivalence Classes
void equivalence() {
initialize seq to NULL and out to TRUE;
while (there are more pairs) {
read the next pair, <i,j>;
put j on the seq[i] list;
put i on the seq[j] list;
}
for (i=0; i<n; i++)
if (out[i]) {
out[i]= FALSE;
output this equivalence class;
compute indirect equivalence using transitivity by using stack;
}
}
Data Structures & Algorithms 43
…Equivalence relations
• Illutration
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
0º4 seq
3º1
6 º 10
8º9 data 11 3 11 5 7 3 8 4 6 8 6 0
7º4 link NULL NULL NULL NULL NULL NULL
6º8
3º5
2 º 11 data 4 1 0 10 9 2
11 º 0 link NULL NULL NULL NULL NULL
Example: 0=4, 3=1, 6=10, 8=9, 7=4, 6=8, 3=5, 2=11, 11=0
ð three equivalent classes: {0,2,4,7,11}; {1,3,5}; {6,8,9,10}
Data Structures & Algorithms 44
…Equivalence relations
• Program (1/4)
#include <stdio.h>
#include <alloc.h>
#define MAX_SIZE 24
#define IS_FULL(ptr) (!(ptr))
#define FALSE 0
#define TRUE 1
typedef struct node *node_pointer ;
typedef struct node {
int data;
node_pointer link;
};
• Sparse matrices
H1 H2 H3 H4
4• 4
0 2
H1 11
H2 1 0 1 1
12 5
2 1
H3 -4
3 3
H4
-15
Data Structures & Algorithms 52
Data Structures & Algorithms 52
…Sparse matrices
• Program (1) - Declarations
#include <stdio.h> struct MatrixNode {
#include <stdlib.h> MatrixPointer down;
#define MAX_SIZE 25 MatrixPointer right;
typedef enum {head,entry} tagfield; tagfield tag;
typedef struct MatrixNode *MatrixPointer; union {
struct EntryNode { MatrixPointer next;
int row; struct EntryNode entry;
int col; } u;
int value; };
}; MatrixPointer HdNode[MAX_SIZE];
• Applications