Linked List
1
Presented by: Rabin BK
Overview of linked list
Inserting a node at a given position
Algorithm
Program
References
2
A sequence of data structures which are connected together via
links
Each link contains a data and data address in connection to another
link
Linked list is the second most used data structure after array
3
Inserting a node at a given position
4
19 100 45 200
100
78 300
200
78 NULL
300
Head= 500
500
5
19 100 45 200
100
78 300
200
78 NULL
300
Head= 500
1 2 3 4
500
struct bag{
data;
struct bag *link;
};
struct bag *node,*temp;
node=size of new structure type node
node->data=x; //Value to be inserted
temp=head;
loop(until int i<position)//traverse
{
temp=temp->link;
}
node->link=temp->link;
temp->link=node;
6
Algorithm
45 200
100
78 300
200
2 3
struct bag{
int data;
struct bag *link;
}
void insert(int x)//value of x is passed from the main function
{
int i;
struct bag *new_node,*temp;
new_node=(struct bag *)malloc(sizeof(struct bag));
node->data=x; //Value to be inserted
temp=*head; //temp=500
for(i=0;i<pos;i++)
{
temp=temp->link;
}
node->link=temp->link;
temp->link=node;
}
7
Program
19 100Head= 500
1
500
x link
address
New node
45 200
100
78 300
200
2 3
8
Finally added node in the 3rd position
19 100 45 400
100
78 600
300
78 NULL
600
Head= 500
1 2 3 4
500
78 300
400
5
References:
Text book:
 Data structure using C/C++, Langsam, Augenstein, Tenenbaum
Web:
http://blog.grkweb.com/2015/04/hackerrank-insert-node-at-
specific.html
https://unacademy.com/lesson/data-structures-insert-a-node-at-the-nth-
position-of-a-linked-list/YHCFSFYX
http://www.knowledge-share.in/c-program-to-insert-a-node-at-front-at-
end-and-at-any-position-in-a-single-linked-list/#.WW9SOOC0nIV
9
Queries
10

Linked list in Data structure

  • 1.
  • 2.
    Overview of linkedlist Inserting a node at a given position Algorithm Program References 2
  • 3.
    A sequence ofdata structures which are connected together via links Each link contains a data and data address in connection to another link Linked list is the second most used data structure after array 3
  • 4.
    Inserting a nodeat a given position 4 19 100 45 200 100 78 300 200 78 NULL 300 Head= 500 500
  • 5.
    5 19 100 45200 100 78 300 200 78 NULL 300 Head= 500 1 2 3 4 500
  • 6.
    struct bag{ data; struct bag*link; }; struct bag *node,*temp; node=size of new structure type node node->data=x; //Value to be inserted temp=head; loop(until int i<position)//traverse { temp=temp->link; } node->link=temp->link; temp->link=node; 6 Algorithm 45 200 100 78 300 200 2 3
  • 7.
    struct bag{ int data; structbag *link; } void insert(int x)//value of x is passed from the main function { int i; struct bag *new_node,*temp; new_node=(struct bag *)malloc(sizeof(struct bag)); node->data=x; //Value to be inserted temp=*head; //temp=500 for(i=0;i<pos;i++) { temp=temp->link; } node->link=temp->link; temp->link=node; } 7 Program 19 100Head= 500 1 500 x link address New node 45 200 100 78 300 200 2 3
  • 8.
    8 Finally added nodein the 3rd position 19 100 45 400 100 78 600 300 78 NULL 600 Head= 500 1 2 3 4 500 78 300 400 5
  • 9.
    References: Text book:  Datastructure using C/C++, Langsam, Augenstein, Tenenbaum Web: http://blog.grkweb.com/2015/04/hackerrank-insert-node-at- specific.html https://unacademy.com/lesson/data-structures-insert-a-node-at-the-nth- position-of-a-linked-list/YHCFSFYX http://www.knowledge-share.in/c-program-to-insert-a-node-at-front-at- end-and-at-any-position-in-a-single-linked-list/#.WW9SOOC0nIV 9
  • 10.