Data Structures and Algorithms
Data Structures and Algorithms
A data structure is a storage that is used to store and organize data. It is a way of arranging data on a
computer so that it can be accessed and updated efficiently.
A data structure is not only used for organizing the data. It is also used for processing, retrieving, and
storing data.
• Linear data structure: Data structure in which data elements are arranged sequentially or
linearly, where each element is attached to its previous and next adjacent elements, is called a linear
data structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
• Static data structure: Static data structure has a fixed memory size. It is easier to access
the elements in a static data structure.
• Dynamic data structure: In dynamic data structure, the size is not fixed. It can be
randomly updated during the runtime which may be considered efficient concerning the
memory (space) complexity of the code.
• Non-linear data structure: Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all the
elements in a single run only.
Examples of non-linear data structures are trees and graphs.
What is Array?
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple
items of the same type together. This makes it easier to calculate the position of each element by
simply adding an offset to a base value, i.e., the memory location of the first element of the array
(generally denoted by the name of the array).
What is String?
Strings are defined as an array of characters. The difference between a character array and a string is
the string is terminated with a special character ‘\0’.
The Cy
How is String represented in Memory?
In C, a string can be referred to either using a character pointer or as a character array. When strings
are declared as character arrays, they are stored like other types of arrays in C. For example, if str[] is
an auto variable then the string is stored in the stack segment, if it’s a global or static variable then
stored in the data segment, etc.
What is Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers as shown in the below image:
In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
What is Stack?
Stack is a linear data structure that follows a particular order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that
is inserted last, comes out first and FILO implies that the element that is inserted first, comes out
last.
There are many real-life examples of a stack. Consider an example of plates stacked over one another
in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has
been placed at the bottommost position remains in the stack for the longest period of time. So, it can
be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.
What is Queue Data Structure?
A Queue is defined as a linear data structure that is open at both ends and the operations are
performed in First in First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end, and all deletions
from the list are made at the other end. The element that is first pushed into the order, the
operation is first performed on that.
The position of the entry in a queue ready to be served, that is, the first entry that will be removed
from the queue, is called the front of the queue(sometimes, head of the queue), similarly, the
position of the last entry in the queue, that is, the one most recently added, is called the rear (or the
tail) of the queue. See the below figure.
Characteristics of Queue:
The topmost node of the tree is called the root, and the nodes below it are called the child nodes.
Each node can have multiple child nodes, and these child nodes can also have their own child nodes,
forming a recursive structure.
This data structure is a specialized method to organize and store data in the computer to be used
more effectively. It consists of a central node, structural nodes, and sub-nodes, which are connected
via edges. We can also say that tree data structure has roots, branches, and leaves connected with
one another.
Binary tree: In a binary tree, each node can have a maximum of two children linked to it.
Some common types of binary trees include full binary trees, complete binary trees,
balanced binary trees, and degenerate or pathological binary trees.
Ternary Tree: A Ternary Tree is a tree data structure in which each node has at most three
child nodes, usually distinguished as “left”, “mid” and “right”.
N-ary Tree or Generic Tree: Generic trees are a collection of nodes where each node is a data
structure that consists of records and a list of references to its children(duplicate references
are not allowed). Unlike the linked list, each node stores the address of multiple nodes.
2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than Linked List and
slower than arrays).
3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than Unordered Linked
Lists).
4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on the number of nodes as
nodes are linked using pointers.