• No results found

Lecture - 5 On Data Structures Linked list

N/A
N/A
Protected

Academic year: 2020

Share "Lecture - 5 On Data Structures Linked list"

Copied!
22
0
0

Loading.... (view fulltext now)

Full text

(1)

Lecture - 5

On

Data Structures

Linked list

(2)

Prepared by, Jesmin Akhter, 2

Lecture Outline

» Inserting into a sorted linked list

» Finding a Node

» Inserting into a sorted linked list

» Deletion from a Linked List

» Variations of Linked Lists:

» (a) Circular linked lists

» (b) Doubly linked lists

(3)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

3

Inserting into a sorted linked list

• ITEM must be inserted between nodes A and B so that • INFO(A)<ITEM<=INFO(B)

• Traverse the list using a pointer variable PTR

• Comparing the ITEM with INFO[PTR] at each node.

• Keep track the location of the preceding node by a pointer variable SAVE, as in fig 5-20 • SAVE and PTR are updated by the assignments

SAVE : =PTR and PTR : =LINK[PTR]

START

Node A Node B

Fig :5-20

(4)

Prepared by, Jesmin Akhter, 4

Finding a Node

FINDA(INFO, LINK, START, ITEM, LOC)

This procedure finds the location LOC of the last node in a sorted list such that INFO[LOC] < ITEM or sets LOC= NULL

1. [List Empty?] If START = NULL then: Set LOC = NULL and return.

2. [special case?] If ITEM < INFO[START], then: Set LOC = NULL and return.

3. Set SAVE=START and PTR = LINK[START] [initialize pointer]

4. Repeat step 5 and 6 while PTR ≠ NULL.

5. If ITEM < INFO[PTR] then: Set LOC= SAVE and Return.

6. Set SAVE = PTR and PTR= LINK[PTR] [update pointer] (End of step 4)

7. Set LOC= SAVE

(5)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

5

Algorithm 5.7: INSSRT(INFO, LINK, START, AVAIL, ITEM) This algorithm inserts an item into a sorted linked list.

1. [Use Procedure 5.6 to find the location of the node preceding ITEM] Call FINDA(INFO, LINK, START, ITEM, LOC).

2. [Use algorithm 5.5 to insert ITEM after the node with location LOC] Call INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM).

3. Exit

(6)

Prepared by, Jesmin Akhter, 6

Deletion from a Linked List

• The next pointer field of node-A now points to node-B, where node-N previously pointed

• The next pointer field of N now points to the original first node in the free pool, where AVAIL previously pointed.

• AVAIL now points to the deleted node-N START

Node A Node N

(a) Before deletion

Node B

Node A Node B After deletion

AVAIL

Free storage list START

(7)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

7

Deleting a node

DELETE(INFO, LINK, START, AVAIL, ITEM)

– Delete a node with the value equal to INFO from the list.

– If such a node is found, return its position. Otherwise, return NULL. Steps

– Find the desirable node (similar to FINDB)

– Release the memory occupied by the found node

– Set the pointer of the predecessor of the found node to the successor of the found node

• Like INSLOC, there are two special cases – Delete first node

(8)

Prepared by, Jesmin Akhter, 8

Finding a Node

FINDB(INFO, LINK, START, ITEM, LOC, LOCP)

This procedure finds the location LOC of the first node N which contains ITEM and the location LOCP of the node preceding node N.

• If ITEM does not appear in the list, then sets LOC=NULL • If ITEM appear in the first node, then sets LOCP=NULL 1. [List Empty?] If START = NULL then:

Set LOC = NULL and LOCP = NULL and return. 2. [ITEM in the first node?] If INFO[] = ITEM, then:

Set LOC = START and LOCP = NULL and return.

3. Set SAVE=START and PTR = LINK[START] [initialize pointer]

4. Repeat step 5 and 6 while PTR ≠ NULL.

5. If INFO[] = ITEM then:

Set LOC= PTR and LOCP = SAVE and Return.

6. Set SAVE = PTR and PTR= LINK[PTR] [update pointer]

7. Set LOC= NULL 8. Exit.

(9)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

9

Deleting a node

DELETE(INFO, LINK, START, AVAIL, ITEM)

1. Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP) 2. IF LOC = NULL then: ITEM not in the list and exit. 3. [Delete node]

IF LOCP = NULL then:

Set START= = LINK[START] [Delete first node] Else:

Set LINK[LOCP] = LINK [LOC] 4. Return deleted node to the AVAIL list

Set LINK[LOC] = AVAIL and AVAIL = LOC 5. Exit.

Try to find the node with

its value equal to N

(10)

Prepared by, Jesmin Akhter, 10

Deleting a node

LOC

START

DELETE(INFO, LINK, START, AVAIL, ITEM)

1. Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP) 2. IF LOC = NULL then: ITEM not in the list and exit. 3. [Delete node]

IF LOCP = NULL then:

Set START= = LINK[START] [Delete first node] Else:

Set LINK[LOCP] = LINK [LOC] 4. Return deleted node to the AVAIL list

Set LINK[LOC] = AVAIL and AVAIL = LOC 5. Exit.

(11)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU 11

Deleting a node

LOC LOCP

DELETE(INFO, LINK, START, AVAIL, ITEM)

1. Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP)

2. IF LOC = NULL then: ITEM not in the list and exit. 3. [Delete node]

IF LOCP = NULL then:

Set START= = LINK[START] [Delete first node]

Else:

Set LINK[LOCP] = LINK [LOC]

4. Return deleted node to the AVAIL list

Set LINK[LOC] = AVAIL and AVAIL = LOC

(12)

Prepared by, Jesmin Akhter, 12

Circular linked lists

– The last node points to the first node of the list

– How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.)

(13)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

13

Variations of Linked Lists

circular linked list:

• In a circular linked list there are two methods to know if a node is the first node or not.

Either a external pointer, list, points the first node or

A header node is placed as the first node of the circular list.

• The header node can be separated from the others by either heaving a

sentinel value as the info part or

(14)

Prepared by, Jesmin Akhter, 14

CIRCULAR LIST with header node

•The header node in a circular list can be specified by a sentinel value or a dedicated flag:

•Header Node with Sentinel: Assume that info part contains positive integers.

(15)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

15

CIRCULAR LIST with header node

Header Node with Flag: In this case a extra variable called flag can be used to

represent the header node.

● For example flag in the header node can be 1, where the flag is 0 for the other

(16)

Prepared by, Jesmin Akhter, 16

Example of application of

circular linked list

• A good example of an application where circular linked list should be used is a timesharing problem solved by the operating system.

• In a timesharing environment, the operating system must maintain a list of present users and must alternately allow each user to use a small slice of CPU time, one user at a time.

• The operating system will pick a user, let him/her use a small amount of CPU time and then move on to the next user, etc.

• For this application, there should be no NIL pointers unless there is absolutely no one requesting CPU time.

• - polygon clipping

• - round robin situations

(17)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

17

Advantages

• Each node is accessible from any node. • Address of the first node is not needed.

• Certain operations, such as concatenation and splitting of string, is more efficient with circular linked list.

Disadvantage:

(18)

Prepared by, Jesmin Akhter, 18

In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list must be preserved in order to be able to reference the list again.

Circular linked lists can be used to help the traverse the same list again and

again if needed. A circular list is very similar to the linear list where in the circular list the pointer of the last node points not NULL but the first node.

(19)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

19

Variations of Linked Lists

Doubly linked lists

A linked list in which each node has three parts :one information and 2 pointers: An information field which contains the data of node.

a forward pointer (a pointer to the next node in the list) and

a backward pointer (a pointer to the node preceding the current node in the list) is called a doubly linked list. Here is a picture:

A

Head

B

C

The primary disadvantage of doubly linked lists are that

(1) Each node requires an extra pointer, requiring more space, and

(2) The insertion or deletion of a node takes a bit longer (more pointer operations).

–Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards.

Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list

(20)

Prepared by, Jesmin Akhter, 20

Example

:Doubly linked lists

• A doubly linked list of lines in a document that may be kept by a text editor. The following denotes how each node should appear:

•To move backward and forward through the document (as it appears on the screen ) and insert or delete lines, a doubly linked list is ideal.

• With the cursor on the current line (stored in a pointer "current"), •it is easy to move up one line (current = current->prev) or

• down one line (current = current->next).

(21)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU

21

Array versus Linked Lists

– Linked lists are more complex to code and manage than arrays, but they have some distinct advantages.

– Dynamic: a linked list can easily grow and shrink in size.

• We don’t need to know how many nodes will be in the list. They are created in memory as needed.

• In contrast, the size of a C++ array is fixed at compilation time. – Easy and fast insertions and deletions

• To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements.

• With a linked list, no need to move other nodes. Only need to reset some pointers.

(22)

Prepared by, Jesmin Akhter, 22

• Solved problems

5.1, 5.2, 5.3, 5.4,5.5, 5.10, 5.11

• Supplementary problems

References

Related documents

A doubly linear linked list contains two pointers and a data part where one of the pointer points to the preceding node and the other pointer will point to the subsequent node..

– Nodes can be inserted and deleted anywhere in linked list – Last node is set to null to mark end of

Including tokyo city as osaka guide looks great side trip to find the local and from.. Clearly demonstrates the time and transfer to set up

Finally, this theory of other hemisphere recruitment also sheds light on the frequent neuroimaging finding after TBI showing greater involvement of the right hemisphere

Before multispecies periodontal biofilms could be quantified (± treatment) by qPCR methodologies, DNA from known concentrations of each bacterial species were used to

The last node points to NULL Declaring a Linked list In C language a linked list would be implemented using structure and pointers struct LinkedList int data.. Creating nodes

You may wonder why another cursor to step through the data structure, but when you traverse a linked list, maybe a cursor in use is at an important position before traversal and

Specialization: Digital design(System design,RTL and Verification), Verilog Main business: Design, Research, Offshore projects. Description: ARF-Design works in two domains of