1 / 19

Data Structure and Algorithm: CIT231

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.

Download Presentation

Data Structure and Algorithm: CIT231

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists

  2. More united and connected we are, more is the flexibility and scalability

  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.

  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:

  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

  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.

  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.

  8. Linked Lists…Diagram Linked list; N stands for Null

  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.

  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

  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.

  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.

  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.

  14. Singly Linked List • The singly linked list can be represented linearly as shown below:

  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.

  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 3rd node in the list

  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.

  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.

  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

More Related