Link list(by harshit)

Post on 13-Apr-2017

214 views 0 download

Transcript of Link list(by harshit)

MOHAN LAL SUKHADIA UNIVERSITY College of Science

LINKED LIST(SINGLY && DOUBLY)

PRESENTING BY:-HARSHIT JAIN

LINKED LIST :-

It is a non-primitive data structure. In a link list elements are arranged in line by line manner link list is a collection of “NODES”.Every node contain two or more then two part.(1.)Data part:- It is used to hold the data or information.(2.)Address part:- It is used to hold the address of next or previous node

What is linked list……?Node 1 Node 3Node 2

A linked list is a collection of objects linked together by references from one object to

another object.

By convention these objects are named as nodes.

So the basic linked list is collection of nodes where each node contains one or more data

fields and a reference to the next node.

The last node points to a NULL reference to indicate the end of the list.

Both an array and a linked list are representations of a list of items in memory. The difference is in the way in which the items are linked together.

10S [1]

20 30 40 50S [2] S [3] S [4] S [5]

Compare the two representations for a list of five integers.

Array representation : -

Linked List representation : -

10 20 30 40 50 XS

Difference between ARRAY & LINKED LIST…..!!!

4672 8814851976497549

There are three types of linked list:-

Singly linked list

Circular linked list

Doubly linked list

10 20

S

2355 3255

10 2032552355

1010

S

S

23553255

Creating a Linked List

Designing the 1st Node. Allocating memory for the node. Adding more nodes as per requirement. Connecting the nodes, & setting last node’s address part to NULL.

Designing the node

Node

struct node{int data;struct node *next;}

struct node{ int data;struct node *lptr;struct node *rptr;}

Node

10 2355 202355 3255

Allocating memory

node *newnode = (node *) malloc (sizeof (node) );

Operations on linked list….!!!

Adding 1st node Insertion

Search Display

Deletion

Singly linked list

Insertion:-

Step 1:- StartStep 2:- a=NewNodeStep 3:- Read a->dataStep 4:- a->next=NULLStep 5:- Start=aStep 6:- Stop

Creating 1st node

a

10a

10a

10Start

Insertion:-at the beginning

a

10Start

10a

Step 1:- StartStep 2:- a=NewNodeStep 3:- Read a->dataStep 4:- a->next=StartStep 5:- Start=aStep 6:- Stop

20a Start 10

20start 10

Insertion:-at the end

Step 1:- StartStep 2:- ptr=startStep 3:- while(ptr!=NULL)Step 4:- ptr=prt->nextStep 5:- a=NewNodeStep 6:- Read a->dataStep 7:- a->next=NULLStep 8:- ptr->next=aStep 9:- [End of while step 3]Step 10:- Stop

20start 10ptr

20start 10 30

Insertion:-at middle:-

Step 1: BeginStep 2: ptr = StartStep 3: while (ptr!=NULL)Step 4: back = ptrStep 5: ptr = ptr -> nextStep 6: if (ele = ptr -> data) thenStep 7: a = New NodeStep 8: Read a -> dataStep 9: back -> next = aStep 10: a -> next = ptrStep 11: [End if of step 6]Step 12: [End while pf step 2]Step 13: End

20start 10

20start 30 10

Step 1: BeginStep 2: if (Start->next = NULL) thenStep 3: print deletion not possible.Step 4: [End if of step 2]Step 5: ptr = StartStep 6: Start = Start -> nextStep 7: free (ptr)Step 8: End

Deletion

from the beginning

20start 10

start 10

Step 1: BeginStep 2: ptr = StartStep 3: while (ptr != NULL)Step 4: back = ptrStep 5: ptr = ptr -> nextStep 6: [End while of step 3]Step 7: back -> next = NULLStep 8: free (ptr)Step 9: End

Deletion

from the end

20 start 10

20 start

Step 1: BeginStep 2: ptr = StartStep 3: while (ptr != NULL)Step 4: back = ptrStep 5: ptr = ptr -> nextStep 6: if (ele = ptr -> data) thenStep 7: back -> next = ptr -> nextStep 8: free (ptr)Step 9: [End if of step 6]Step 10: [End while of step 3]Step 11: End

Deletion

from the middle

start 10 20 30

start 10 30

Doubly linked list

Step 1:- BeginStep 2:- a=NewNodeStep 3:- Read a->dataStep 4:- a->lptr=NULLStep 5:- a->rptr=NULLStep 6:- start=aStep 7:- End

Creating 1st node

Insertion

start 102355

Step 1:- BeginStep 2:- a=NewNodeStep 3:- read a->dataStep 4:- a->rptr=startStep 5:- a->lptr=NULLStep 6:- start->lptr=aStep 7:- start=aStep 8:- End

At the beginning

Insertion

start 102355

start 202255

102355

Step 1:- BeginStep 2:- ptr=startStep 3:- while(ptr->rptr=NULL)Step 4:- ptr=ptr->rptrStep 5:-[end of while step 3]Step 6:- a=NewNodeStep 7:- Read a->dataStep 8:- start->rptr=aStep 9:- a->lptr=startStep 10:- a->lptr=NULLStep 11:- End

At the End

Insertion

start 102355

2355start 10 20

3355

Step 1: BeginStep 2: ptr = StartStep 3: while (ptr != NULL)Step 4: back = ptr Step 5: ptr = ptr -> rptrStep 6: if (ele = ptr -> data)Step 7: a = New NodeStep 8: Read a -> dataStep 9: back -> rptr = a -> lptrStep 10: a -> rptr = ptr -> rptrStep 11: a -> lptr = a -> rptrStep 12: ptr -> lptr = a -> rptrStep 13: [End if of step 6]Step 14: [End while of step 3]Step 15: End

At the middle

Insertion

2355start 10 20

3355

203355

3024552355

start 10

Deletion

from the beginning

Step 1:- BeginStep 2:- if(start=NULL)Step 3:- print “empty list”Step 4:-[End of if step 2]Step 5:- else if(strat->rptr=NULL)Step 6:- print “list have only one element”Step 7:- start=NULLStep 8:- [End of else if step 5]Step 9:- elseStep 10:- ptr=startStep 11:- ptr->rptr->lptr=NULLStep 12:- start=ptr->rptrStep 13:- free(ptr)Step 14:- [End of else step 9]Step 15:- End

start 102355

2355start 10 20

3355

start 203355

Deletion

from the End

Step 1:- BeginStep 2:- if(start=NULL) thenStep 3:- print "Empty list"Step 4:- elseStep 5:- ptr=startStep 6:- while(ptr->rptr!=NULL)Step 7:- back=ptrStep 8:- ptr=ptr->rptrStep 9:- [End of while step 6]Step 10:- ptr->lptr=NULLStep 11:- back->rptr=NULLStep 12:- [End of if step 2]Step 13:- End

2355start 10 20

3355

start 102355

THANK YOU