200 likes | 376 Vues
Data Structure and Algorithm: CIT231. Lecture 6: Linked Lists. More united and connected we are, more is the flexibility and scalability. Linked Lists. Is the most used data structure in the scientific computing though it is hard to be understood.
E N D
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists
More united and connected we are, more is the flexibility and scalability
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.
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:
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
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.
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.
Linked Lists…Diagram Linked list; N stands for Null
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.
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
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.
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.
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.
Singly Linked List • The singly linked list can be represented linearly as shown below:
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.
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 3rd node in the list
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.
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.
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