PGT 3033 DATA STRUCTURES NUROL HUSNA CHE ROSE

  • Slides: 52
Download presentation
PGT 303/3 DATA STRUCTURES NUROL HUSNA CHE ROSE ELECTRONIC ENGINEERING TECHNOLOGY DEPARTMENT FACULTY OF

PGT 303/3 DATA STRUCTURES NUROL HUSNA CHE ROSE ELECTRONIC ENGINEERING TECHNOLOGY DEPARTMENT FACULTY OF ENGINEERING TECHNOLOGY Email: husnarose@unimap. edu. my Phone : 013 505 9294 ( Available from Monday to Friday from 9 a. m till 5. 30 p. m ) Kindly, no call or whats app after working hours ya . Thank you

LINK LISTS (Chapter 6)

LINK LISTS (Chapter 6)

OUTLINE §Introduction §Singly Linked Lists §Circularly Linked Lists §Doubly Linked Lists §Multiply Linked Lists

OUTLINE §Introduction §Singly Linked Lists §Circularly Linked Lists §Doubly Linked Lists §Multiply Linked Lists §Applications §ADT for links §ADT for singly linked lists

INTRODUCTION-WHY NEED LINKED LIST Sequential data structures in general, suffer from the following drawbacks:

INTRODUCTION-WHY NEED LINKED LIST Sequential data structures in general, suffer from the following drawbacks: Inefficient implementation of insertion and deletion operations Inefficient use of storage memory

INTRODUCTION A linked representation serves to counteract the drawbacks of sequential representation by exhibiting

INTRODUCTION A linked representation serves to counteract the drawbacks of sequential representation by exhibiting the following merits: Efficient implementation of insertion and deletion operations. Unlike sequential data structures, there is complete

INTRODUCTION A linked representation of data structure known as a linked list is a

INTRODUCTION A linked representation of data structure known as a linked list is a collection of nodes. • Each node is a collection of fields categorized as data items and links. DATA ITEM LINK • The data item fields hold the information content or data to be represented by the node. • The link fields hold the addresses of the neighbouring nodes or of those nodes which are associated with the given node as dictated by the application.

Implementation of linked lists Frame chunks of memory into nodes with the desired number

Implementation of linked lists Frame chunks of memory into nodes with the desired number of data items and fields. We use: structure is use for the node and pointer is use for the link. A structure is the combination of bits of data from different sources and types. Unlike arrays, structures can combine data of different types and can be referred to using the same name.

 Let us assume that we want to create a data node which consist

Let us assume that we want to create a data node which consist information of student name and age. Name; Age; Link – the link field is a pointer to another node DATA ITEM NAME AGE LINK NULL

Implementation of linked lists (CONT. . ) Data represent of a node

Implementation of linked lists (CONT. . ) Data represent of a node

Implementation of linked lists (CONT. . ) To define a structure Node struct Node

Implementation of linked lists (CONT. . ) To define a structure Node struct Node { char name[30]; int age; struct Node * link; }; typedef NODE struct Node NODE; new. Node

Implementation of linked lists (CONT. . ) Pointer is a variable that contains an

Implementation of linked lists (CONT. . ) Pointer is a variable that contains an address value. This value refers to the memory location for a variable. The address of a variable is called a pointer to the variable and a variable that holds an address is called a pointer. POINTER

 Let say we declare a pointer to struct stud. Node; NODE *p; //

Let say we declare a pointer to struct stud. Node; NODE *p; // declaring a pointer of NODE. P = new. Node //assign pointer p to point to node

Implementation of linked lists (CONT. . ) We need to determine which nodes are

Implementation of linked lists (CONT. . ) We need to determine which nodes are free and which have been allotted for use to obtain nodes from the free storage area or storage pool for use, use GETNODE(X) to return or dispose nodes from the reserved area or pool to the free area after use, use RETURN(X)

Singly Linked Lists A singly linked list is a linear data structure each node

Singly Linked Lists A singly linked list is a linear data structure each node of which has one or more data item fields (DATA) but only a single link field (LINK). The link will be pointing to the next data node and so on until we have a list which consists of nodes of data linked to each other. The last node shall point to the null. Head DATA LINK

OPERATIONS ON Linked List Creating Insert a simple linked list a node Delete a

OPERATIONS ON Linked List Creating Insert a simple linked list a node Delete a node Reading through the list

CREATING A Linked List To create a linked list, we create a pointer of

CREATING A Linked List To create a linked list, we create a pointer of a node and set it as NULL Procedure create_list (Start) ( Start = NULL )

Insertion Procedure

Insertion Procedure

Insertion (cont…) Insert in an empty list Insert a new node to the right

Insertion (cont…) Insert in an empty list Insert a new node to the right of Node Insert a new node to end of the list

deletion Procedure

deletion Procedure

Deletion (cont…) Delete a node to the right CNode

Deletion (cont…) Delete a node to the right CNode

Reading through the list We read through the list to display or print the

Reading through the list We read through the list to display or print the list, to search particular item in the list or to sort the list. Procedure

Reading through the list (cont…)

Reading through the list (cont…)

Operations on a link list When we want to create an ascending or descending

Operations on a link list When we want to create an ascending or descending list we need to sort the list before inserting a new node. There are several occasion of insertion process. Insertion: can insert as long as there are memory available Insert at the first / front of the list Insert at the middle of the list

Operations on a link list (cont…) Deletion : cannot delete when the list is

Operations on a link list (cont…) Deletion : cannot delete when the list is empty Delete first node Delete the middle node Delete the last node

Insert into a sorting list

Insert into a sorting list

Insert at the front of the list To insert new node in front of

Insert at the front of the list To insert new node in front of the linked list Get node first GETNODE(NNode) DATA(NNode) LINK(NNode) Insert = NULL into empty list List Insert = Item = NNode infront of the list LINK(NNode) = List;

Insert at the front of the list (cont…)

Insert at the front of the list (cont…)

Insert in the middle of the list To insert a node in the middle

Insert in the middle of the list To insert a node in the middle of the list Get node first GETNODE(NNode); DATA(NNode) LINK(NNode) = “F” = Null Insert the new node after before the Cnode or after PNode LINK(NNode) = LINK(PNode); LINK(PNode) = NNode;

Insert in the middle of the list (cont…)

Insert in the middle of the list (cont…)

Insert at the end of the list Insert node at the end of the

Insert at the end of the list Insert node at the end of the list Get the new node GETNODE(NNode) DATA(NNode) LINK(NNode) Insert = I; = NULL; new node to the end of the list LINK(CNode) = NNode;

Insert at the end of the list (cont…)

Insert at the end of the list (cont…)

Delete from a list Deleting a specific node. Before that we need to search

Delete from a list Deleting a specific node. Before that we need to search the node

Operation Deletion from the linked list Delete first node Temp List = LINK(List) LINK(Temp)

Operation Deletion from the linked list Delete first node Temp List = LINK(List) LINK(Temp) = Null REMOVE(Temp)

Operation Deletion from the linked list Delete the middle node in the list Temp

Operation Deletion from the linked list Delete the middle node in the list Temp = Cnode LINK(PNode) LINK(Temp) = LINK(CNode) = NULL REMOVE(Temp)

Insertion in a singly linked list Algorithm 6. 1 : To insert a data

Insertion in a singly linked list Algorithm 6. 1 : To insert a data element ITEM in a non empty singly li. Nked list START, to the right of node NODE. Procedure INSERT_SL(START, ITEM, NODE) /* Insert ITEM to the right of node NODE in the list START */ Call GETNODE(X); DATA(X) = ITEM; LINK(X) = LINK(NODE); Node X points to the original right neighbour of node NODE */ LINK(NODE) = X; end INSERT_SL.

DELETION IN A SINGLY LINKED LIST Algorithm 6. 3 : Deletion of a node

DELETION IN A SINGLY LINKED LIST Algorithm 6. 3 : Deletion of a node to the right of linked list START Procedure which is node NODEX in a singly DELETE_SL(START, NODEX) if (START = NIL) then Call ABANDON_DELETE; /*ABANDON_DELETE terminates the delete operation */ else {TEMP = LINK(NODEX); LINK(NODEX) = LINK(TEMP); Call RETURN(TEMP); } end DELETE_SL.

Circularly Linked Lists For further improvement in processing of a singly linked list one

Circularly Linked Lists For further improvement in processing of a singly linked list one may replace the null pointer in the last node with the address of the first node in the list. Such a list is called as a circularly linked list or a circular linked list or simply a circular list. Head

Advantage of Circularly Linked Lists Advantages of circular linked list accessibility of a node

Advantage of Circularly Linked Lists Advantages of circular linked list accessibility of a node every node is accessible in one way movement delete operations the forward movement does not allow to access the predecessor of the node to be deleted, with circular linked list, the predecessor is able to access relative efficiency in the implementation of list based operations

Disadvantages of Circularly Linked Lists Disadvantage of circular link list getting into an infinite

Disadvantages of Circularly Linked Lists Disadvantage of circular link list getting into an infinite loop owing to the circular nature of pointers in the list Solution solution to this problem is to designate a special node to act as the head of the list, known as list head or head node.

Circularly Linked Lists headed circularly linked list or circularly linked list with head node.

Circularly Linked Lists headed circularly linked list or circularly linked list with head node.

Basic operation on Circular Linked Lists Insert a node to the leftmost of the

Basic operation on Circular Linked Lists Insert a node to the leftmost of the link. Let say we have circular linked list P. LINK(X) = LINK(P) =X

Delete a node on a circular linked list Delete the leftmost from a circular

Delete a node on a circular linked list Delete the leftmost from a circular list P Temp = LINK(P) = LINK(Temp)=NULL REMOVE(Temp)

Doubly Linked Lists To enhance greater flexibility of movement, the linked representation could include

Doubly Linked Lists To enhance greater flexibility of movement, the linked representation could include two links in every node, each of which points to the nodes on either side of the given node. Such a linked representation known as doubly linked list Each node has one or more data fields but only two link fields termed left link (LLINK) and right link (RLINK). The LLINK field of a given node points to the node on its left and its RLINK field points to the on its right.

Doubly Linked Lists DATA LLINK Advantages RLINK of a doubly linked list The availability

Doubly Linked Lists DATA LLINK Advantages RLINK of a doubly linked list The availability of two links LLINK and RLINK permit forward and backward movement during the processing of the list. The deletion of a node X from the list calls only for the value X to be known. Disadvantages memory of a doubly linked list requirement

Insert to a Doubly Linked Lists To insert a node LLINK(X) =Y RLINK(X)=RLINK(Y) LLINK(RLINK(Y))

Insert to a Doubly Linked Lists To insert a node LLINK(X) =Y RLINK(X)=RLINK(Y) LLINK(RLINK(Y)) RLINK(Y) =X =X

Delete from a doubly linked list To delete Node Y RLINK(LLINK(Y) = RLINK(Y) LLINK(RLINK(Y)

Delete from a doubly linked list To delete Node Y RLINK(LLINK(Y) = RLINK(Y) LLINK(RLINK(Y) = LLINK(Y)=NULL RLINK(Y) = NULL REMOVE(Y)

Insertion in a doubly linked list Algorithm 6. 4 : To insert node X

Insertion in a doubly linked list Algorithm 6. 4 : To insert node X to the right of node Y in a headed circular doubly linked list P Procedure INSERT_DL(X, Y) LLINK(X) = Y; RLINK(X) = RLINK(Y); LLINK(RLINK(Y)) = X; RLINK(Y) = X; end INSERT_DL.

Deletion in a doubly linked list Algorithm 6. 5 : Delete node X from

Deletion in a doubly linked list Algorithm 6. 5 : Delete node X from a headed circular doubly linked list P procedure DELETE_DL(P, X) if (X = P) then ABANDON_DELETE; else {RLINK(LLINK(X)) = RLINK(X); LLINK(RLINK(X)) = LLINK(X); Call RETURN(X); } end DELETE_DL.

Application of a link list Addition of polynomials Representation of a sparse matrix

Application of a link list Addition of polynomials Representation of a sparse matrix

ADT for Links Data objects: Addresses of the nodes holding data and null links

ADT for Links Data objects: Addresses of the nodes holding data and null links Operations: Allocate node (address X) accommodate data GETNODE Return from Available Space to ( X) node (address X) after use to Available Space RETURN(X) Store a value of one link variable LINK 1 to another link variable LINK 2 STORE_LINK ( LINK 1, LINK 2) Store ITEM into a node whose address is X

ADT for singly Linked lists Data objects: A list of nodes each holding one

ADT for singly Linked lists Data objects: A list of nodes each holding one (or more) data field(s) DATA and a single link field LINK. LIST points to the start node of the list. Operations: Check if list LIST is empty CHECK_LIST_EMPTY Insert ITEM into the list LIST as the first element INSERT_FIRST ( LIST) (Boolean function) (LIST, ITEM) Insert ITEM into the list LIST as the last element INSERT_LAST (LIST, ITEM)

ADT for singly Linked lists (cont…) Delete the first node from the list LIST

ADT for singly Linked lists (cont…) Delete the first node from the list LIST DELETE_FIRST(LIST) Delete the last node from the list LIST DELETE_LAST(LIST) Delete ITEM from the list LIST DELETE_ELEMENT Advance Link to traverse down the list ADVANCE_LINK Store (LINK) ITEM into a node whose address is X STORE_DATA(X, Retrieve ITEM (LIST, ITEM) data of a node whose address is X and return it in