






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
b tree,red blACK TRESS ,SCAPE GOAT TREES, HEAP
Typology: Lecture notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







struct Node { int data; Node* next; } struct Node { void *data; struct Node *next; }; The void pointer can store the address of any data type
Doubly Linked List Way 1: Ordinary Representation Node A: prev = NULL, next = add(B) // previous is NULL and next is address of B Node B: prev = add(A), next = add(C) // previous is address of A and next is address of C Node C: prev = add(B), next = add(D) // previous is address of B and next is address of D Node D: prev = add(C), next = NULL // previous is address of C and next is NULL Illustration: Node A: npx = 0 XOR add(B) // bitwise XOR of zero and address of B Node B: npx = add(A) XOR add(C) // bitwise XOR of address of A and address of C Node C: npx = add(B) XOR add(D) // bitwise XOR of address of B and address of D Node D: npx = add(C) XOR 0 // bitwise XOR of address of C and 0 npx(C) XOR add(B) => (add(B) XOR add(D)) XOR add(B) // npx(C) = add(B) XOR add(D) => add(B) XOR add(D) XOR add(B) // a^b = b^a and (a^b)^c = a^(b^c) => add(D) XOR 0 // a^a = 0 => add(D) // a^0 = a
// Insert a node at the start of the Xored LinkedList and // mark the newly inserted node as head void insert(Node** head_ref, int data) { // Allocate memory for new node Node* new_node = new Node(); new_node -> data = data; // Since new node is inserted at the // start , xnode of new node will always be // Xor of current head and NULL new_node -> xnode = head_ref; // If linkedlist is not empty, then xnode of // present head node will be Xor of new node // and node next to current head / if (head_ref != NULL) { // (head_ref)->xnode is Xor of (NULL and next). // If we Xor Null with next we get next (head_ref) -> xnode = Xor(new_node, (head_ref) -> xnode); } // Change head *head_ref = new_node; }
// list in forward direction void printList(Node* head) { Node* curr = head; Node* prev = NULL; Node* next; cout << "The nodes of Linked List are: \n"; // Till condition holds true while (curr != NULL) { // print current node cout << curr -> data << " "; // get address of next node: curr-
xnode is // next^prev, so curr->xnode^prev will
be // next^prev^prev which is next next = Xor(prev, curr -> xnode); // update prev and curr for next iteration prev = curr; curr = next; } } // main driver method int main() { Node* head = NULL; insert(&head, 10); insert(&head, 100); insert(&head, 1000); insert(&head, 10000); // Printing the created list printList(head); return (0); }