1
Linked List
Link − Each link of a linked list can store a data
called an element.
Next − Each link of a linked list contains a link to
the next link called Next.
Linked List − A Linked List contains the
connection link to the first link called head.
2
3
Types of Linked List
• Single Linked List − Item navigation is forward
only.
• Doubly Linked List − Items can be navigated
forward and backward.
• Circular Linked List − Last item contains link of
the first element as next and the first element
has a link to the last element as previous.
4
Operations of Linked List
• Insert − Add an element into list.
• Delete − Remove an element of the list.
• Search − Searches an element using the given
key.
• Sort - Sort List elements
• Merge – Join 2 list elements
• Display − Displays the complete list.
5
Insert Routine
6
Delete Routine
7
Double Linked List
• Data
• Forward link
• Backward link
8
Insert
9
Delete routine
10
Application of Linked List
• Polynomial ADT
• Radix Sort
• Multilist
11
Circular Linked List
Singly Linked List
• Pointer of the last node points to the first node
Doubly linked list
• The next pointer of the last node points to the first node and the previous
pointer of the first node points to the last node making the circular in both
directions.
12
Circular Linked List
Singly Linked List
struct node
{
int data;
struct node *next;
};
typedef struct node *list;
typedef struct node *position;
Doubly Linked List
struct node
{
int data;
struct node *flink;
struct node *blink;
};
struct node *head;
13
Insertion
1. Insertion at the beginning of the list
2. Insertion at the end of the list
3. Insertion in between the nodes
14
Insertion at the beginning of the list
struct Node *addBegin(struct Node *last, int data)
{
// Creating a node dynamically.
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
// Assigning the data.
temp -> data = data;
// Adjusting the links.
head=temp;
temp -> next = last -> next;
last -> next = temp;
return last;
}
15
Insertion at the end of the list
struct Node *addEnd(struct Node *last, int data)
{
// Creating a node dynamically.
struct Node *temp = (struct Node *)malloc( sizeof(struct Node));
// Assigning the data.
temp -> data = data;
// Adjusting the links.
temp -> next = last -> next;
last -> next = temp;
last = temp;
return last;
}
16
Deletion
• Deletion at beginning
• Deletion at last
• Deletion at middle
17
delete
node* deleteNode(int x)
{
//searching the Node with data x
node *n = search(x);
node *ptr = head;
if(ptr == NULL)
{
printf( "List is empty“);
return NULL;
}
else if(ptr == n)
{
head = n->next;
last->next=head;
return n;
}
else
{
while(ptr->next != n)
{
ptr = ptr->next;
}
ptr->next = n->next;
return n;
}
}

data structures Linked List concept.pptx

  • 1.
    1 Linked List Link −Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. Linked List − A Linked List contains the connection link to the first link called head.
  • 2.
  • 3.
    3 Types of LinkedList • Single Linked List − Item navigation is forward only. • Doubly Linked List − Items can be navigated forward and backward. • Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.
  • 4.
    4 Operations of LinkedList • Insert − Add an element into list. • Delete − Remove an element of the list. • Search − Searches an element using the given key. • Sort - Sort List elements • Merge – Join 2 list elements • Display − Displays the complete list.
  • 5.
  • 6.
  • 7.
    7 Double Linked List •Data • Forward link • Backward link
  • 8.
  • 9.
  • 10.
    10 Application of LinkedList • Polynomial ADT • Radix Sort • Multilist
  • 11.
    11 Circular Linked List SinglyLinked List • Pointer of the last node points to the first node Doubly linked list • The next pointer of the last node points to the first node and the previous pointer of the first node points to the last node making the circular in both directions.
  • 12.
    12 Circular Linked List SinglyLinked List struct node { int data; struct node *next; }; typedef struct node *list; typedef struct node *position; Doubly Linked List struct node { int data; struct node *flink; struct node *blink; }; struct node *head;
  • 13.
    13 Insertion 1. Insertion atthe beginning of the list 2. Insertion at the end of the list 3. Insertion in between the nodes
  • 14.
    14 Insertion at thebeginning of the list struct Node *addBegin(struct Node *last, int data) { // Creating a node dynamically. struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); // Assigning the data. temp -> data = data; // Adjusting the links. head=temp; temp -> next = last -> next; last -> next = temp; return last; }
  • 15.
    15 Insertion at theend of the list struct Node *addEnd(struct Node *last, int data) { // Creating a node dynamically. struct Node *temp = (struct Node *)malloc( sizeof(struct Node)); // Assigning the data. temp -> data = data; // Adjusting the links. temp -> next = last -> next; last -> next = temp; last = temp; return last; }
  • 16.
    16 Deletion • Deletion atbeginning • Deletion at last • Deletion at middle
  • 17.
    17 delete node* deleteNode(int x) { //searchingthe Node with data x node *n = search(x); node *ptr = head; if(ptr == NULL) { printf( "List is empty“); return NULL; } else if(ptr == n) { head = n->next; last->next=head; return n; } else { while(ptr->next != n) { ptr = ptr->next; } ptr->next = n->next; return n; } }