0% found this document useful (0 votes)
11 views

Data

Data Structure note

Uploaded by

shubham
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Data

Data Structure note

Uploaded by

shubham
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structure is a way of collecting and organising data in such a way that we can perform operations

on these data in an effective way.

In simple language, Data Structures are structures programmed to store ordered data, so that various
operations can be performed on it easily. It represents the knowledge of data to be organized in
memory. It should be designed and implemented in such a way that it reduces the complexity and
increases the efficiency.

The data structures can also be classified on the basis of the following characteristics:

Characterstic Description
In Linear data structures,the data items are arranged in a linear sequence.
Linear
Example: Array
In Non-Linear data structures,the data items are not in sequence. Example:
Non-Linear
Tree, Graph
In homogeneous data structures,all the elements are of same type. Example:
Homogeneous
Array
Non- In Non-Homogeneous data structure, the elements may or may not be of the
Homogeneous same type. Example: Structures
Static data structures are those whose sizes and structures associated memory
Static
locations are fixed, at compile time. Example: Array
Dynamic structures are those which expands or shrinks depending upon the
Dynamic program need and its execution. Also, their associated memory locations
changes. Example: Linked List created using pointers
What is an Algorithm ?
An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain
predefined task. Algorithm is not the complete code or program, it is just the core logic(solution)
of a problem, which can be expressed either as an informal high level description as pseudocode
or using a flowchart.

Every Algorithm must satisfy the following properties:

1. Input- There should be 0 or more inputs supplied externally to the algorithm.


2. Output- There should be atleast 1 output obtained.
3. Definiteness- Every step of the algorithm should be clear and well defined.
4. Finiteness- The algorithm should have finite number of steps.
5. Correctness- Every step of the algorithm must generate a correct output.

An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less
memory space. The performance of an algorithm is measured on the basis of following
properties :

1. Time Complexity
2. Space Complexity

Sorting is nothing but arranging the data in ascending or descending order. The term sorting came into
picture, as humans realised the importance of searching quickly.Sorting arranges data in a sequence
which makes searching easier.

Different Sorting Algorithms

There are many different techniques available for sorting, differentiated by their efficiency and
space requirements. Following are some sorting techniques which we will be covering in next
few tutorials.

1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Heap Sort
Linear Search

Linear search is a very basic and simple search algorithm. In Linear search, we search an element
or value in a given array by traversing the array from the starting, till the desired element or
value is found.

It compares the element to be searched with all the elements present in the array and when the
element is matched successfully, it returns the index of the element in the array, else it return -1.

Linear Search is applied on unsorted or unordered lists, when there are fewer elements in a list.

Features of Linear Search Algorithm

1. It is used for unsorted and unordered small list of elements.


2. It has a time complexity of O(n), which means the time is linearly dependent on the number of
elements, which is not bad, but not that good too.
3. It has a very simple implementation.

Arrays
An array is a collection of similar data elements. These data elements have the same data type.
The elements of the array are stored in consecutive memory locations and are referenced by an
index (also known as the subscript).

Arrays are generally used when we want to store large amount of similar type of data. But they
have the following limitations:
Σ Arrays are of fixed size.
Σ Data elements are stored in contiguous memory locations which may not be always available.
Σ Insertion and deletion of elements can be problematic because of shifting of elements from
their positions.

Step 1: [INITIALIZATION] SET I = lower_bound


Step 2: Repeat Steps 3 to 4 while I <= upper_bound
Step 3: Apply Process to A[I]
Step 4: SET I = I + 1
[END OF LOOP]
Step 5: EXIT

Step 1: Set upper_bound = upper_bound + 1


Step 2: Set A[upper_bound] = VAL
Step 3: EXIT

Step 1: [INITIALIZATION] SET I = N


Step 2: Repeat Steps 3 and 4 while I >= POS
Step 3: SET A[I + 1] = A[I]
Step 4: SET I = I – 1
[END OF LOOP]
Step 5: SET N = N + 1
Step 6: SET A[POS] = VAL
Step 7: EXIT

Step 1: SET upper_bound = upper_bound - 1


Step 2: EXIT

Step 1: [INITIALIZATION] SET I = POS


Step 2: Repeat Steps 3 and 4 while I <= N – 1
Step 3: SET A[I] = A[I + 1]
Step 4: SET I = I + 1
[END OF LOOP]
Step 5: SET N = N – 1
Step 6: EXIT

A linked list
does not store its elements in consecutive memory locations and the user can add any number
of elements to it. However, unlike an array, a linked list does not allow random access of data.
Elements in a linked list can be accessed only in a sequential manner.

A linked list, in simple terms, is a linear collection of data elements. These data elements are
called nodes. Linked list is a data structure which in turn can be used to implement other data
. structures. Thus, it acts as a building block to implement data structures such as stacks, queues,
and their variations.

Since
in a linked list, every node contains a pointer to another node which is of the same type, it is also
called a self-referential data type.

let us briefly discuss the basic concept behind it. The computer maintains a list of
all free memory cells. This list of available space is called the free pool

We have seen that every linked list has a pointer variable START which stores the address of the
first node of the list. Likewise, for the free pool (which is a linked list of all free memory cells),
we have a pointer variable AVAIL which stores the address of the first free space.

The operating system does this task of


adding the freed memory to the free pool. The
operating system will perform this operation
whenever it finds the CPU idle or whenever the
programs are falling short of memory space.
The operating system scans through all the
memory cells and marks those cells that are
being used by some program. Then it collects
all the cells which are not being used and adds

their address to the free pool, so that these cells can be reused by other programs. This process
is called garbage collection.

Step 1: [INITIALIZE] SET PTR = START


Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR DATA
Step 4: SET PTR = PTR NEXT
[END OF LOOP]
Step 5: EXIT

Step 1: [INITIALIZE] SET =


Step 2: [INITIALIZE] SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR != NULL
Step 4: SET = +1
Step 5: SET PTR = PTR NEXT
[END OF LOOP]
Step 7: EXIT
COUNT
COUNT COUNT
Step 6: Write COUNT

In a circular linked list, the last node contains a pointer to the first node of the list. We can have
a circular singly linked list as well as a circular doubly linked list. While traversing a circular
linked list, we can begin at any node and traverse the list in any direction, forward or backward,
until we reach the same node where we started. Thus, a circular linked list has no beginning and
no ending. Figure 6.26 shows a circular linked list.

doubly linked list or a two-way linked list is a more complex type of linked list which contains
a pointer to the next as well as the previous node in the sequence. Therefore, it consists of three
parts—data, a pointer to the next node, and a pointer to the previous node

Unlike C++ and Java, C doesn’t support generics. How to create a linked list in C that can be used for any
data type? In C, we can use void pointer and function pointer to implement the same functionality. The
great thing about void pointer is it can be used to point to any data type. Also, size of all types of
pointers is always is same, so we can always allocate a linked list node. Function pointer is needed
process actual content stored at address pointed by void pointer.
stack is a linear data structure which uses the same principle, i.e., the elements in a stack are
added and removed only from one end, which is called the
TOP. Hence, a stack is called a LIFO (Last-In-First-Out) data
structure, as the element that was inserted last is the first one to
be taken out.

Reversing a list
Σ Parentheses checker
Σ Conversion of an infix expression into a postfix expression
Σ Evaluation of a postfix expression
Σ Conversion of an infix expression into a prefix expression
Σ Evaluation of a prefix expression
Σ Recursion
Σ Tower of Hanoi

A deque (pronounced as ‘deck’ or ‘dequeue’) is a list in which the elements can be inserted or
deleted at either end. It is also known as a head-tail linked list because elements can be added to
or removed from either the front (head) or the back (tail) end.
However, no element can be added and deleted from the middle. In the computer’s memory, a
deque is implemented using either a circular array or a circular doubly linked list. In a deque, two
pointers are maintained, LEFT and RIGHT, which point to either end of the deque.

You might also like