1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.

Post on 30-Mar-2015

212 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

1

DATA STRUCTURE

S

2

LINKED LIST

3

PROS Dynamic in nature, so grow and shrink in size during

execution

Efficient memory utilization

Insertion can be done at specified location

Many complex applications can be carried out using Linked List

CONS Occupy more memory

Random access of data is some what cumbersome & Time consuming

WHY & WHY NOT ?

4

A linked list is a linear collection of specially designed data elements, called NODE, linked to one another by means of Pointer.

Each NODE is divided In two parts, first part contains the information and second part contains the address of the next node.

Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is.

The next field in the last node points to nothing. We will place the memory address NULL

Struct Node

{

};

TERMINOLOGY

int DATAStruct node *Next

DATA

Next

5

1051

1052

1055

1059

1060

1061

1062

1063

1064

1056

1057

1058

1053

1054 2

6

8

7

1

1051

1063

1057

1060

0

head 1054

1063

2 6 8 7 1

head

1065

ACTUAL PICTURE IN MEMORY

6

add(9): Create a new node in memory to hold ‘9’

Node* newNode = new Node(9);

Link the new node into the list

9newNode

2 6 8 7 1

head

current

size=5 6

9

newNode

1

3

2

OPERATION

7

2 6 8 7 1

headSINGLY

LINKED LIST

2 6 8 1head

DOUBLY LINKED LIST

2 6 8 7 1head

current

CIRCULAR LINKED LIST

TYPES

8

SINGLY LINKED LIST

9

headNode size=0List list;

2headNode

currentNode

size=1

lastcurrentNode

list.add(2);

2 6headNode

currentNode

size=2

lastcurrentNode

list.add(6);

list.add(8); list.add(7); list.add(1);

2 6 7 1headNode

currentNode

size=5

lastcurrentNode

8

10

30START

Create Node With DATA (30)

30START

Create Node With DATA (40) at END

40

10START

Create Node With DATA (10) at BEGINING

30 40

10START

Create Node With DATA (20) at SECOND Position

20 4030

10START

Create Node With DATA (20) at LAST Position

20 5030 40

11

Insert node at BEGINING

1. Input DATA to be inserted

2. Create a NewNode

3. NewNode -> DATA = DATA

4. If (START == NULL)

NewNode -> Next = NULL

else

NewNode -> Next = START

5. START = NewNode

6. Exit

ALGORITHM

12

1. Input DATA to be inserted

2. Create a NewNode

3. NewNode - > DATA = DATA

4. NewNode -> Next = NULL

4. If (START == NULL)

START = NewNode

else

TEMP = START

while (TEMP -> NEXT != NULL)

TEMP = TEMP -> NEXT

5. TEMP -> NEXT = NewNode

6. Exit

ALGORITHM Insert node at END

13

1. Input DATA & POS to be inserted

2. Initialize TEMP = START & K =0

3. Repeat step 3 while K < POS

(a) TEMP = TEMP -> NEXT

(b) if (TEMP == NULL)

“Node Is Not In The List”

Exit

(c) K = K + 1

4. Create a New Node

5. NewNode -> DATA = DATA

6. NewNode -> Next = TEMP -> NEXT

7. TEMP -> Next = NewNode

8. EXIT.

ALGORITHM Insert node at SPECIFIED LOCATION

14

1. Input DATA to be deleted

2. If (START-> DATA = = DATA)(a) TEMP = START(b) START = START -> Next(c) free (TEMP)(d) Exit

3. HOLD = START

4. While (HOLD -> Next -> Next != NULL)(a) if (HOLD -> Next ->DATA == DATA)

(i) TEMP = HOLD -> Next(ii) HOLD -> Next = TEMP ->

Next(iii) free (TEMP)(iv) Exit.

(b) HOLD = HOLD -> Next

5. if (HOLD -> Next -> DATA == DATA )(a) TEMP = HOLD -> Next(b) free (TEMP)(c) HOLD -> Next = NULL(d) Exit.

6. “DATA not Found”

7. Exit.

1st Location

Ith Location

Last Location

ALGORITHM DELETE NODE

15

1. Input DATA to be searched

2. Initialize TEMP = START & POS = 1

3. Repeat Steps 4, 5 & 6 until TEMP = NULL

4. If (TEMP -> DATA == DATA) (a) “DATA found at POS” (b) Exit.

5. TEMP = TEMP -> Next

6. POS = POS + 1

7. If (TEMP = NULL) “DATA Not Found in LIST”

8. Exit.

ALGORITHM SEARCHING NODE

16

1. If (START == NULL)

(a) “LIST is Empty”

(b) Exit.

2. Initialize TEMP =START

3. Repeat Step 4 and 5 Until TEMP = NULL

4. Display “TEMP -> DATA”

5. TEMP = TEMP -> Next

6. Exit.

ALGORITHM DISPLAY NODES

17

10 NULL Push (10)

TOP

20 Push (20)

10 NULLTOP

30 Push (30)

20TOP 10 NULL

20 Pop()

10 NULLTOP

40 Push (40)

20TOP 10 NULL

STACK – Using LINKED LIST

18

PUSH

1. Input DATA to be pushed

2. Create a New Node

3. NewNode -> DATA = DATA

4. NewNode -> Next = TOP

5. TOP = NewNode

6. Exit.

ALGORITHM

19

ALGORITHM POP

1. If ( TOP == NULL )

“STACK Is Empty”

2. Else

(a). TEMP = TOP

(b). “Poped Data : TOP -> DATA”

(c). TOP = TOP -> Next

(d). TEMP -> Next = NULL

(e). Free (TEMP)

3. Exit.

20

10 NULL Push (10)

Front

10 Push (20)

20 NULL

10 Push (30)

20 30 NULL

20 Pop()

30 NULL

20 Push (40)

30 40 NULL

Rear

Front Rear

Front

Front

Front

Rear

Rear

Rear

NULL

QUEUE – Using LINKED LIST

21

PUSH

1. Input DATA to be pushed

2. Create a New Node

3. NewNode -> DATA = DATA

4. NewNode -> Next = NULL

5. If ( REAR != NULL )

REAR -> Next = NewNode

6. REAR = NewNode

7. Exit.

ALGORITHM

22

ALGORITHM POP

1. If ( FRONT == NULL || FRONT > REAR )

“QUEUE Is Empty”

2. Else

(a). “Popped Data : FRONT -> DATA”

(b). If (FRONT != REAR )

FRONT = FRONT -> Next

Else

FRONT = NULL

3. Exit.

23

DOUBLY LINKED LIST

24

Moving forward in a singly-linked list is easy; moving backwards is not so easy.

To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current.

To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node

Struct node

{

}

TERMINOLOGY

int DATA;

Struct node *next;Struct node *prev;

DATA

next

prev

25

Need to be more careful when adding or removing a node.

size=52 6 8 7 1head

current

Insert Node

26

1. NewNode->Next = current->Next

size=52 6 8 7head

current

1

9newNode 1

Insert Node

27

size=52 6 8 7head

current

1

9newNode 1

2

2. NewNode->prev = current

Insert Node

28

size=52 6 8 7head

current

1

9newNode 1

2 3

3. current->next->prev = NewNode

Insert Node

29

size=52 6 8 7head

current

1

9newNode 1

2 34

4. current->next = NewNode

Insert Node

30

Size=62 6 8 7head

current

1

9newNode 1

2 34

5. current = NewNode

Insert Node

31

1. Input DATA & POS

2. Initialize TEMP = START & i=1

3. while ( i < POS ) & (TEMP != NULL)

TEMP = TEMP -> Next

4. If (TEMP != NULL) & I = POS

a). Create a NewNode

b). NewNode -> DATA = DATA

c). NewNode -> next = TEMP -> next

d). NewNode -> prev = TEMP

e). TEMP -> next - > prev = NewNode

f). TEMP - > next = NewNode

5. Else

“Position Not Found”

6. Exit

INSERT NODE

32

1. Input DATA to be deleted

2. Initialize TEMP = START

3. while (TEMP -> next -> DATA != DATA)

TEMP = TEMP -> next

5. HEAD = TEMP

TEMP = TEMP -> next

6. HEAD -> next = TEMP -> next

7. TEMP -> next -> prev = HEAD

8. Exit.

DELETE NODE

33

CIRCULAR LINKED LIST

34

The next field in the last node in a singly-linked list is set to NULL.

Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node.

A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list.

TERMINOLOGY

35

2 6 8 7 1head

current

size=5

2

8

7

1

head

current

size=5

6

View of CIRCULAR QUEUE

36

2 6 8 7 1start size=5

Traversing a CIRCULAR QUEUE

Display(Start)

1. If Start = NULL

Print “List is empty!!”

End If

2. Set TEMP = Start

3. Do

Print TEMP -> DATA

Set TEMP = TEMP -> next

While TEMP != Start

4. Exit.

37

2 6 8 7 1

start

Insertion in the Begining

Insert_beg(Start)

1. If Start = NULL

Set Start=nptr

Set Start -> next = Start

Else

Set TEMP = Start

While TEMP ->next !=Start

Set TEMP = TEMP -> next

End While

Set nptr -> next = Start

Set Start = nptr

Set TEMP -> next = Start

End If

2. Exit.

nptr

4

nptr

4start

(a)

(b)

38

2 6 8 7 1

start

Insertion at the End

Insert_beg(Start)

1. If Start = NULL

Set Start=nptr

Set Start -> next = Start

Else

Set TEMP = Start

While TEMP ->next !=Start

Set TEMP = TEMP -> next

End While

Set TEMP -> next = nptr

Set nptr -> next = Start

End If

2. Exit.

nptr

(b)

4

temp

39

2 6 8 7 1

start

Deletion from Beginingdelete_beg(Start)

1. If Start = NULL

Print “Underflow: List is empty! “

End If

2. Set TEMP = Start

3. Set ptr = TEMP

4. While ptr ->next !=Start

Set ptr = ptr -> next

End While

5. Set Start = Start -> next

6. Set ptr -> next = Start

7. Deallocate TEMP

8. Exit.(b)

ptrtemp

40

2 6 8 7 1

start

Deletion from the Enddelete_end(Start)

1. If Start = NULL

Print “Underflow: List is empty! “

End If

2. Set TEMP = Start

3. If temp -> next = start

Set Start = NULL

Else

While TEMP ->next !=Start

Set save = TEMP

Set TEMP = TEMP -> next

End While

Set save -> next = Start

End If

4. Deallocate TEMP

5. Exit.(b)

tempsave

top related