Programming & Data Structures: Pallab Dasgupta
Programming & Data Structures: Pallab Dasgupta
} WeatherData;
int main () {
int numdays;
WeatherData * days;
scanf (%d, &numdays) ;
days=(WeatherData *)malloc (sizeof(WeatherData)*numdays);
if (days == NULL) printf (Insufficient memory);
...
free (days) ;
}
Self-referential structures
struct list {
int data ;
struct list * next ;
};
Pictorial representation
A structure of type struct list
data next
The pointer variable next contains either
an address of the location in memory of the
successor list element
or the special value NULL defined as 0.
NULL is used to denote the end of the list.
Dept. of CSE, IIT KGP
struct list a, b, c;
a.data = 1;
b.data = 2;
c.data = 3;
a.next = b.next = c.next = NULL;
a
b
1
NULL
data next
c
2
NULL
data next
NULL
data next
a.next = &b;
b.next = &c;
a
b
1
data next
c
2
data next
NULL
data next
2
3
Linked Lists
A singly linked list is a
concrete data structure
consisting of a
sequence of nodes
Each node stores
next
element
link to the next node
node
elem
NULL
A
Dept. of CSE, IIT KGP
head
NULL
A
Dept. of CSE, IIT KGP
Storage allocation
LINK head ;
head = malloc (sizeof(ELEMENT));
head->d = n;
head->next = NULL;
NULL
Storage allocation
head->next = malloc (sizeof(ELEMENT));
head->next->d = e;
head->next->next = NULL;
NULL
Storage allocation
head->next->next = malloc (sizeof(ELEMENT));
head->next->next->d = w;
head->next->next-> = NULL;
NULL
List operations
List operations
(i) How to initialize such a self referential structure
(LIST),
(ii) how to insert such a structure into the LIST,
(iii) how to delete elements from it,
(iv) how to search for an element in it,
(v) how to print it,
(vi) how to free the space occupied by the LIST?
#include list.h
list from a string
LINK SToL (char s[]) {
(iterative version)
LINK head = NULL, tail;
int
i;
if (s[0] != \0) {
head = malloc (sizeof(ELEMENT));
head->d = s[0];
tail = head;
for (i=1; s[i] != \0; i++) {
tail->next = malloc(sizeof(ELEMENT));
tail = tail->next;
tail->d = s[i];
}
tail->next = NULL;
}
return head;
}
Dept. of CSE, IIT KGP
Insertion
To insert a data item into an ordered linked list involves:
creating a new node containing the data,
finding the correct place in the list, and
linking in the new node at this place.
Example of an Insertion
first
prev
3
new
ptr
8
12
Create_node function
Listpointer create_node(int data)
{
LINK new;
new = (LINK) malloc (sizeof (ELEMENT));
new -> data = data;
return (new);
}
insert function
LINK insert (int data, LINK ptr)
{
LINK new, prev, first;
new = create_node(data);
if (ptr == NULL || data < ptr -> value)
{
// insert as new first node
new -> next = ptr;
return new;
// return pointer to first node
}
else
{
Deletion
To delete a data item from a linked list involves
(assuming it occurs only once!):
finding the data item in the list, and
linking out this node, and
freeing up this node as free space.
Example of Deletion
first
prev
3
ptr
5
12
else
{
}
}
}
// end delete
dummy node
Insertion at the beginning is the same as insertion
after the dummy node
Initialization
head
head
head
Insert
head
Delete
temp
head
temp
head
head
temp1
temp2
1. A one-element list
head
head
tail
tail
3. Updating the tail
head
B
tai
l
B NULL
tai
l
/* Print a List */
void PrintList (LINK head) {
if (head == NULL)
printf (NULL) ;
else {
printf (%c --> , head->d) ;
PrintList (head->next);
}
}
Insertion
Before Insertion
p2
p1
A
q
B
Dept. of CSE, IIT KGP
Insertion
/* Inserting an element in a linked list. */
void insert (LINK p1, LINK p2, LINK q) {
p1->next = q;
q->next = p2;
}
After Insertion
p2
p1
A
q
B
Dept. of CSE, IIT KGP
Deletion
Before deletion
p
1
p->next = p->next->next;
p
1
garbage
After deletion
2
Deletion
Before deletion
q = p->next;
p->next = p->next->next;
p
After deletion
1
2
q
3
free (q) ;