* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Creating a linked List
Survey
Document related concepts
Transcript
Introduction to Linked Lists Linked lists and arrays are similar since they both store collections of data. They store linear data of similar type. Below is the comparison between arrays and linked lists. Based on Access: Random / Sequential Memory Structure Insertion / Deletion Memory Allocation Arrays Array elements can be randomly accessed using Subscript Variables. e.g a[0],a[1],a[3] can be randomly accessed Array is stored in contiguous Memory Locations, i.e. Suppose first integer element is Stored at 2000 then Second Integer element will be stored at 2002. Elements are stored in Consecutive memory Locations, so while inserting, we have to create space for Insertion which takes more time. Similarly while deleting, delete the element from given Location and then Shift All successive elements up by 1 position Memory is allocated at compile time. i.e it uses static memory allocation Linked Lists We have to Traverse Through the Linked List for Accessing Element. So O (n) Time required for Accessing Element. Generally in linked List Elements are accessed Sequentially Element is stored at any available Location, but the Pointer to that memory location is stored in Previous Node. we have to Just Change the Pointer address field (Pointer),So Insertion and Deletion Operations are quite easy to implement Memory is allocated at run time. It uses dynamic memory allocation. Linked List: A linked list is a linear data structure where each element is a separate object. Each element (node) of a list comprises of two items - the data and a reference to the next node. Types of Linked Lists 1. 2. 3. 4. Singly Linked Lists Doubly Linked Lists Circular Singly Linked Lists Circular Doubly Linked Lists Singly Linked Lists: 1. In this type of Linked List two successive nodes are linked together in linear fashion. 2. Each node contains address of the next node to be followed 3. Only forward sequential movement is possible. So accessing singly linked list is unidirectional 4. Elements are accessed sequentially. No direct access is allowed. 5. Head (start) is a pointer that stores the address of the first node. 6. The link field of the last node (Tail node) is NULL which indicates the end of the list. 7. Moving from one node to another by following the next reference is known as link hopping or pointer hopping Linked list can be defined as Struct NODE { Data_type info; Struct node *link; } head of structure definition SAME type of link Different Operations possible on Linked List 1. Creating a linked List To create a list of nodes, we first create a header HEAD to NULL. NULL HEAD The nodes in the linked list are created using self referential structures. i.e. member in the structure is same as the header of the structure definition. After creating the node, its address is assigned to HEAD. HEAD New Node 5001 40 NULL 8000 5001 CurrPtr 5001 5002 When the second Node is created, the first node’s Link is updated with the address of the second node. This process continues until the user stops creating nodes. HEAD 5001 8000 First NODE 40 5001 7050 5002 New NODE 50 7050 NULL 7051 7050 CurrPtr Other possible operations on linked Lists include 2. Deleting an element from the list 3. Searching an element from the list 4. Traversing a linked list Doubly Linked Lists: Doubly-linked list is a more sophisticated form of linked list where each node of the list contain two references (or links) – one to the previous node and other to the next node. The previous link of the first node and the next link of the last node points to NULL. In comparison to singly-linked list, doubly-linked list requires handling of more pointers but less information is required as one can use the previous links to observe the preceding element. The advantage of a doubly linked list is that we don’t need to keep track of the previous node for traversal or no need of traversing the whole list for finding the previous node. The disadvantage is that more pointers needs to be handled and more links need to updated. Applications of Linked Lists: 1. Lists are very useful for holding many elements where we need to add or remove elements during the course of the program. 2. Constructing Symbol Tables 3. Memory management in Operating systems. Real world applications include: A list of the lines in a file A list of the options in a menu (particularly if you can drag menu items around) A list in the GPS of the turns along your route