## Data Structures

#### What is a Data Structure?

A data structure is a way of defining, storing & retrieving of data in a structural & systematic way. A data structure may contain a different type of data items. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. (Wikipedia)

#### What are the various data-structures available?

Data structure availability may vary by programming languages. Commonly available data structures are:

- Lists
- Arrays
- Stack
- Queues
- Graph
- Tree

#### What is an algorithm?

An algorithm is a step by step procedure, which defines a set of instructions to be executed in certain order to get the desired output. A computer program can be viewed as an elaborate algorithm. In mathematics and computer science, an algorithm usually means a small procedure that solves a recurrent problem.

#### What are linear and non linear Data Structures?

**Linear:**A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array. Linked List, Stacks and Queues**Non-Linear:**A data structure is said to be non-linear if traversal of nodes is nonlinear in nature. Example: Graph and Trees.

#### What are the various operations that can be performed on different Data Structures?

**Insertion**: Add a new data item in the given collection of data items.**Deletion**: Delete an existing data item from the given collection of data items.**Traversal**: Access each data item exactly once so that it can be processed.**Searching**: Find out the location of the data item if it exists in the given collection of data items.**Sorting**: Arranging the data items in some order i.e. in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data.

#### What is Stack and where it can be used?

Stack is a linear data structure which the order LIFO (Last In First Out) or FILO( First In Last Out) for accessing elements. Basic operations of stack are : Push, Pop , Peek.

Applications of Stack:

Applications of Stack:

- Infix to Postfix Conversion using Stack
- Evaluation of Postfix Expression
- Reverse a String using Stack
- Implement two stacks in an array
- Check for balanced parentheses in an expression

#### What are Infix, prefix, Postfix notations?

**Infix notation:**X**+**Y – Operators are written in-between their operands. This is the usual way we write expressions. An expression such asA * ( B + C ) / D

**Postfix notation (also known as “Reverse Polish notation”):**X Y**+**Operators are written after their operands. The infix expression given above is equivalent toA B C + * D/

**Prefix notation (also known as “Polish notation”):**+ X Y/ * A + B C D

#### What is a Linked List and What are its types?

A linked list is a linear data structure (like arrays) where each element is a separate object. Each element (that is node) of a list is comprising of two items – the data and a reference to the next node.Types of Linked List:

**Singly Linked List :**In this type of linked list, every node stores address or reference of next node in list and the last node has next address or reference as NULL. For example 1->2->3->4->NULL**Doubly Linked List :**Here,**Circular Linked List :**Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked list. Eg. 1->2->3->1 [The next pointer of last node is pointing to the first]

#### How is an Array different from Linked List?

- The size of the arrays is fixed, Linked Lists are Dynamic in size.
- Inserting and deleting a new element in an array of elements is expensive, Whereas both insertion and deletion can easily be done in Linked Lists.
- Random access is not allowed in Linked Listed.
- Extra memory space for a pointer is required with each element of the Linked list.
- Arrays have better cache locality that can make a pretty big difference in performance.

#### Which data structures are used for BFS (Breadth First Traversal) and DFS (Depth First Search) of a graph?

- Queue is used for BFS
- Stack is used for DFS. DFS can also be implemented using recursion (Note that recursion also uses function call stack).

#### Can doubly linked be implemented using a single pointer variable in every node?

Doubly linked list can be implemented using a single pointer.

#### How to implement a stack using queue?

A stack can be implemented using two queues. Let stack to be implemented be ‘s’ and queues used to implement be ‘q1’ and ‘q2’. Stack ‘s’ can be implemented in two ways:

- Method 1 (By making push operation costly)
- Method 2 (By making pop operation costly) See Implement Stack using Queues