































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































































C H A P TER TH R EE: LIN KED LISTS DATA STRUCTURES AND ALGORITHM BY: KIBRU G.
▪ Introduction to Linked List ▪ Types of Linked List ▪ Singly Linked Lists ▪ Doubly Linked Lists ▪ Circular Linked Lists ▪ Applications of Linked Lists ▪ Common Problems and Algorithms on Linked Lists ▪ Advanced Topics (Reading Assignment) ▪ Skip Lists. ▪ Self-organizing Linked Lists. ▪ XOR Linked Lists (Memory-efficient Doubly Linked List). ▪ Unrolled Linked Lists.
▪Size must be known at compile-time.
operations in the middle too costly because you are required to shift
▪ Dynamic Data Structures: Data structures that can grow, shrink , or change their form during program execution. ▪ Example: Linked lists, stacks, queues, trees, graphs. ▪Size can change dynamically as needed. ▪Memory is allocated non-contiguously (nodes can be scattered in memory). ▪Efficient insertion and deletion (O( 1 ) in many cases , e.g., at the head/tail of a linked list). ▪ No need to pre-allocate memory ; uses memory only as needed. ▪ Flexible and adaptable to complex problems. ▪Slightly higher memory overhead due to storing pointers/references. ▪No random access; sequential access only (e.g., O(n) for accessing an element in a linked list).
chain and holding the entire thing together.
▪This linked list has four nodes in it, each with a link to the next node in the series. ▪The last node has a link to the special value NULL , which any pointer (whatever its type) can point to, to show that it is the last link in the chain. ▪There is also another special pointer, called Start , which points to the first link in the chain so that we can keep track of it.
▪ Singly Linear Linked List ▪Consists only one link points to next node ▪ Singly Circular Linked List ▪One link is point to next element and this list is circular because the last node point to first node ▪ Doubly Linear Linked List ▪Consists two pointers one point to next node and the other point to previous node ▪ Doubly Circular Linked List ▪Previous pointer of the first node and the next pointer of the last node point to the head node
1. Structure of a Linked List ▪ Data Element : This is the actual value or data stored in each node of the linked list. It can be of any data type (e.g., integer, string, object). ▪ Link Field (Pointer): This is a reference (or pointer) to the next node in the list. In a singly linked list, each node points to the next node. In a doubly linked list, each node points to both the next and the previous node. 2. The Way to Make Links Between Elements ▪ First Element (Head): The first node in the list. It is the entry point to the linked list. ▪ Last Element (Tail): The last node in the list. Its next pointer is None (or null in some languages) to indicate the end of the list. ▪ Middle Elements: Nodes between the head and tail. Each node points to the next node in the sequence.
3. How Many Nodes to Take All List Elements, How to Traverse the List ▪The number of nodes in the list is equal to the number of elements in the list. ▪To traverse the list, start at the head and follow the next pointers until you reach a node where next is None. 4. Basic operations:
1. Insertion: ▪ At the Beginning : Update the new node’s next to point to the current head, then update the head to the new node. ▪ At the End: Traverse to the last node, then update its next to point to the new node. ▪ In the Middle: Traverse to the desired position, update the new node’s next to point to the next node, and update the previous node’s next to point to the new node.
▪ In C++, linked list can be defined using structures and pointers. ▪ Each structure represents a node having some data and also a pointer that holds the address of the next node and creates the link between two nodes. ▪ Nodes can be located anywhere in memory , as they are connected via links. ▪ The simplest form of linked list is the singly linked list. ▪ In a singly linked list, each node contains some data and a single link to its successor in the list. ▪ Note that only one variable p can be used to access any node in the list. ▪ The last node in the list is indicated by having a null pointer (backslash ‘\’ symbol to denotes the null pointer ).
*Types of Linked Lists: Singly Linked Lists: Syntax struct nodeType { int info; // stores the actual value nodeType link; // Pointer to next node }; nodeType head; //head will permanently point to the start of the list
▪Consider the linked list with 4 nodes:
to store the address of the next node. ▪The first node is at location 2000 , the second node is at location 2800 , the third node is at location 1500 , and the fourth node is at location 3600. Types of Linked Lists: Singly Linked Lists: Some Properties
▪Suppose that current is a pointer of the same type as the pointer head. Then the statement ▪ current = head; copies the value of head into current. ▪Now consider the following statement: ▪ current = current->link; This statement copies the value of current->link, which is 2800 , into current. ▪Therefore, after this statement executes, current points to the second node in the list. Types of Linked Lists: Singly Linked Lists: Some Properties