0% found this document useful (0 votes)
27 views

Linked List

Linked list

Uploaded by

ijantkartanishka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Linked List

Linked list

Uploaded by

ijantkartanishka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Linked List

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
• 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.
Spring 2012 Programming and Data Structure 4
Spring 2012 Programming and Data Structure 5
Node representation:
Struct Node struct Node *node1=(struct Node {
*)malloc(sizeof(struct Node));
Int data;
Node *next;
};

typedef struct node{ Node *node1=(Node


{ *)malloc(sizeof(Node));
int data;
struct node* next;
}Node;
Spring 2012 Programming and Data Structure 6
Illustration: Insertion
A B C

Item to be
tmp X inserted

A B C

curr
X
Pseudo-code for insertion

Programming and Data Structure 8


Spring 2012 Programming and Data Structure 9
Spring 2012 Programming and Data Structure 10
Spring 2012 Programming and Data Structure 11
Spring 2012 Programming and Data Structure 12
Spring 2012 Programming and Data Structure 13
Spring 2012 Programming and Data Structure 14
Spring 2012 Programming and Data Structure 15
Spring 2012 Programming and Data Structure 16
Illustration: Deletion
Item to be deleted
A B C

tmp
curr

A B C
Programming and Data Structure 18
Spring 2012 Programming and Data Structure 19
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);
}
Programming and Data Structure 21
Programming and Data Structure 22
Programming and Data Structure 23
Programming and Data Structure 24
Programming and Data Structure 25
bool searchnode(struct node *head, int key)
{
struct node *current;
current = head;
while(current != NULL)
{
if(current->data == key)
return true;
current=current->next;
}
return false;
}

Programming and Data Structure 26


Void destroy(node *head)
{
node *temp;
while(head!=null)
{
temp=head;
head=head->next;
free(temp);
}
}

Programming and Data Structure 27


Programming and Data Structure 28
Programming and Data Structure 29
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.
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.
Programming and Data Structure 32
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
Doubly linked list

Programming and Data Structure 34


Programming and Data Structure 35
Programming and Data Structure 36
Programming and Data Structure 37
Programming and Data Structure 38
Programming and Data Structure 39
Programming and Data Structure 40
Programming and Data Structure 41
Programming and Data Structure 42
Programming and Data Structure 43
Programming and Data Structure 44
Programming and Data Structure 45
Programming and Data Structure 46
Programming and Data Structure 47
Programming and Data Structure 48
Programming and Data Structure 49
Programming and Data Structure 50
Programming and Data Structure 51
Programming and Data Structure 52
Programming and Data Structure 53
Deletion at the Beginning of
Doubly Linked List

Spring 2012 Programming and Data Structure 54


Spring 2012 Programming and Data Structure 55
Programming and Data Structure 56
Spring 2012 Programming and Data Structure 57
Programming and Data Structure 58
Programming and Data Structure 59
Programming and Data Structure 60
Programming and Data Structure 61
Programming and Data Structure 62
Search an element in a Doubly
Linked List

Programming and Data Structure 63


Programming and Data Structure 64
Spring 2012 Programming and Data Structure 65
Display Function:

Programming and Data Structure 66


Sorting the list:

Programming and Data Structure 67


Programming and Data Structure 68
Programming and Data Structure 69
– Circular linked list
• The pointer from the last element in the list points back
to the first element.
head

A B C

You might also like