Self-Referential Structures and Linked List: Programming and Data Structure 1
Self-Referential Structures and Linked List: Programming and Data Structure 1
Contd.
A completely different way to represent a list:
Make each item in the list part of a structure. The structure also contains a pointer or link to the structure containing the next item. This type of list is called a linked list.
Structure 1 item Structure 2 item Structure 3 item
Contd.
Each structure of the list is called a node, and consists of two fields:
One containing the data item(s). The other containing the address of the next item in the list (that is, a pointer).
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.
Spring Semester 2011 Programming and Data Structure 4
Contd.
Such a structure can be represented as:
struct node { int item; struct node }
*next; node
item
next
Such structures which contain a member field pointing to the same structure type are called self-referential structures.
Spring Semester 2011 Programming and Data Structure 5
Contd.
In general, a node may be represented as follows:
struct node_name { type member1; type member2; struct node_name *next; }
Illustration
Consider the structure:
struct stud { int roll; char name[30]; int age; struct stud *next; }
Also assume that the list consists of three nodes n1, n2 and n3.
struct stud n1, n2, n3;
Spring Semester 2011 Programming and Data Structure 7
Contd.
To create the links between nodes, we can write:
n1.next = &n2; n2.next = &n3; n3.next = NULL; /* No more nodes follow */
n1
Spring Semester 2011
n2
Programming and Data Structure
n3
8
stud stud
scanf (%d %s %d, &n1.roll, n1.name, &n1.age); scanf (%d %s %d, &n2.roll, n2.name, &n2.age); scanf (%d %s %d, &n3.roll, n3.name, &n3.age);
Spring Semester 2011 Programming and Data Structure 10
= = =
/* Now traverse the list and print the elements */ p = &n1; /* point to 1st element */ while (p != NULL) { printf (\n %d %s %d, p->roll, p->name, p->age); p = p->next; } }
11
void traverse (struct stud *head) { while (head != NULL) { printf (\n %d %s %d, head->roll, head->name, head->age); head = head->next; } }
Programming and Data Structure
12
stud
scanf (%d %s %d, &n1.roll, n1.name, &n1.age); scanf (%d %s %d, &n2.roll, n2.name, &n2.age); scanf (%d %s %d, &n3.roll, n3.name, &n3.age); n1.next n2.next n3.next = = = &n2; &n3; NULL;
14
15
Introduction
A linked list is a data structure which can change during execution.
Successive elements are connected by pointers. Last element points to NULL. It can grow or shrink in size during execution of a program. It can be made just as long as required. It does not waste memory space. A
March 31, 2011
head
B
Programming and Data Structure
C
16
17
Illustration: Insertion
head
X
head
Item to be inserted
X
March 31, 2011 Programming and Data Structure 18
Illustration: Deletion
head Item to be deleted
head
19
In essence ...
For insertion:
A record is created holding the new item. The next pointer of the new record is set to link it to the item which is to follow it in the list. The next pointer of the item which is to precede it must be modified to point to the new item.
For deletion:
The next pointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item.
March 31, 2011 Programming and Data Structure 20