0% found this document useful (0 votes)
28 views24 pages

Linked List

A linked list is a data structure where elements are linked using pointers. Each element contains a data field and a pointer to the next element. Elements can easily be added or removed. Linked lists use memory efficiently and can grow and shrink dynamically during program execution. Common operations on linked lists include traversing the list, inserting elements, deleting elements, and concatenating lists.

Uploaded by

mankitt786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views24 pages

Linked List

A linked list is a data structure where elements are linked using pointers. Each element contains a data field and a pointer to the next element. Elements can easily be added or removed. Linked lists use memory efficiently and can grow and shrink dynamically during program execution. Common operations on linked lists include traversing the list, inserting elements, deleting elements, and concatenating lists.

Uploaded by

mankitt786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Linked List

8/21/2023 1
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.
head
– It does not waste memory space.

A B C

8/21/2023 2
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

8/21/2023 3
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
8/21/2023 4
Contd.
• In general, a node may be represented as
follows:

struct node_name
{
type member1;
type member2;
………
struct node_name *next;
};
8/21/2023 5
• 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.

8/21/2023 6
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.

8/21/2023 7
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

8/21/2023 8
Create a list
1. #include <stdio.h>
2. #include <stdlib.h>
3. //Represent a node of singly linked list
4. struct node{
5. int data;
6. struct node *next;
7. };
8.
9. //Represent the head and tail of the singly linked list
10.struct node *head, *tail = NULL;

8/21/2023 9
1. //addNode() will add a new node to the list
2. void addNode(int data) {
3. //Create a new node
4. struct node *newNode = (struct node*)malloc(sizeof(struct node));
5. newNode->data = data;
6. newNode->next = NULL;
7.
8. //Checks if the list is empty
9. if(head == NULL) {
10. //If list is empty, both head and tail will point to new node
11. head = newNode;
12. tail = newNode;
13. }
14. else {
15. //newNode will be added after tail such that tail's next will point to newNode
16. tail->next = newNode;
17. //newNode will become new tail of the list
18. tail = newNode;
19. }
20. }
8/21/2023 10
1. //display() will display all the nodes present in the list
2. void display() {
3. //Node current will point to head
4. struct node *current = head;
5.
6. if(head == NULL) {
7. printf("List is empty\n");
8. return;
9. }
10. printf("Nodes of singly linked list: \n");
11. while(current != NULL) {
12. //Prints each node by incrementing pointer
13. printf("%d ", current->data);
14. current = current->next;
15. }
16. printf("\n");
17.}
8/21/2023 11
1. int main()
2. {
3. //Add nodes to the list
4. addNode(1);
5. addNode(2);
6. addNode(3);
7. addNode(4);
8.
9. //Displays the nodes present in the list
10. display();
11.
12. return 0;
13. }

8/21/2023 12
Output-

Nodes of singly linked list:


1234

8/21/2023 13
Illustration: Insertion
A B C

Item to be
tmp X inserted

A B C

curr
X

8/21/2023 14
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.

8/21/2023 15
Pseudo-code for insertion
typedef struct nd {
struct item data;
struct nd * next;
} node;

void insert(node *curr)


{
node * tmp;

tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
8/21/2023 16
Inserting a Node at the Beginning
1 7 3 4 2 6 5 X

START

9 1 7 3 4 2 6 5 X

START

•Allocate memory for new node


•Store data struct node *newNode;

•Change next of new node to point to head newNode = malloc(sizeof(struct node));


•Change head to point to recently created newNode->data = 4;
newNode->next = head;
node
head = newNode;
Inserting a Node at the End
1 7 3 4 2 6 5 X

START, PTR

1 7 3 4 2 6 5 9 X

START

struct node *newNode;


•Allocate memory for new node newNode = malloc(sizeof(struct
•Store data node)); newNode->data = 4;
newNode->next = NULL;
•Traverse to last node struct node *temp = head;
•Change next of last node to recently while(temp->next != NULL)
{
created node temp = temp->next;
}
temp->next = newNode;
Inserting a Node at Middle

struct node *newNode;


newNode = malloc(sizeof(struct
•Allocate memory and store data for node)); newNode->data = 4;
new node
struct node *temp = head;
•Traverse to node just before the for(int i=2; i < position; i++)
{
required position of new node
if(temp->next != NULL)
•Change next pointers to include new { temp = temp->next; }
}
node in between
newNode->next = temp->next;
temp->next = newNode;
Illustration: Deletion
Item to be deleted
A B C

tmp
curr

A B C

8/21/2023 20
Pseudo-code for deletion
typedef struct nd {
struct item data;
struct nd * next;
} node;

void delete(node *curr)


{
node * tmp;
tmp=curr->next;
curr->next=tmp->next;
free(tmp);
}

8/21/2023 21
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)


head
• One we have discussed so far.

A B C

8/21/2023 22
– Circular linked list
• The pointer from the last element in the list points back
to the first element.
head

A B C

8/21/2023 23
– 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
head tail
the list, head and tail.

A B C

8/21/2023 24

You might also like