11
Linked List Linked List L L i i n n k k e e d d www.eshikshak.co.i www.eshikshak.co.i n n

Linked list

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Linked list

Linked ListLinked List

LL

ii

nn

kk

ee

dd

www.eshikshak.co.inwww.eshikshak.co.in

Page 2: Linked list

Limitations of ArrayLimitations of Array To insert or remove an element at an interior location in an To insert or remove an element at an interior location in an

ArrayList requires shifting of data and is an O(n) operation.ArrayList requires shifting of data and is an O(n) operation.

www.eshikshak.co.inwww.eshikshak.co.in

Page 3: Linked list

Limitations of ArrayLimitations of Array An array has a limited number of elementsAn array has a limited number of elements

routines inserting a new value have to check that there is roomroutines inserting a new value have to check that there is room Can’t increase or decrease its size during executionCan’t increase or decrease its size during execution

Can partially solve this problem by reallocating the array as Can partially solve this problem by reallocating the array as needed (how much memory to add?)needed (how much memory to add?) adding one element at a time could be costlyadding one element at a time could be costly one approach - double the current size of the arrayone approach - double the current size of the array

Array elements are stored in contiguous memory locationsArray elements are stored in contiguous memory locations Always we won’t have enough contiguous memoryAlways we won’t have enough contiguous memory

Q: how do we know what is part of the array?Q: how do we know what is part of the array?A: have the elements keep track of each otherA: have the elements keep track of each other use pointers to connect the elements together as a use pointers to connect the elements together as a LISTLIST of things of things

www.eshikshak.co.inwww.eshikshak.co.in

Page 4: Linked list

Introduction to Linked ListIntroduction to Linked List It is linear collection of data items, called It is linear collection of data items, called nodes – nodes – where linear where linear

order is maintain by means of order is maintain by means of pointerspointers

Where each item contains two parts or fields

Data : Information or actual value of an element

Link : It contains the address to its successor (and sometimes its predecessor)

DataData LinkLink

www.eshikshak.co.inwww.eshikshak.co.in

Page 5: Linked list

Introduction to Linked ListIntroduction to Linked List How to define the linked list data strcture in ‘C’How to define the linked list data strcture in ‘C’

struct linklist

{

int data;

struct linklist *link;

}

DataData LinkLink

www.eshikshak.co.inwww.eshikshak.co.in

Page 6: Linked list

Introduction to Linked ListIntroduction to Linked List

The entire linked list is accessed from an external pointer list that points to first node in the list

The link field of the last node in the list contains a special value, known as NULLNULL

The NULL NULL pointer is used to signal the end of a list

‘‘S’S’ 4545 ‘‘O’O’ 2222 ‘‘NN’’

9999 ‘‘U’U’ NULLNULL

45 22 99

list

www.eshikshak.co.inwww.eshikshak.co.in

Page 7: Linked list

Introduction to Linked ListIntroduction to Linked List

The list with no nodes on it is called the The list with no nodes on it is called the empty list or the or the null list

A list can be initialized to the empty list by list = NULLA list can be initialized to the empty list by list = NULL

Need way to indicate end of list (NULL pointer)Need way to indicate end of list (NULL pointer)

Need to know where list starts (first element)Need to know where list starts (first element)

Each element needs pointer to next element (its link)Each element needs pointer to next element (its link)

Need way to allocate new element (use malloc)Need way to allocate new element (use malloc)

Need way to return element not needed any more (use free)Need way to return element not needed any more (use free)

Divide element into data and pointerDivide element into data and pointer

‘‘S’S’ 4545 ‘‘O’O’ 2222 ‘‘NN’’

9999 ‘‘U’U’ NULLNULL

45 22 99

list

www.eshikshak.co.inwww.eshikshak.co.in

Page 8: Linked list

Types of Linked List Types of Linked List

There are 4 different kinds of linked lists:There are 4 different kinds of linked lists:Linear singly linked listLinear singly linked list

Circular singly linked listCircular singly linked list

Two way or doubly linked listTwo way or doubly linked list

Circular doubly linked list.Circular doubly linked list.

‘‘S’S’ 4545 ‘‘O’O’ 2222 ‘‘NN’’

9999 ‘‘U’U’ NULLNULL

45 22 99

list

www.eshikshak.co.inwww.eshikshak.co.in

Page 9: Linked list

Types of Linked List Types of Linked List

The basic operations of Linked ListThe basic operations of Linked List

Traverse Traverse

InsertInsert

At BeginningAt Beginning

At EndAt End

At given positionAt given position

DeleteDelete

At BeginningAt Beginning

At EndAt End

At given positionAt given position

UpdateUpdate

At BeginningAt Beginning

At EndAt End

At given positionAt given position

‘‘S’S’ 4545 ‘‘O’O’ 2222 ‘‘NN’’

9999 ‘‘U’U’ NULLNULL

45 22 99

list

www.eshikshak.co.inwww.eshikshak.co.in

Page 10: Linked list

Sample Linked List OperationsSample Linked List OperationsStruct linklistStruct linklist

{{

int data;int data;

Struct linklist *next;Struct linklist *next;

}}

ListStart

100

ListStart

0

Pictorially In Memory

ListStart

? ? 108

100

ListStart

? ?

108Data Next

Data Next

ListStart = (linklist *) malloc(sizeof(linklist)); /* ListStart points to memory allocated at location 108 */

www.eshikshak.co.inwww.eshikshak.co.in

Page 11: Linked list

Sample Linked List Operations Sample Linked List Operations (cont)(cont)

ListStart->data = 5;ListStart->data = 5;

ListStart->next = (EPtr) malloc(sizeof(EStruct));

ListStart->next = NULL;

ListStart

5 ? 108

100

ListStart

5 ?

108

ListStart

5 108

100

ListStart

5 0

108

ListStart

5 ? ? 108

100

ListStart

5 120

108

? ?

120

ListStart->next->data = 9;ListStart->next->next = NULL;

ListStart

5 9 108

100

ListStart

5 120

108

9 0

120