Wednesday, 18 October 2017

What is XOR Linked list and its benefit ?


XOR Linked list is the memory efficient version of Doubly Linked list.

An ordinary Doubly Linked list needs 2 pointers - for next and previous elements.
XOR Linked list uses bitwise XOR operation to save the space (Keeps single pointer with XOR of both the pointers instead of 2 pointers)

EXAMPLE
Node1 : npx = 0 XOR pointer(Node1)
Node2 : npx = pointer(Node1) XOR pointer(Node2)
Node3 : npx = pointer(Node2) XOR pointer(Node3)
Node4 : npx = pointer(Node3) XOR 0

What are the types of Linked list ?


Each element of Linked list have 2 items : Data and Pointer to next element

SINGLY L.L.
Every node have pointer of next node. Last node has pointer = NULL
1->2->3->4->NULL

DOUBLY L.L.
Every node have 2 pointers : for next and previous node
NULL<-1<->2<->3->NULL

CIRCULAR L.L.
It can be singly or doubly L.L. where all elements are connected to form a circle.
The next pointer of last node points to first node
1->2->3->1

 

Arrays vs. Linked list


ARRAYS VS. LINKED LIST
  1. Arrays are of fixed size, Not for Linked list
  2. Random access is not allowed in Linked list
  3. Extra memory needed in Linked list to store pointer to next element
  4. Insertion and deletion elements in arrays are expensive than Linked list

Where we can use stacks ?


STACK
A linear DS uses LIFO or FILO for accessing elements and allows PUSH, POP and PEAK operations.

Applications of stack
  • Infix to Postfix conversion
  • Evaluation of Postfix operation
  • Reverse a string
  • Check of balanced parentheses in an expression

Linear vs. Non-Linear data structures


LINEAR
Elements forms a sequence or a linear list.
E.g. : Arrays, Linked list, Stack, Queue

NON LINEAR
Traversal of nodes is non linear in nature.
E.g. : Tree, Graph