Linked list

21
(Download file to view animation) Dr. S. Lovelyn Rose PSG College of Technology Coimbatore, India N L I T K E D L I S

description

Insertion and deletion in singly linked lists and doubly linked lists using animation. Will be useful for

Transcript of Linked list

Page 1: Linked list

(Download file to view animation)

Dr. S. Lovelyn Rose

PSG College of Technology

Coimbatore, India

NL I TK E D LI S

Page 2: Linked list

SINGLY LINKED LIST

DOUBLY LINKED LIST

Previous node Data part Next

node

NODE STRUCTURE

DATAADDRES

S

Data 1 Data 2 . . . . . . Data n

ADDRESS

ADDRESS

Data 1 Data 2 . . . . . Data n

ADDRESS

ID Name Desig Address of Node2 ID Name Desig Address of Node 3 ID Name Desig NULL

Start

NULL Data 1 Address of Node 2Address of Data 2 Address of Node 1 Node 3Address of Data 3 Node 2 NULL

Start

Page 3: Linked list

CIRCULAR LINKED LISTSINGLY

Node 1 Node 2 Node 3

DOUBLY

Node 1 Node 2

Node 3

Start

Data 1 Node 2

Data 2 Node 3

Data 3 Node1

Start

Node3 Data 1 Node 2

Node 1 Data 1 Node 3

Node 2 Data 1 Node1

Page 4: Linked list

struct node{

int x; char c[10];struct node *next;}*current;

Node Structure

x c[10] next

Page 5: Linked list

create_node(){ current=(struct node *)malloc(sizeof(struct

node)); current->x=10; current->c=“try”; current-

>next=null; return current;}Allocation

Assignment

Node Creation

x c[10] next

10 try NULL

Page 6: Linked list

Create a linked list for maintaining the employee details such as Ename,Eid,Edesig.

Steps:1)Identify the node structure2)Allocate space for the node3)Insert the node in the list

Inserting N Nodes

EID EName EDesig

struct node

{

int Eid;

char Ename[10],Edesig[10];

struct node *next;

} *current;

next

Page 7: Linked list

Insert_N_Nodes(N){

Start=current=create_node();

AlgorithmNumber of nodes

Allocation and Assignment

011 ABI HR

Start, current

NULL

Page 8: Linked list

for(i=1;i<n-1;i++){

current->next=create_node();current=current->next;

}}

011 ABI HR

Start, current

012 Banu DBAi=1 NULL

Page 9: Linked list

Insert(){

c=create_node();start=c;c1=create_node();c->next=c1;c2=create_node();c1->next=c2;

}

Inserting 2 nodes

10 c

20 c1

30 c2

start

Page 10: Linked list

Insert_a_node(Start){current= create_node();current->next=Start;Start=current;}

Note : Passing Start as a parameter implies the list already exists

Insertion at the first positionStart

20 30

10Start

20

30

10

current

Page 11: Linked list

Steps:1)Have a pointer (slow) to the node after which

the new node is to be inserted 2)Create the new node to be inserted and store

the address in ‘current’3) Adjust the pointers of ‘slow’ and ‘current’

Insertion at the middle

Start

10 20 30 40

25

Page 12: Linked list

Use two pointers namely, fast and slowMove the ‘slow’ pointer by one element in the

listFor every single movement of ‘slow’, move

‘fast’ by 2 elementsWhen ‘fast’ points to the last element, ‘slow’

would be pointing to the middle elementThis required moving through the list only

onceSo the middle element is found in a time

complexity of O(n)

Finding the middle element of alinked list in O(n) time complexity

Page 13: Linked list

{current=(struct node *)malloc(sizeof(struct node))fast=slow=Startdo{

if(fast->next->next!=null)fast=fast->next->next

else break;slow=slow->next

}while(fast->next!=null)current->next=slow->nextslow->next=current}Note : fast and slow are below the node they point to

Insert_Middle(Start)

Start 10

20

30

40

25

fast slow

current

Page 14: Linked list

Insert_last(Start){

current=Start;while(current->next!=null){

current=current->next;}temp=create_node();current->next=temp;

}

Insertion at the last

Start 10

20

30

40

25

temp

Page 15: Linked list

delete_node(start,x){

temp=start;temp1=temp;while(temp!=null){if(temp->n==x){if(temp==start && temp->next==null)

start=null

Delete a node with a value x

Last node

Only one nodestart

10

temp

Page 16: Linked list

else if(temp==start){

start=start->next;temp->next=null;break;

}else if(temp->next==null){

temp1->next=null;}

Cont..

First node

start

10 temp

20

Last node

start

10

20

temp1

temp

Page 17: Linked list

else{

temp1->next=temp->next;temp->next=null;break;

}}temp1=temp;temp=temp->next;}}

Cont..

Any node

Start 50

20

10

40

temp1

temp

Page 18: Linked list

Inserting N nodesInsert_node(N){start=c=create_node();for(i=0;i<N-1;i++){

c1=create_node();c->next=c1;c1->previous=c;

}}

Doubly Linked List

create_node(){c=(struct node*)malloc(sizeof(struct

node)) c->x;

c->previous=null;c->next=null;return c;

}

10

cstart

20

c1

Page 19: Linked list

delete_node(start,n){temp=startwhile(temp!=null){if(temp->x==n){ if(temp==start) {temp->next->previous=null;start=temp->next}

Node Deletion

End of list

single node

10

temp

start

Page 20: Linked list

else if(temp->next==null) temp->previous->next=null;else{temp->next->previous=temp->previous;temp->previous->next=temp->next;}}else temp=temp->next;}if(temp==null) print(“Element not found”);}

Last node

20

start

10

temp

middle node

30

Page 21: Linked list

http://datastructuresinterview.blogspot.in/

http://talkcoimbatore.blogspot.in/

http://simpletechnical.blogspot.in/

My Blogs