Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.

Similar presentations


Presentation on theme: "Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1."— Presentation transcript:

1 Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1

2 More united and connected we are, more is the flexibility and scalability DeSiaMorewww.desiamore.com/ifm2

3 Linked Lists Is the most used data structure in the scientific computing though it is hard to be understood. They offer high flexibility and performance when used in programming. DeSiaMorewww.desiamore.com/ifm3

4 Linked Lists For storing similar data in memory we can use either an array or a linked list. Arrays are simple to understand and elements of arrays are easily accessible. But the following are cons of arrays: DeSiaMorewww.desiamore.com/ifm4

5 The cons of arrays Arrays have fixed dimension so once the size of an array is set it cannot be increased or decreased during execution. Array elements are always contiguous memory locations of which might be scarce for the big size array that we need to create Operations like insertion of new element in an array or deletion of an existing element of an array require the shifting of position left or right which make these operations tedious DeSiaMorewww.desiamore.com/ifm5

6 Alternative of arrays Linked list overcomes all these disadvantages by growing and shrinking in size during its lifetime. Nodes (elements) are stored at different memory locations so it happens that we fall short of memory when required. Unlike arrays shifting of nodes is not required. DeSiaMorewww.desiamore.com/ifm6

7 Linked Lists Linked list is a very common data structure often used to store similar data in memory. Linked list elements are not constrained to be stored in adjacent locations. The order of the elements is maintained by explicit links between them. Example the students marks can be stored in a linked list. DeSiaMorewww.desiamore.com/ifm7

8 Linked Lists…Diagram Linked list; N stands for Null DeSiaMorewww.desiamore.com/ifm8

9 Linked Lists…Definition A linked list is a collection of elements (records) called nodes or struct, each of which stores two items of information – an element of the list and link. A link is a pointer or an address that indicates explicitly the location of the node containing the successor of list element. In the figure of the above slide the arrows represent the links. DeSiaMorewww.desiamore.com/ifm9

10 Linked Lists The data part of each node consists of the marks obtained by a student and the link part is the pointer to the next node. The NULL in the last node indicates that this is the last node in the list DeSiaMorewww.desiamore.com/ifm10

11 Types of Linked Lists There are two types of linked list: Singly linked list Doubly Linked List Singly linked lists contains only one pointer to the next node as it was seen above. Doubly linked lists (Circular Linked List) have two pointers; one points to the previous node and other points to the next node. DeSiaMorewww.desiamore.com/ifm11

12 Linked Lists We could have record hold anything you want, however each record must be the instance of the same structure for this to work. That is we couldn’t have a node (record) point to another structure holding short, a char array, and a long. Also each structure can be located anywhere in memory, each node doesn’t have to be linear in memory. DeSiaMorewww.desiamore.com/ifm12

13 Linked Lists Linked list is an odd way of storing data, some people see it as useful way of storing data when developing the games software. DeSiaMorewww.desiamore.com/ifm13

14 Singly Linked List The singly linked list can be represented linearly as shown below: DeSiaMorewww.desiamore.com/ifm14

15 Linked List Operation In the singly linked list each node is dynamically allocated from the heap, so the node has a unique address. The most two operations that can be performed in the linked lists are insertion and deletion. DeSiaMorewww.desiamore.com/ifm15

16 Insertion/Deletion The node within the lists can be inserted or deleted. If a new node need to be allocated to the list and is to be inserted as the 3 rd node in the list DeSiaMorewww.desiamore.com/ifm16

17 Inserting New Node To insert new node in the list you must have a pointer to a node that will point the first node in the list. This node pointer will persistently point to the first node no matter amount of nodes get added in the list. When no node has been added in the list the pointer must be set to NULL to indicate that the list is empty. DeSiaMorewww.desiamore.com/ifm17

18 Deleting Node from the List There are few steps to deleting a specific element from the list: Find the node with the element (if it exists) Remove that node. Reconnect the linked list Finding the node is the matter of traversing the list and looking at each node’s element. DeSiaMorewww.desiamore.com/ifm18

19 Deletion Reconnecting the list once a node is to be removed involve 3 cases: Removing a node from the beginning Removing a node from the middle Removing a node from the end DeSiaMorewww.desiamore.com/ifm19


Download ppt "Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1."

Similar presentations


Ads by Google