Tuesday, September 29, 2015

Circularly Linked List

Circularly linked list is a linked list where all nodes are connected to form a circle.There is no end or rear or null pointer.



Circularly Linked List

Circular linked list can be implemented in both singly linked list as well as doubly linked list.
The only difference between implementation of a circularly linked list and other linked list is that, Circularly linked list doesn't have a last element instead points to the head.Whereas other linked list points to null in place of the head.

To traverse through all the elements in the circularly linked list, we need to use two loops 
  1. One which checks the condition that the head reference is not equal to null
  2. Other loop which checks if the loop doesn't repeat. i.e temp variable doesn't equal to head.

Function to push an element into the Circularly linked list 





Source: Geeksforgeeks
Also Refer to Tortoise-Hare Algorithm

Doubly Linked list

A doubly linked list contains an extra pointer which points to the previous element of a list and which does not exist in a singly linked list.This makes easy traversal of a linked list to the previous elements.


Algorithm for insertion 

of an element in a Doubly Linked List . Insertion can be classified into 3 different methods.
  1. Insertion at the Head
  2. Insertion at a given postion 
  3. Insertion at the end of the Linked List 

Insertion at the head



Insertion after a given position


Insertion of a node towards the end



Algorithm for deletion of a node



Algorithm for reversing a Doubly Linked List


Source: GeeksforGeeks

Advantages over Singly Linked list

  1. Can be traversed front and back from any location
  2. Less ambiguity in deleting an element from the linked list.

Disadvantages over a Singly Linked list

  1. Every Node takes in an extra pointer when a linked list could be created with a single pointer.
  2. Extra maintenance.
Important Links to Follow
Doubly Linked List(Geeksforgeeks)
Code for doubly Linked List

Thursday, September 24, 2015

Tortoise & Hare Algorithm

Tortoise & Hare algorithm is also known as Floyd's Cycle-Finding Algorithm.This is a technique for detecting infinite loops inside a linked list, symlinks, state machines etc.

To detect a loop inside a linked list
Source
  • Make a pointer to start of the linked list and call it Hare 
  • Make a pointer to the head of the linked list and call it Tortoise 
  • advance the Hare by two and the tortoise by one for every interation.
  • There's a loop if the Tortoise and Hare meet
  • There's no loop if the Hare reaches the end.
Algorithm is intersting as it takes O(n) time and O(1) space.

Python code to understand Tortoise and Hare algorithms
Source




With this Algorithm we detect the loops and iterations in a circularly linked list as suggested by the name Floyd's Cycle-finding Algorithm.

More Reference: Coding Freak

Sunday, September 20, 2015

Stack

Stack is a linear data structure which follows a particular order in which the operations are performed. Order is LIFO(Last In First Out) or FILO(First In Last Out)

Three main operations of a stack are :

  1. Push
  2. Pop
  3. Peek or Top
Stack


Push
The Push Operation pushes an element into the stack.

Pop
Pop operation removes the top most element in the stack.

Peek
Peek or top operation returns the top most value of the stack.


To understand stack, you can assume a pile of books placed one over the other.To get the last book in the stack, one needs to take away all the books before the last book.


Applications of Stack
  1. Recursion
  2. Tower of Hanoi
  3. Converting Infix to Postfix.


Stack can be implemented in two different ways: 
  1. Stack Implementation using Arrays 
  2. Stack Implementation using Linked list
The implemented codes are carried out into separate blogs in the above given links.

More stack related blogs


Stack Implementation Using Linked list

Implementation of a stack using linked list is dynamic and flexible. Yet the space consumed is high and adding or manipulating the linked list is complex.
Refer to the code for more details. The Code language is C++.

Code:



Output:




Stack Implementation Using Array

This blog will deal with implementation of stack using arrays.The implementation is simple yet we need to allocate a pre-defined size.The allocated size may or may not be utilized completely. This makes the array implementation non-flexible.

Using Array





Output





Linked List v/s Arrays

Linked list is a replacement to the arrays. Since arrays take in a block of memory so the flexibility is very less.It has a defined structure which uses a limited memory and uses only when the memory is required. Though adding more elements is a cumbersome task.

Each node in a list consists of at least two parts:
  1. Data
  2. Pointer to the next node





A tabular comparison of Arrays v/s Linked List:

S. No
Parameter
Arrays
Linked list
1
Cost
Constant time O(1)
O(n)
2
Memory usage
Fixed Size
Contiguous Blocks of memory
No unused memory,
Extra memory for pointer variables
3
Cost of inserting and Deleting elements
At the beginning
O(n)
Shifting one index at a time to the right
Creating a new node and head pointer
O(1)
At the end
If Array not full O(1) else O(n)
Traversing a new node & End
At the nth position
O(n)
O(n)
4
Ease of Use
Very Easy
Complicated especially in C/C++

Advantages over arrays

  1. Dynamic size
  2. Ease of insertion/deletion

Drawbacks:
  1. Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists.
  2. Extra memory space for a pointer is required with each element of the list.

Linked List

linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference  to the next node in the sequence.

                                   

Types of Linked List: 

Based on the working of a data structure, we list only a few important linked list models.
  1. Singly Linked list
  2. Doubly Linked list 
  3. Circular Linked list

Others forms of linked lists include multiply linked list, sentinal nodes etc.

Advantages

  • Linked lists are a dynamic data structure, allocating the needed memory while the program is running.
  • Insertion and deletion node operations are easily implemented in a linked list.
  • Linear data structures such as stacks and queues are easily executed with a linked list.
  • They can reduce access time and may expand in real time without memory overhead.

Disadvantages


  • They have a tendency to use more memory due to pointers requiring extra storage space.
  • Nodes in a linked list must be read in order from the beginning as linked lists are inherently sequential access.
  • Nodes are stored incontiguously, greatly increasing the time required to access individual elements within the list.
  • Difficulties arise in linked lists when it comes to reverse traversing. For instance, singly linked lists are cumbersome to navigate backwards and while doubly linked lists are somewhat easier to read, memory is wasted in allocating space for a back pointer.

Abstract Data Types(ADT)

An abstract data type (ADT) is a mathematical model for data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations.This contrasts with data structures, which are concrete representations of data, and are the point of view of an implementer, not a user.

ADT is a theoretical concept in computer science, used in the design and analysis of algorithms, data structures, and software systems, and do not correspond to specific features of any computer languages.

In ADTs,inferences are derived using a mathematical or a logical module.It is very much equivalent to writing a set of points understandable to a user.Lets assume a list with user input data which can be implemented in two ways:
  1. Using Arrays
  2. Using Linked Lists
Both have their own set of advantages and disadvantages,But it is made sure that advantages are used to make our understanding ends meet and the idea to implement looks concrete.

Each data structure has its limitations. One such data structure is a Linked list which is followed in the next chapter.