linked list without animation

23
Dr. S. Lovelyn Rose PSG College of Technology N L I T K E D L I S

Upload: lovelyn-rose

Post on 11-May-2015

1.497 views

Category:

Education


1 download

DESCRIPTION

Insertion and Deletion in Singly, Doubly Linked List

TRANSCRIPT

Page 1: Linked list without animation

Dr. S. Lovelyn Rose

PSG College of Technology

NL I TK E D LI S

Page 2: Linked list without animation

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

Page 3: Linked list without animation

Singly Linked List

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

Start

Page 4: Linked list without animation

Doubly Linked List

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

Start

Page 5: Linked list without animation

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 6: Linked list without animation

struct node{

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

Node Structure

x c[10] next

Page 7: Linked list without animation

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 8: Linked list without animation

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 9: Linked list without animation

Insert_N_Nodes(N){

Start=current=create_node();

AlgorithmNumber of nodes

Allocation and Assignment

011 ABI HR

Start, current

NULL

Page 10: Linked list without animation

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 11: Linked list without animation

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 12: Linked list without animation

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 13: Linked list without animation

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 14: Linked list without animation

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 15: Linked list without animation

{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 16: Linked list without animation

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 17: Linked list without animation

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 18: Linked list without animation

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 19: Linked list without animation

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 20: Linked list without animation

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 21: Linked list without animation

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 22: Linked list without animation

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 23: Linked list without animation

Download the file from http://

www.slideshare.net/lovelynrose/linked-list-17752737

Also visit my blogs :

http://datastructuresinterview.blogspot.in/

http://talkcoimbatore.blogspot.in/

http://simpletechnical.blogspot.in/

To View With Animation