Lecture 6 on data structures
This presentation is the property of its rightful owner.
Sponsored Links
1 / 22

Lecture - 6 On Data Structures PowerPoint PPT Presentation


  • 52 Views
  • Uploaded on
  • Presentation posted in: General

Lecture - 6 On Data Structures. Linked list. 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 Array versus Linked Lists.

Download Presentation

Lecture - 6 On Data Structures

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lecture 6 on data structures

Lecture - 6 OnData Structures

Linked list


Lecture outline

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

  • Array versus Linked Lists


Inserting into a sorted linked list

START

Node B

Node A

PTR

SAVE

Fig :5-20

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]


Finding a node

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

  • [List Empty?] If START = NULL then:

    Set LOC = NULL and return.

  • [special case?] If ITEM < INFO[START], then:

    Set LOC = NULL and return.

  • Set SAVE=START and PTR = LINK[START] [initialize pointer]

  • Repeat step 5 and 6 while PTR ≠ NULL.

  • If ITEM < INFO[PTR] then:

    Set LOC= SAVE and Return.

  • Set SAVE = PTR and PTR= LINK[PTR] [update pointer]

    (End of step 4)

  • Set LOC= SAVE

  • Exit.


Inserting into a sorted linked list1

Inserting into a sorted linked list

Algorithm 5.7: INSSRT(INFO, LINK, START, AVAIL, ITEM)

This algorithm inserts an item into a sorted linked list.

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

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

  • Exit


Deletion from a linked list

START

Node N

Node A

Node B

(a) Before deletion

START

After deletion

Node A

Node N

Node B

AVAIL

Free storage list

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


Deleting a node

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

    • Delete the node in middle or at the end of the list


Finding a node1

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

  • [List Empty?] If START = NULL then:

    Set LOC = NULL and LOCP = NULL and return.

  • [ITEM in the first node?] If INFO[] = ITEM, then:

    Set LOC = START and LOCP = NULL and return.

  • Set SAVE=START and PTR = LINK[START] [initialize pointer]

  • Repeat step 5 and 6 while PTR ≠ NULL.

  • If INFO[] = ITEM then:

    Set LOC= PTR and LOCP = SAVE and Return.

  • Set SAVE = PTR and PTR= LINK[PTR] [update pointer]

  • Set LOC= NULL

  • Exit.


Deleting a node1

Deleting a node

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

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

  • IF LOC = NULL then: ITEM not in the list and exit.

  • [Delete node]

  • IF LOCP = NULL then:

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

  • Else:

  • Set LINK[LOCP] = LINK [LOC]

  • Return deleted node to the AVAIL list

  • Set LINK[LOC] = AVAIL and AVAIL = LOC

  • Exit.

Try to find the node with its value equal to N


Deleting a node2

START

LOC

Deleting a node

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

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

  • IF LOC = NULL then: ITEM not in the list and exit.

  • [Delete node]

  • IF LOCP = NULL then:

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

  • Else:

  • Set LINK[LOCP] = LINK [LOC]

  • Return deleted node to the AVAIL list

  • Set LINK[LOC] = AVAIL and AVAIL = LOC

  • Exit.


Deleting a node3

LOCP

LOC

Deleting a node

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

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

  • IF LOC = NULL then: ITEM not in the list and exit.

  • [Delete node]

  • IF LOCP = NULL then:

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

  • Else:

  • Set LINK[LOCP] = LINK [LOC]

  • Return deleted node to the AVAIL list

  • Set LINK[LOC] = AVAIL and AVAIL = LOC

  • Exit.


Lecture 6 on data structures

Variations of Linked Lists

  • 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.)


Variations of linked lists

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 theinfo part or

  • having a dedicated flag variable to specify if the node is a header node or not.


Circular list with header node

CIRCULAR LIST with header node

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

  • Header Node with Sentinel: Assume that info part contains positive integers. Therefore the infopart of a header node can be -1.


Circular list with header node1

CIRCULAR LIST with header node

  • Header Node with Flag: In this case a extra variable called flag can be used to represent theheader node.

  • For example flag in the header node can be 1, where the flag is 0 for the othernodes.


Example of application of circular linked list

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

  • - when you need to rotate the list, rather than the reference.


Advantages

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:

  • Danger of an infinite loop !


Linear linked list vs circular linked lists

Linear linked list vs Circular Linked Lists

  • In linear linked lists if a list is traversed (all the elements visited) an external pointer to the listmust 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. Acircular list is very similar to the linear list where in the circular list the pointer of the last nodepoints not NULL but the first node.


Variations of linked lists1

A

C

B

Head

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:

  • Each node points to not only successor but the predecessor

  • There are two NULL: at the first and last nodes in the list

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

  • The primary disadvantage of doubly linked lists are that

  • Each node requires an extra pointer, requiring more space, and

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


Example doubly linked lists

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). 

  • With a singly linked list this is possible but probably too slow.


Array versus linked lists

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.


Lecture 6 on data structures

  • Solved problems

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

  • Supplementary problems

    5.14, 5.15, 5.16, 5.17, 5.18, 5.19, 5.20


  • Login