74
Data Structure & Algorithm Linear Data Structure : Linked List

Linear Data Structure : Linked Lists

Embed Size (px)

Citation preview

Page 1: Linear Data Structure : Linked Lists

Data Structure & Algorithm

Linear Data Structure : Linked List

Page 2: Linear Data Structure : Linked Lists

2

Problem With Array • Fixed Length (Occupy a Block of

memory)

• To expand an Array– create a new array, longer in size and – copy the contents of the old array into

the new array

• Insertion or Deletion

Page 3: Linear Data Structure : Linked Lists

3

Solution • Attach a pointer to each item

in the array, which points to the next item

–This is a linked list–An data item plus its pointer is called a node

Page 4: Linear Data Structure : Linked Lists

4

Linked List node

• Each node contains at least–A piece of data (any type)–Pointer to the next node in the

list

data

pointer

nodeA

Page 5: Linear Data Structure : Linked Lists

5

malloc & free• void * malloc(n); -- allocates a

memory block of n bytes dynamically and returns the base address of the block

• int * ptr; ptr = malloc(sizeof(int))

1100 11011100ptr

Page 6: Linear Data Structure : Linked Lists

6

malloc & free• void free(void * ptr); --

deallocates memory block (dynamically created) pointed by ptr

• free(ptr)

Page 7: Linear Data Structure : Linked Lists

7

Node structure in Cstruct NODE {

int DATA;struct NODE * Next;

};struct NODE *ptr;ptr = malloc(sizeof(struct NODE))

11001100ptr

Page 8: Linear Data Structure : Linked Lists

8

Linked List

• A linked list, or one-way list, is a linear collection of data elements , called nodes, where the linear order is given by means of pointer

Page 9: Linear Data Structure : Linked Lists

9

Linked List

• A linked list is a series of connected nodes

• Head : pointer to the first node• The last node points to NULL

HeadA B C

Page 10: Linear Data Structure : Linked Lists

10

List Traversal

Head

Data Link

Page 11: Linear Data Structure : Linked Lists

11

=

Head

Data Link

PTR

Page 12: Linear Data Structure : Linked Lists

12

=

Head

Data Link

PTR = PTR - > Link

Page 13: Linear Data Structure : Linked Lists

13

List Traversal

Let Head be a pointer to a linked list in memory. Write an algorithm to print the contents of each node of the list

Page 14: Linear Data Structure : Linked Lists

14

List Traversal Algorithm1. set PTR = Head 2. Repeat step 3 and 4 while

PTR NULL3. Print PTR->DATA4. Set PTR = PTR -> LINK5. Stop

Page 15: Linear Data Structure : Linked Lists

15

Search for an ITEM

• Let Head be a pointer to a linked list in memory. Write an algorithm that finds the location LOC of the node where ITEM first appears in the list, or sets LOC = NULL if search is unsuccessful.

Page 16: Linear Data Structure : Linked Lists

16

Search for an ITEM

Algorithm1. Set PTR = Head 2. Repeat step 3 while PTR NULL3. if ITEM == PTR -> DATA, then

Set LOC = PTR, and Exitelse Set PTR = PTR -> LINK

4. Set LOC = NULL /*search unsuccessful */

5. Stop

Page 17: Linear Data Structure : Linked Lists

17

Search for an ITEM

Algorithm [Sorted ]1. Set PTR = Head 2. Repeat step 3 while PTR NULL3. if ITEM < PTR -> DATA, then

Set PTR = PTR->LINK, else if ITEM == PTR->DATA, thenSet LOC = PTR, and Exit else Set LOC = NULL, and Exit

4. Set LOC = NULL /*search unsuccessful */5. Stop

Page 18: Linear Data Structure : Linked Lists

18

Insertion to a Linked List

Head

Data Link

Page 19: Linear Data Structure : Linked Lists

19

Insertion to Beginning

Head

Data Link

New Node PTR

Page 20: Linear Data Structure : Linked Lists

20

Insertion to Beginning

Head

Data Link

New Node PTRPTR->Link = Head

Page 21: Linear Data Structure : Linked Lists

21

Insertion to Beginning

Head

Data Link

New Node PTRPTR->Link = Head , Head = PTR

Page 22: Linear Data Structure : Linked Lists

22

Overflow and Underflow• Overflow : A new Data to be

inserted into a data structure but there is no available space.

• Underflow: A situation where one wants to delete data from a data structure that is empty.

Page 23: Linear Data Structure : Linked Lists

23

Insertion to Beginning

Head

Data Link

New Node PTROverflow , PTR == NULL

Page 24: Linear Data Structure : Linked Lists

24

Insertion at the Beginning

Let Head be a pointer to a linked list in memory. Write an algorithm to insert node PTR at the beginning of the List.

Page 25: Linear Data Structure : Linked Lists

25

Insertion at the BeginningAlgorithm

1.PTR = create new node 2.If PTR == NULL , then Write

Overflow and Exit3. Set PTR -> DATA = ITEM 4. Set PTR -> LINK = Head5. Set Head = PTR 6. Exit

Page 26: Linear Data Structure : Linked Lists

26

Insertion After a Given Node

Let Head be a pointer to a linked list in memory. Write an algorithm to insert ITEM so that ITEM follows the node with location LOC or insert ITEM as the first node when LOC == NULL

Page 27: Linear Data Structure : Linked Lists

27

Insertion at a Given Node

Head

Data Link

New Node PTR

A BALOC

Page 28: Linear Data Structure : Linked Lists

28

Insertion at a Given Node

Head

Data Link

New Node PTR

BALOC

PTR->Link = LOC->Link

Page 29: Linear Data Structure : Linked Lists

29

Insertion at a Given Node

Head

Data Link

New Node PTR

BALOC

LOC->Link = PTR

Page 30: Linear Data Structure : Linked Lists

30

Insertion After a Given Node Algorithm

1.PTR = create new node 2.If PTR == NULL , then Write Overflow

and Exit3. Set PTR -> DATA = ITEM 4. If LOC == NULL

Set PTR -> LINK = HeadSet Head = PTR

Else Set PTR->Link = LOC->LinkSet LOC->Link = PTR

5. Exit

Page 31: Linear Data Structure : Linked Lists

31

Insertion into a Sorted Linked List

Head

Data Link

1000 2000 3000 4000 5000

35003000 < 3500 < 4000

Page 32: Linear Data Structure : Linked Lists

32

Insertion into a Sorted Linked List

1000 2000 3000 4000 5000

3500 < 4000BA

To Insert Between Node A and B We have to Remember the Pointer to Node A which is the predecessor Node of B

Page 33: Linear Data Structure : Linked Lists

33

Insertion into a Sorted Linked ListSteps to Find the LOC of Insertion

1. If Head == NULL, then Set LOC = NULL and Return

2. If ITEM < Head ->Data , then Set LOC = NULL and Return

3. Set SAVE = Head and PTR = Head -> Link 4. Repeat Steps 5 and 6 while PTR NULL5. If ITEM < PTR -> Data then

LOC = SAVE and Return6. Set SAVE = PTR and PTR = PTR->Link7. Set LOC = SAVE 8. Return

Page 34: Linear Data Structure : Linked Lists

34

Insertion into a Sorted Linked List

1000 2000 3000 4000 5000

SAVE = Head;

BA

ITEM = 3500

SAVE

PTR = HEAD -> Link;

PTR

Page 35: Linear Data Structure : Linked Lists

35

Insertion into a Sorted Linked List

1000 2000 3000 4000 5000

SAVE = PTR;

ITEM = 3500

SAVE

PTR = PTR -> Link;

PTR

Page 36: Linear Data Structure : Linked Lists

36

Insertion into a Sorted Linked List

1000 2000 3000 4000 5000

SAVE = PTR;

ITEM = 3500

SAVE

PTR = PTR -> Link;

PTR

LOC = SAVE

LOC

Page 37: Linear Data Structure : Linked Lists

37

Deletion Algorithm

A

Page 38: Linear Data Structure : Linked Lists

38

Deletion Algorithm

A

Page 39: Linear Data Structure : Linked Lists

39

Delete the Node Following a Given Node

• Write an Algorithm that deletes the Node N with location LOC. LOCP is the location of the node which precedes N or when N is the first node LOCP = NULL

Page 40: Linear Data Structure : Linked Lists

40

Delete the Node Following a Given Node

• Algorithm: Delete(Head, LOC, LOCP)

1 If LOCP = NULL thenSet Head = Head ->Link. [Deletes the 1st Node]

Else Set LOCP->Link = LOC->Link [Deletes Node N]

2. Exit

Page 41: Linear Data Structure : Linked Lists

41

Delete an ItemLet Head be a pointer to a linked list in memory that contains integer data. Write an algorithm to delete node which contains ITEM.

Page 42: Linear Data Structure : Linked Lists

42

Delete an Item

1000 2000 3000 4000 5000

4000BA

To delete a Node [Node B] We have to Remember the Pointer to its predecessor [Node A]

Page 43: Linear Data Structure : Linked Lists

43

Deletion of an ITEMAlgorithm

1. Set PTR=Head and SAVE = Head2. If Head->DATA == ITEM Head = Head -> Link PTR -> Link = NULL; 3. Else

PTR = PTR -> Link4. Repeat step 5 while PTR NULL5. If PTR->DATA == ITEM, then

Set SAVE->LINK = PTR -> LINK, exit else SAVE = PTR PTR = PTR -> LINK

6. Stop

Page 44: Linear Data Structure : Linked Lists

44

Header Linked Lists• A header linked list is a linked list

which always contains a special node called header node

• Grounded Header List: A header list where the last node contains the NULL pointer.

Header Node

Page 45: Linear Data Structure : Linked Lists

45

Header Linked Lists

• Circular Header List: A header list where the last node points back to the header node.

Header Node

Page 46: Linear Data Structure : Linked Lists

46

Header Linked Lists• Pointer Head always points to the

header node.

• Head->Link == NULL indicates that a grounded header list is empty

• Head->Link == Head indicates that a circular header list is empty

Page 47: Linear Data Structure : Linked Lists

47

Header Linked Lists• The first node in a header list is the

node following the header node• Circular Header list are frequently

used instead of ordinary linked list– Null pointer are not used, hence all

pointer contain valid addresses– Every node has a predecessor, so the

first node may not require a special case.

Page 48: Linear Data Structure : Linked Lists

48

Traversing a Circular Header List

• Let Head be a circular header list in memory. Write an algorithm to print Data in each node in the list.

Page 49: Linear Data Structure : Linked Lists

49

Traversing a Circular Header List Algorithm

1. Set PTR = Head->Link;2. Repeat Steps 3 and 4 while PTR

Head3. Print PTR->Data4. Set PTR = PTR ->Link5. Exit

Page 50: Linear Data Structure : Linked Lists

50

Locating an ITEM

• Let Head be a circular header list in memory. Write an algorithm to find the location LOC of the first node in the list which contains ITEM or return LOC = NULL when the item is not present.

Page 51: Linear Data Structure : Linked Lists

51

Locating an ITEM Algorithm

1. Set PTR = Head->Link2. Repeat while PTR Head

If PTR->Data == ITEM thenSet LOC = PTR and exit ElseSet PTR = PTR ->Link

3. Set LOC = NULL4. Exit

Page 52: Linear Data Structure : Linked Lists

52

Other variation of Linked List • A linked list whose last node points

back to the first node instead of containing a NULL pointer called a circular list

Page 53: Linear Data Structure : Linked Lists

53

Other variation of Linked List

• A linked list which contains both a special header node at the beginning of the list and a special trailer node at the end of list

Header Node Trailer Node

Page 54: Linear Data Structure : Linked Lists

Applications of Linked Lists

1. Polynomial Representation and operation on Polynomials

Ex: 10 X 6 + 20 X 3 + 55 

2. Sparse Matrix Representation

0 0 1122 0 00 0 66

Page 55: Linear Data Structure : Linked Lists

021021 ...)( ee

me

m xaxaxaxA mm

Representation of Node

Polynomials

coef expon link

Page 56: Linear Data Structure : Linked Lists

3 14 2 8 1 0a

8 14 -3 10 10 6b

123 814 xxa

61014 1038 xxxb

null

null

Example

Page 57: Linear Data Structure : Linked Lists

Polynomial Operation

1. Addition 2. Subtraction 3. Multiplication4. Scalar Division

Page 58: Linear Data Structure : Linked Lists

3 14 2 8 1 0a

-3 108 14 10 6b

11 14d

a->expon == b->expon

a

b

da->expon < b->expon-3 10

Polynomial Addition

3 14 2 8 1 0

-3 108 14 10 6

11 14

Page 59: Linear Data Structure : Linked Lists

3 14 2 8 1 0a

8 14 -3 10 10 6b

11 14a->expon > b->expon

-3 10d

2 8

Polynomial Addition

Page 60: Linear Data Structure : Linked Lists

3 14 2 8

1 0

a8 14 -3 10 10 6

b11 14

a->expon > b->expon-3 10

d2 8

Polynomial Addition (cont’d)

10 6

1 0

Page 61: Linear Data Structure : Linked Lists

3 14 2 8 1 0a

-3 108 14 10 6b

-5 14d

a->expon == b->expon

a

b

da->expon < b->expon3 10

Polynomial Subtraction

3 14 2 8 1 0

-3 108 14 10 6

-5 14

Page 62: Linear Data Structure : Linked Lists

3 14 2 8 1 0a

8 14 -3 10 10 6b

-5 14a->expon > b->expon

3 10d

2 8

Polynomial Subtraction (cont’d)

Page 63: Linear Data Structure : Linked Lists

3 14 2 8

1 0

a8 14 -3 10 10 6

b-5 14

a->expon > b->expon3 10

d2 8

Polynomial Subtraction (cont’d)

-10 6

1 0

Page 64: Linear Data Structure : Linked Lists

64

Two-Way List • What we have discussed till now is a

one-way list [ Only one way we can traversed the list]

• Two-way List : Can be traversed in two direction – Forward : From beginning of the list to

end– Backward: From end to beginning of the

list

Page 65: Linear Data Structure : Linked Lists

65

Two-Way List • A two-way list is a linear collection

of data element called nodes where each node N is divided into three parts:– A information field INFO which

contains the data of N– A pointer field FORW which contains

the location of the next node in the list– A pointer field BACK which contains

the location of the preceding node in the list

Page 66: Linear Data Structure : Linked Lists

66

Two-Way List • List requires two pointer variables:– FIRST: which points to the first node in

the list– LAST: which points to the last node in

the list INFO field BACK pointerFORW pointer

Page 67: Linear Data Structure : Linked Lists

67

Two-Way List

FIRST LAST

Page 68: Linear Data Structure : Linked Lists

68

Two-Way List Suppose LOCA and LOCB are the locations of nodes A and B respectively in a two-way list.

The statement that node B follows node A is equivalent to the statement that node A precedes node B

Pointer Property: LOCA->FORW = LOCB if and only if LOCB->BACK = LOCA

Page 69: Linear Data Structure : Linked Lists

69

Two-Way List

FIRST LAST

A BForward Pointer to Node A

Backward Pointer to Node A

Page 70: Linear Data Structure : Linked Lists

70

Two-Way List

FIRST LAST

A BLOCA LOCB

LOCA->FORW = LOCB if and only if LOCB->BACK = LOCA

Page 71: Linear Data Structure : Linked Lists

71

Operation in two-way list

• Traversing • Searching • Deleting • Inserting

Page 72: Linear Data Structure : Linked Lists

72

Deletion in Two-Way List

FIRST LAST

A BLOCB

DELETE NODE B

C

LOCB->BACK->FORW

LOCB->FORW =

Page 73: Linear Data Structure : Linked Lists

73

Deletion in Two-Way List

FIRST LAST

A BLOCB

DELETE NODE B

C

LOCB->FORW->BACK

LOCB->BACK =

Page 74: Linear Data Structure : Linked Lists

74

Insertion in Two-Way List

FIRST LAST

A BLOCA

INSERT NODE NEW

C

NEW

LOCA->FORW->BACK = NEWNEW->FORW = LOCA->FORW

LOCA->FORW = NEW NEW->BACK = LOCA