self-referential structures and linked listcse.iitkgp.ac.in/.../pdstheory2019a/l9-linkedlist.pdf–...
TRANSCRIPT
![Page 1: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/1.jpg)
1
Self-referential Structuresand
Linked List
![Page 2: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/2.jpg)
2
Linked List :: Basic Concepts
• A list refers to a set of items organized sequentially.– An array is an example of a list.• The array index is used for accessing and
manipulating array elements.– Problems with array:• The array size has to be specified at the
beginning.• Deleting an element or inserting an element
may require shifting of elements in the array.
![Page 3: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/3.jpg)
3
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 Structure 2 Structure 3
item item item
![Page 4: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/4.jpg)
4
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.
![Page 5: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/5.jpg)
5
Contd.
• Such a structure can be represented as:struct node{
int item;struct node *next;
}
• Such structures which contain a member field pointing to the same structure type are called self-referential structures.
item
node
next
![Page 6: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/6.jpg)
6
Contd.
• In general, a node may be represented as follows:
struct node_name{
type member1;type member2;………struct node_name *next;
}
![Page 7: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/7.jpg)
7
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;
![Page 8: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/8.jpg)
8
Contd.
• To create the links between nodes, we can write:
n1.next = &n2;n2.next = &n3;n3.next = NULL; /* No more nodes follow */
• Now the list looks like:
n1 n2 n3
rollnameagenext
![Page 9: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/9.jpg)
• Some important observations:– The NULL pointer is used to indicate that no
more nodes follow, that is, it is the end of the list.
– To use a linked list, we only need a pointer to the first element of the list.
– Following the chain of pointers, the successive elements of the list can be accessed by traversing the list.
9
![Page 10: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/10.jpg)
10
Example: without using function#include<stdio.h>struct stud{
int roll;char name[30];int age;struct stud *next;
}
main(){
struct stud n1, n2, n3;struct stud *p;
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);
![Page 11: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/11.jpg)
11
n1.next = &n2;n2.next = &n3;n3.next = NULL;
/* 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;
}}
![Page 12: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/12.jpg)
A function to carry out traversal
12
#include<stdio.h>struct stud{
int roll;char name[30];int age;struct stud *next;
}
void traverse (struct stud *head){
while (head != NULL){printf (”\n %d %s %d”, head->roll, head->name,
head->age);head = head->next;
}}
![Page 13: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/13.jpg)
The corresponding main() function
13
main(){
struct stud n1, n2, n3, *p;
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; n2.next = &n3; n3.next = NULL;
p = &n1;traverse (p);
}
![Page 14: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/14.jpg)
14
Alternative and More General Way
• Dynamically allocate space for the nodes.– Use malloc() or calloc() for allocating
space for every individual nodes.– No need for allocating additional space
unnecessarily like in an array.
![Page 15: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/15.jpg)
Linked List in more detail
15
![Page 16: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/16.jpg)
16
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 B C
head
![Page 17: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/17.jpg)
17
• Keeping track of a linked list:– Must know the pointer to the first element of
the list (called start, head, etc.).
• Linked lists provide flexibility in allowing the items to be rearranged efficiently.– Insert an element.– Delete an element.
![Page 18: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/18.jpg)
18
Illustration: Insertion
Item to be inserted
X
A B
A B C
C
Xhead
head
![Page 19: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/19.jpg)
19
Illustration: Deletion
CA B
A B CItem to be deleted
head
head
![Page 20: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/20.jpg)
20
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.
![Page 21: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/21.jpg)
21
Array versus Linked Lists
• Arrays are suitable for:– Inserting/deleting an element at the end.– Randomly accessing any element.– Searching the list for a particular value.
• Linked lists are suitable for:– Inserting an element.– Deleting an element.– Applications where sequential access is required.– In situations where the number of elements
cannot be predicted beforehand.
![Page 22: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/22.jpg)
22
Types of Lists
• Depending on the way in which the links are used to maintain adjacency, several different types of linked lists are possible.– Linear singly-linked list (or simply linear list)• One we have discussed so far.
A B C
head
![Page 23: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/23.jpg)
23
– Circular linked list• The pointer from the last element in the list
points back to the first element.
A B C
head
![Page 24: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/24.jpg)
24
– 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.
A B C
head tail
![Page 25: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/25.jpg)
25
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
![Page 26: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/26.jpg)
26
List is an Abstract Data Type
• What is an abstract data type?– It is a data type defined by the user.– Typically more complex than simple data types
like int, float, etc.• 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.
![Page 27: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/27.jpg)
27
Conceptual Idea
List implementation
and therelated functions
Insert
Delete
Traverse
![Page 28: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/28.jpg)
28
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;
};
/* A user-defined data type called “node” */
typedef struct stud node;node *head;
![Page 29: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/29.jpg)
29
Creating a List
![Page 30: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/30.jpg)
30
How to begin?
• To start with, we have to create a node (the first node), and make head point to it.
head = (node *) malloc(sizeof(node));
head
next
age
name
roll
![Page 31: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/31.jpg)
31
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.
A B C
head
![Page 32: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/32.jpg)
32
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);
}
![Page 33: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/33.jpg)
• To be called from main() function as:
node *head;………head = create_list();
33
![Page 34: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/34.jpg)
34
Traversing the List
![Page 35: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/35.jpg)
35
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.
![Page 36: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/36.jpg)
36
void display (node *head){int count = 1;node *p;
p = head;while (p != NULL){printf ("\nNode %d: %d %s %d", count,
p->roll, p->name, p->age);count++;p = p->next;
}printf ("\n");
}
![Page 37: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/37.jpg)
37
• To be called from main() function as:
node *head;………display (head);
![Page 38: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/38.jpg)
38
Inserting a Node in a List
![Page 39: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/39.jpg)
39
How to do?
• The problem is to insert a node before a specified node.– Specified means some value is given for the
node (called key).– In this example, we consider it to be roll.
• Convention followed:– If the value of roll is given as negative, the
node will be inserted at the end of the list.
![Page 40: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/40.jpg)
40
Contd.
• When a node is added at the beginning,– Only one next pointer needs to be modified.• head is made to point to the new node.• New node points to the previously first
element.
• When a node is added at the end,– Two next pointers need to be modified.• Last node now points to the new node.• New node points to NULL.
![Page 41: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/41.jpg)
• When a node is added in the middle,– Two next pointers need to be modified.• Previous node now points to the new node.• New node points to the next node.
41
![Page 42: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/42.jpg)
42
void insert (node **head) {
int k = 0, rno; node *p, *q, *new;
new = (node *) malloc (sizeof(node));
printf ("\nEnter data to be inserted: ");scanf ("%d %s %d", &new->roll, new->name, &new->age); printf ("\nInsert before roll (-ve for end):"); scanf ("%d", &rno);
p = *head;
if (p->roll == rno) /* At the beginning */ {
new->next = p; *head = new;
}
Why is the argument a pointer to pointer?
![Page 43: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/43.jpg)
43
else {while ((p != NULL) && (p->roll != rno)) {
q = p; p = p->next;
}
if (p == NULL) /* At the end */ {
q->next = new; new->next = NULL;
}
else if (p->roll == rno) /* In the middle */
{ q->next = new; new->next = p;
} }
}
The pointers q and p always point to consecutive nodes.
![Page 44: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/44.jpg)
44
• To be called from main() function as:
node *head;………insert (&head);
![Page 45: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/45.jpg)
45
Deleting a node from the list
![Page 46: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/46.jpg)
46
What is to be done?
• Here also we are required to delete a specified node.– Say, the node whose roll field is given.
• Here also three conditions arise:– Deleting the first node.– Deleting the last node.– Deleting an intermediate node.
![Page 47: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/47.jpg)
47
void delete (node **head) {
int rno; node *p, *q;
printf ("\nDelete for roll: "); scanf ("%d", &rno);
p = *head; if (p->roll == rno)
/* Delete the first element */ {
*head = p->next; free (p);
}
![Page 48: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/48.jpg)
48
else{
while ((p != NULL) && (p->roll != rno)) {
q = p; p = p->next;
}
if (p == NULL) /* Element not found */ printf ("\nNo match :: deletion failed");
else if (p->roll == rno) /* Delete any other element */
{ q->next = p->next; free (p);
} }
}
The pointers q and p always point to consecutive nodes.
![Page 49: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/49.jpg)
49
A sample main() function
int main(){
node *head;
head = create_list();display(head);
insert(&head);display(head);
delete(&head);display(head);
}
![Page 50: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/50.jpg)
50
Abstract Data Types
![Page 51: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/51.jpg)
51
Definition
• An abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data.
• Such data type is abstract in the sense that it is independent of various concrete implementations.
• Some examples follow.
![Page 52: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/52.jpg)
52
ComplexNumber
add
mul
sub
read
div
Example 1 :: Complex numbers
![Page 53: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/53.jpg)
53
Example 1 :: Complex numbers
struct cplx {float re;float im;
}typedef struct cplx complex;
complex *add (complex a, complex b);complex *sub (complex a, complex b);complex *mul (complex a, complex b);complex *div (complex a, complex b);complex *read();void print (complex a);
Structure definition
Function prototypes
![Page 54: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/54.jpg)
54
Set
union
size
minus
intersect
delete
insert
Example 2 :: Set manipulation
![Page 55: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/55.jpg)
55
Example 2 :: Set manipulation
struct node {int element;struct node *next;
}typedef struct node set;
set *union (set a, set b);set *intersect (set a, set b);set *minus (set a, set b);void insert (set a, int x);void delete (set a, int x);int size (set a);
Structure definition
Function prototypes
![Page 56: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/56.jpg)
56
STACK
push
create
pop
isfull
isempty
Example 3 :: Last-In-First-Out STACK
![Page 57: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/57.jpg)
57
Visualization of a Stack
In Out
ABC CB
![Page 58: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/58.jpg)
58
Example 3 :: Last-In-First-Out STACK
Assume:: stack contains integer elementsvoid push (stack s, int element);
/* Insert an element in the stack */int pop (stack s);
/* Remove and return the top element */void create (stack s);
/* Create a new stack */int isempty (stack s);
/* Check if stack is empty */int isfull (stack s);
/* Check if stack is full */
![Page 59: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/59.jpg)
59
Contd.
• We shall later look into two different ways of implementing stack:– Using arrays– Using linked list
![Page 60: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/60.jpg)
60
QUEUE
enqueue
create
dequeue
size
isempty
Example 4 :: First-In-First-Out QUEUE
![Page 61: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/61.jpg)
61
Visualization of a Queue
In Out
AC B AB
![Page 62: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/62.jpg)
62
Example 4 :: First-In-First-Out QUEUE
Assume:: queue contains integer elementsvoid enqueue (queue q, int element);
/* Insert an element in the queue */int dequeue (queue q);
/* Remove an element from the queue */queue *createq();
/* Create a new queue */int isempty (queue q);
/* Check if queue is empty */int size (queue q);
/* Return the no. of elements in queue */
![Page 63: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/63.jpg)
63
Few Exercises to Try Out
• Write functions to:1. Concatenate two given lists into one big list.
node *concatenate (node *head1, node *head2);
2. Insert an element in a linked list in sorted order. The function will be called for every element to be inserted.
void insert_sorted (node **head, node *element);
3. Always insert elements at one end, and delete elements from the other end (first-in first-out QUEUE).void insert_q (node **head, node*element)node *delete_q (node **head) /* Return the deleted node */
![Page 64: Self-referential Structures and Linked Listcse.iitkgp.ac.in/.../pdstheory2019a/L9-LinkedList.pdf– Inserting/deleting an element at the end. – Randomly accessing any element. –](https://reader034.vdocument.in/reader034/viewer/2022050405/5f82527b0337c3550d0e67b2/html5/thumbnails/64.jpg)
More Exercises
4. Implement a circular linked list, and write functions to insert, delete, and traverse nodes in the list.
5. Represent a polynomial as a linked list, where every node will represent a term of the polynomial (anxn), and will contain the values of ‘n’ and ‘an’. Write a function to add two given polynomials.
64