









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive overview of implementing various types of linked lists in c++, including singly linked lists, circular singly linked lists, doubly linked lists, and circular doubly linked lists. It covers the basic operations such as insertion, deletion, and traversal, as well as more advanced concepts like binary trees, binary search trees, avl trees, and weight-balanced binary search trees. The document also includes sample c++ code to demonstrate the implementation of these data structures. This resource would be valuable for students and developers looking to understand and implement linked list data structures in their c++ programs.
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!
C++ Program to Implement Singly Linked List The linked list stores data in sequential storage, like arrays. Though the data are stored sequentially, the memory locations are not contiguous. Unlike an array, the linked list can store data of different data types. The below diagram represents the linked-list structure. #include
ptr = ptr->next; } } int main() { insert( 3 ); insert( 1 ); insert( 7 ); insert( 2 ); insert( 9 ); cout<<"The linked list is: "; display(); return 0 ; } Output The linked list is: 9 2 7 1 3 In the above program, the structure Node forms the linked list node. It contains the data and a pointer to the next linked list node. This is given as follows. struct Node { int data; struct Node next; }; The function insert () inserts the data into the beginning of the linked list. It creates a new_node and inserts the number in the data field of the new_node. Then the new_node points to the head. Finally the head is the new_node i.e. the linked list starts from there. This is given below. void insert(int new_data) { struct Node new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = head;
๏ท Circular Doubly linked list: Circular Doubly Linked List has properties of both doubly linked list and circular linked list in which two consecutive elements are linked or connected by the previous and next pointer and the last node points to the first node by the next pointer and also the first node points to the last node by the previous pointer. #include
if (head!= NULL) { while (ptr->next != head) ptr = ptr->next; ptr->next = newnode; } else newnode->next = newnode; head = newnode; } void display() { struct Node* ptr; ptr = head; do { cout<<ptr->data <<" "; ptr = ptr->next; } while(ptr != head); } int main() { insert( 3 ); insert( 1 ); insert( 7 ); insert( 2 ); insert( 9 ); cout<<"The circular linked list is: "; display(); return 0 ; } Binary Tree A binary tree is a tree data structure in which each node can have at most two children, which are referred to as the left child and the right child.
๏ท Depth-First Search (DFS) Algorithms ๏ท Breadth-First Search (BFS) Algorithms Tree Traversal using Depth-First Search (DFS) algorithm can be further classified into three categories: ๏ท Preorder Traversal (current-left-right): Visit the current node before visiting any nodes inside the left or right subtrees. Here, the traversal is root โ left child โ right child. It means that the root node is traversed first then its left child and finally the right child. ๏ท In order Traversal (left-current-right): Visit the current node after visiting all nodes inside the left subtree but before visiting any node within the right subtree. Here, the traversal is left child โ root โ right child. It means that the left child is traversed first then its root node and finally the right child. ๏ท Post order Traversal (left-right-current): Visit the current node after visiting all the nodes of the left and right subtrees. Here, the traversal is left child โ right child โ root. It means that the left child has traversed first then the right child and finally its root node. Tree Traversal using Breadth-First Search (BFS) algorithm can be further classified into one category: ๏ท Level Order Traversal: Visit nodes level-by-level and left-to-right fashion at the same level. Here, the traversal is level-wise. It means that the most left child has traversed first and then the other children of the same level from left to right have traversed. Let us traverse the following tree with all four traversal methods:
Pre-order Traversal of the above tree: 1-2-4-5-3-6- In-order Traversal of the above tree: 4-2-5-1-6-3- Post-order Traversal of the above tree: 4-5-2-6-7-3- Level-order Traversal of the above tree: 1-2-3-4-5-6- Threaded Binary Tree A threaded binary tree is a type of binary tree data structure where the empty left and right child pointers in a binary tree are replaced with threads that link nodes directly to their in-order predecessor or successor, thereby providing a way to traverse the tree without using recursion or a stack. Threaded binary trees can be useful when space is a concern, as they can eliminate the need for a stack during traversal. However, they can be more complex to implement than standard binary trees. There are two types of threaded binary trees. Single Threaded: Where a NULL right pointers is made to point to the inorder successor (if successor exists) Double Threaded: Where both left and right NULL pointers are made to point to inorder predecessor and inorder successor respectively. The predecessor threads
In the above figure, we can observe that the root node is 40, and all the nodes of the left subtree are smaller than the root node, and all the nodes of the right subtree are greater than the root node. Similarly, we can see the left child of root node is greater than its left child and smaller than its right child. So, it also satisfies the property of binary search tree. Therefore, we can say that the tree in the above image is a binary search tree. Suppose if we change the value of node 35 to 55 in the above tree, check whether the tree will be binary search tree or not. In the above tree, the value of root node is 40, which is greater than its left child 30 but smaller than right child of 30, i.e., 55. So, the above tree does not satisfy the property of Binary search tree. Therefore, the above tree is not a binary search tree. AVL Tree Data Structure The name AVL tree is coined after its inventor's names โ Adelson-Velsky and Landis. In AVL trees, the difference between the heights of left and right sub trees, known as the Balance Factor, must be at most one. An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right sub trees for any node cannot be more than one. The difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of the node.
The above tree is AVL because the differences between the heights of left and right sub trees for every node are less than or equal to 1. Applications of AVL Tree:
Weight-Balanced Tree โ The weight of the right and left subtree in each node differ by at most one. Every complete binary tree is height-balanced. Weight Balanced Tree: In a weight balanced binary tree, the aim is to balance the weight of a given tree in terms of number of leaves. The weight of a node depends on the weight of its children. A Binary Search Tree is weight balanced if for each node the number of nodes in the left subtree is at least half and at most twice the number of nodes in the right subtree. A node in a weight-balanced binary search tree stores the sizes of subtrees in the nodes. A node will contain the following data: ๏ท value of the element stored ๏ท left and right pointer to children node or null ๏ท weight of the node The size of a leaf is usually considered as zero. The size of an internal node is the sum of sizes of its two children, plus one: (size[n] = size[n.left] + size[n.right] + 1). Based on the size, one defines the weight to be weight[n] = size[n] + 1.
The above figure shows the weight of nodes along with its value. The node with the greatest weight is the root of the tree. The leaf nodes have size = 0 and weight = 1. The weight of the nodes decreases from root to leaf nodes. C++ Program to Implement Doubly Linked List Doubly linked list is a type of data structure that is made up of nodes that are created using self-referential structures. Each of these nodes contains three parts, namely the data and the reference to the next list node and the reference to the previous list node. Only the reference to the first list node is required to access the whole linked list. This is known as the head. The last node in the list points to nothing so it stores NULL in that part. Also the doubly linked list can be traversed in both directions as each node points to its previous and next node. A program to implement doubly linked list is given as follows #include
return 0 ; } Output The doubly linked list is: 9 2 7 1 3 In the above program, the structure Node forms the doubly linked list node. It contains the data and a pointer to the next and previous linked list node. This is given as follows. struct Node { int data; struct Node prev; struct Node next; }; The function insert() inserts the data into the beginning of the doubly linked list. It creates a newnode and inserts the number in the data field of the newnode. Then the prev pointer in newnode points to NULL as it is entered at the beginning and the next pointer points to the head. If the head is not NULL then prev pointer of head points to newnode. Finally the head is the newnode i.e. the linked list starts from there. This is given below. void insert(int newdata) { struct Node newnode = (struct Node) malloc(sizeof(struct Node)); newnode->data = newdata; newnode->prev = NULL; newnode->next = head; if(head != NULL) head->prev = newnode ; head = newnode; } The function display() displays the whole doubly linked list. First ptr points to head. Then it is continuously forwarded to the next node until all the data values of the nodes are printed. This is given below.
void display() { struct Node* ptr; ptr = head; while(ptr != NULL) { cout<< ptr->data <<" "; ptr = ptr->next; } } In the function main(), first various values are inserted into the doubly linked list by calling insert(). Then the doubly linked list is displayed. This is given below. int main() { insert( 3 ); insert( 1 ); insert( 7 ); insert( 2 ); insert( 9 ); cout<<"The doubly linked list is: "; display(); return 0 ; } C++ Program to Implement Circular Doubly Linked List In data structure Link List is a linear collection of data elements. Each element or node of a list comprising of two items - the data and a reference to the next node. The last node has a reference to null. In a linked list the entry point is called the head of the list. In Circular Doubly Linked List two consecutive elements are linked or connected by previous and next pointer and the last node points to first node by next pointer and the first node also points to last node by previous pointer.