Linked list in data structure and algorithm
Linked list in data structure and algorithm
Linked List
Linked List
head
A B C
Keeping track of a linked list:
Must know the pointer to the first element of the list (called start, head, etc.).
Must know the pointer to the first element of the list (called start, head, etc.).
Item to be
tmp X inserted
A B C
curr
X
Pseudo-code for insertion
typedef struct nd {
struct item data;
struct nd * next;
} node;
tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
Illustration: Deletion
Item to be deleted
A B C
curr tmp
A
A B
B C
Pseudo-code for deletion
typedef struct nd {
struct item data;
struct nd * next;
} node;
head
A B C
Circular linked list
head
A B C
Doubly linked list
• Pointers exist between adjacent nodes in both directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of the list, head and tail.
head tail
A B C
Basic Operations on a List
● Creating a list
● Traversing the list
● Inserting an item in the list
● Deleting an item from the list
● Concatenating two lists into one
List is an Abstract Data Type
Why abstract?
Because details of the implementation are hidden.
When you do some operation on the list, say insert an element, you just call a function.
Details of how the list is implemented or how the insert function is written is no longer
required.
Conceptual Idea
List
Insert implementatio
n
Delete and the
Traverse related
functions
Example: Working with linked list
• Consider the structure of a node as follows:
struct stud {
int roll;
char name[25];
int age;
struct stud *next;
};
head
roll
name
age
Contd.
• If there are n number of nodes in the initial linked list:
– Allocate n records, one by one.
– Read in the fields of the records.
– Modify the links of the records so that the chain is formed.
head
A B C
Code
node *create_list()
{
int k, n;
node *p, *head;
printf ("\n How many elements to enter?");
scanf ("%d", &n);
for (k=0; k<n; k++)
{
if (k == 0) {
head = (node *) malloc(sizeof(node));
p = head;
}
else {
p->next = (node *) malloc(sizeof(node));
p = p->next;
}
scanf ("%d %s %d", &p->roll, p->name, &p->age);
}
p->next = NULL;
return (head);
}
Things to remember
node *head;
………
head = create_list();
Traversing the List
What is to be done?
• Once the linked list has been constructed and head points to the first node of the list,
– Follow the pointers.
– Display the contents of the nodes as they are traversed.
– Stop when the next pointer points to NULL.
Code
p = head;
while (p != NULL)
{
printf ("\n Node %d: %d %s %d", count,
p->roll, p->name, p->age);
count++;
p = p->next;
}
printf ("\n");
}
Things to remember
node *head;
………
display (head);
Inserting a Node in a List
How to do?
p = *head;
node *head;
………
insert (&head);
Deleting a node from the list
What is to be done?
p = *head;
if (p->roll == rno)
/* Delete the first element */
{
*head = p->next;
free (p);
}
else
{
while ((p != NULL) && (p->roll != rno))
{
q = p;
p = p->next;
}