advanced trees and graphs, Lecture notes of Data Structures and Algorithms

b tree,red blACK TRESS ,SCAPE GOAT TREES, HEAP

Typology: Lecture notes

2020/2021

Uploaded on 01/02/2022

guna_shekar
guna_shekar 🇮🇳

4 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download advanced trees and graphs and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Generic Linked List in C

• Generic linked lists are implementations of linked lists

which can store data of any data type.

struct Node { int data; Node* next; } struct Node { void *data; struct Node *next; }; The void pointer can store the address of any data type

printIt()

• To print the contents of a

linked list, this function will

require

  • (^) the pointer to the head of the linked list,
  • (^) the suitable function to print according to the type of data stored in it. void printIt( struct Node node, void (prt)( void )) { while (node != NULL ) { (prt)(node->data); node = node->next; } printf("\n"); } // function to print integer values void prInt( void *x){ printf(" %d", *( int *)x ); } // function to print float values void prFlt( void *x){ printf(" %f", *( float *)x ); }

Main() Function

  • int main()
  • {
  • struct Node *start = NULL ;
  • // Create and print linked list storing int data
  • unsigned int_size = sizeof( int );
  • int a[] = {7, 42, 11, 41, 5};
  • for ( int i=4; i>=0; i--)
  • push_front(&start, &a[i], int_size);
  • printIt(start, prInt);
  • // Create and print linked list storing float data
  • unsigned float_size = sizeof( float );
  • start = NULL ;
  • float b[] = { 1 .1, 6.43, 4.2, 3.14, 5.08};
  • for ( int i=4; i>=0; i--)
  • push_front(&start, &b[i], float_size);
  • printIt(start, prFlt);
  • return 0;
  • }

A Memory Efficient Doubly

Linked

Memory-efficient version of

Doubly Linked List that can be

created using only one space for

the address field with every node

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 new Node

// 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; }

// It simply prints contents of doubly linkedPrint List

// 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); }