KEMBAR78
Data Structure U-1 | PDF | Queue (Abstract Data Type) | Data Structure
0% found this document useful (0 votes)
11 views22 pages

Data Structure U-1

Data structure note

Uploaded by

routsubrat297
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

Data Structure U-1

Data structure note

Uploaded by

routsubrat297
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Data Structure

A data structure is a particular way of organising data in a computer so that it


can be used effectively. The idea is to reduce the space and time complexities of
different tasks.
Need Of Data Structure:
The structure of the data and the synthesis of the algorithm are relative to each
other. Data presentation must be easy to understand so the developer, as well
as the user, can make an efficient implementation of the operation.
Data structures provide an easy way of organising, retrieving, managing, and
storing data.
Here is a list of the needs for data.
 Data structure modification is easy.
 It requires less time.
 Save storage memory space.
 Data representation is easy.
 Easy access to the large database
Classification/Types of Data Structures:
1. Linear Data Structure
2. Non-Linear Data Structure.
Linear Data Structure:
 Elements are arranged in one dimension ,also known as linear
dimension.
 Example: lists, stack, queue, etc.
Non-Linear Data Structure
 Elements are arranged in one-many, many-one and many-many
dimensions.
 Example: tree, graph, table, etc.
Most Popular Data Structures:
1. Array:
An array is a collection of data 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).

Array Data Structure


2. Linked Lists:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list
elements are not stored at a contiguous location; the elements are linked using
pointers.

Linked Data Structure


3. Stack:
Stack is a linear data structure which 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). In stack, all insertion and deletion are permitted at only one end of
the list.

Stack Operations:
 push(): When this operation is performed, an element is inserted into the
stack.
 pop(): When this operation is performed, an element is removed from
the top of the stack and is returned.
 top(): This operation will return the last inserted element that is at the
top without removing it.
 size(): This operation will return the size of the stack i.e. the total
number of elements present in the stack.
 isEmpty(): This operation indicates whether the stack is empty or not.
4. Queue:
Like Stack, Queue is a linear structure which follows a particular order in which
the operations are performed. The order is First In First Out (FIFO). In the
queue, items are inserted at one end and deleted from the other end. A good
example of the queue is any queue of consumers for a resource where the
consumer that came first is served first. The difference between stacks and
queues is in removing. In a stack we remove the item the most recently added;
in a queue, we remove the item the least recently added.

Queue Data Structure


Queue Operations:
 Enqueue(): Adds (or stores) an element to the end of the queue..
 Dequeue(): Removal of elements from the queue.
 Peek() or front(): Acquires the data element available at the front node
of the queue without deleting it.
 rear(): This operation returns the element at the rear end without
removing it.
 isFull(): Validates if the queue is full.
 isNull(): Checks if the queue is empty.
5. Binary Tree:
Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures,
trees are hierarchical data structures. A binary tree is a tree data structure in
which each node has at most two children, which are referred to as the left
child and the right child. It is implemented mainly using Links.
A Binary Tree is represented by a pointer to the topmost node in the tree. If the
tree is empty, then the value of root is NULL. A Binary Tree node contains the
following parts.
1. Data
2. Pointer to left child
3. Pointer to the right child

Binary Tree Data Structure


6. Binary Search Tree:
A Binary Search Tree is a Binary Tree following the additional properties:
 The left part of the root node contains keys less than the root node key.
 The right part of the root node contains keys greater than the root node
key.
 There is no duplicate key present in the binary tree.
A Binary tree having the following properties is known as Binary search tree
(BST).

Binary Search Tree Data Structure


7. Heap:
A Heap is a special Tree-based data structure in which the tree is a complete
binary tree. Generally, Heaps can be of two types:
 Max-Heap: In a Max-Heap the key present at the root node must be
greatest among the keys present at all of its children. The same property
must be recursively true for all sub-trees in that Binary Tree.
 Min-Heap: In a Min-Heap the key present at the root node must be
minimum among the keys present at all of its children. The same
property must be recursively true for all sub-trees in that Binary Tree.
Max and Min Heap
8. Hash Table Data Structure:
Hashing is an important Data Structure which is designed to use a special
function called the Hash function which is used to map a given value with a
particular key for faster access of elements. The efficiency of mapping depends
on the efficiency of the hash function used.
Let a hash function H(x) maps the value x at the index x%10 in an Array. For
example, if the list of values is [11, 12, 13, 14, 15] it will be stored at positions
{1, 2, 3, 4, 5} in the array or Hash table respectively.
Hash Data Structure
9. Matrix:
A matrix represents a collection of numbers arranged in an order of rows and
columns. It is necessary to enclose the elements of a matrix in parentheses or
brackets.
A matrix with 9 elements is shown below.

Matrix
10. Trie:
Trie is an efficient information retrieval data structure. Using Trie, search
complexities can be brought to an optimal limit (key length). If we store keys in
the binary search tree, a well-balanced BST will need time proportional to M *
log N, where M is maximum string length and N is the number of keys in the
tree. Using Trie, we can search the key in O(M) time. However, the penalty is on
Trie storage requirements.

Trie Data Structure


11. Graph:
Graph is a data structure that consists of a collection of nodes (vertices)
connected by edges. Graphs are used to represent relationships between
objects and are widely used in computer science, mathematics, and other
fields. Graphs can be used to model a wide variety of real-world systems, such
as social networks, transportation networks, and computer networks.
Graph Data Structure

Array is a collection of items of the same variable type that are stored at
contiguous memory locations. It is one of the most popular and simple data
structures used in programming.

Arrays work on an index system starting from 0 to (n-1), where n is the size of
the array.
Basic terminologies of Array
 Array Index: In an array, elements are identified by their indexes. Array
index starts from 0.
 Array element: Elements are items stored in an array and can be
accessed by their index.
 Array Length: The length of an array is determined by the number of
elements it can contain.
Memory representation of Array
In an array, all the elements are stored in contiguous memory locations. So, if
we initialize an array, the elements will be allocated sequentially in memory.
This allows for efficient access and manipulation of elements.

2/3

Declaration of Array
Arrays can be declared in various ways in different languages. For better
illustration, below are some language-specific array declarations:
C++CJavaPythonC#Javascript
// This array will store integer type element
int arr[5];

// This array will store char type element


char arr[10];

// This array will store float type element


float arr[20];
Initialization of Array
Arrays can be initialized in different ways in different languages. Below are
some language-specific array initializations:
C++CJavaPythonC#JavaScript
int arr[] = { 1, 2, 3, 4, 5 };
char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
float arr[10] = { 1.4, 2.0, 24, 5.0, 0.0 };

Types of Arrays
There are majorly two types of arrays, they are:
One-Dimensional Arrays:

You can imagine a 1d array as a row, where elements are stored one after
another.
Multi-Dimensional Arrays:
These multi-dimensional arrays are again of two types. They are:
Two-Dimensional Arrays:

You can imagine it like a table where each cell contains elements.
Three-Dimensional Arrays:

You can imagine it like a cuboid made up of smaller cuboids where each cuboid
can contain an element.
In this "arrays in data structures" tutorial, you will work around one-
dimensional arrays.

What Operations Can You Perform on an Array?


 Traversal
 Insertion
 Deletion
 Searching
 Sorting

Array Traversal
Array traversal refers to the process of accessing and processing each element
of an array sequentially. This is one of the most fundamental operations in
programming, as arrays are widely used data structures for storing multiple
elements in a single variable.
How Array Traversal Works?
When an array is created, it occupies a contiguous block of memory where
elements are stored in an indexed manner. Each element can be accessed using
its index, which starts from 0 in most programming languages.
For example, consider an array containing five integers:
arr = [10, 20, 30, 40, 50]
Here:
 The first element (10) is at index 0.
 The second element (20) is at index 1.
 The last element (50) is at index 4.

Traversal in an array is a process of visiting each element once.


Code:
#include<stdio.h>
int main()
{
int a[5] = {2, 3, 5, 7, 11};
for(int i=0;i<5;i++)
{
//traversing ith element in the array
printf(“%d\n”,a[i]);
}
return 0;
}

2.Insertion in Array
Insertion in an array refers to the process of adding a new element at a
specific position while maintaining the order of the existing elements.
Since arrays have a fixed size in static implementations, inserting an
element often requires shifting existing elements to make space.
1. Identify the Position: Determine where the new element should be
inserted.
2. Shift Elements: Move the existing elements one position forward to
create space for the new element.
3. Insert the New Element: Place the new value in the correct position.
4. Update the Size (if applicable): If the array is dynamic, its size is
increased.
For example, if we have the array:
arr = [10, 20, 30, 40, 50]
Types of Insertion
1. Insertion at the Beginning (Index 0)
 Every element must shift one position right.
 This is the least efficient case for large arrays as it affects all elements.
2. Insertion at a Specific Index
 Elements after the index shift right.
 If the index is in the middle, half of the array moves.
3. Insertion at the End
 The simplest case since no shifting is required.
 Used in dynamic arrays where size increases automatically (e.g., Python
lists, Java ArrayList).

At the Beginning

Code:
#include<stdio.h>
int main()
{
int array[10], n,i, item;
printf("Enter the size of array: ");
scanf("%d", &n);
printf("\nEnter Elements in array: ");
for(i=0;i<n;i++)
{
scanf("%d", &array[i]);
}
printf("\n enter the element at the beginning");
scanf("%d", &item);
n++;
for(i=n; i>1; i--)
{
array[i-1]=array[i-2];
}
array[0]=item;
printf("resultant array element");
for(i=0;i<n;i++)
{
printf("\n%d", array[i]);
}
getch();
return 0;
}

At the End:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int array[10], i, values;
printf("Enter 5 Array Elements: ");
for(i=0; i<5; i++)
scanf("%d", &array[i]);
printf("\nEnter Element to Insert: ");
scanf("%d", &values);
array[i] = values;
printf("\nThe New Array is:\n");
for(i=0; i<6; i++)
printf("%d ", array[i]);
getch();
return 0;
}

At a Specific Position:
Code:
#include <stdio.h>
int main()
{
int array[100], pos, size, val;
printf("Enter size of the array:");
scanf("%d", &size);
printf("\nEnter %d elements\n", size);
for (int i = 0; i < size; i++)
scanf("%d", &array[i]);
printf("Enter the insertion location\n");
scanf("%d", &pos);
printf("Enter the value to insert\n");
scanf("%d", &val);
for (int i = size - 1; i >= pos - 1; i--)
array[i+1] = array[i];
array[pos-1] = val;
printf("Resultant array is\n");
for (int i = 0; i <= size; i++)
printf("%d\n", array[i]);
return 0;
}
Deletion in Array
Deletion in an array refers to the process of removing an element
from a specific position while maintaining the order of the remaining
elements. Unlike linked lists, where deletion is efficient, removing an
element from an array requires shifting elements to fill the gap.
1. Identify the Position: Find the index of the element to be deleted.
2. Shift Elements: Move the elements after the deleted element one
position to the left.
3. Update the Size (if applicable): If using a dynamic array, the size
might be reduced.
For example, consider the array:
arr = [10, 20, 30, 40, 50]
Types of Deletion
1. Deletion at the Beginning (Index 0)
 Every element shifts left by one position.
 This is the most expensive case as it affects all elements.
2. Deletion at a Specific Index
 Only elements after the index shift left.
 If the index is in the middle, half of the array moves.
3. Deletion at the End
 The simplest case since no shifting is required.
 The size of the array is reduced (in dynamic arrays).

At the Beginning:

#include<stdio.h>

int main()

int n,array[10];

printf("enter the size of an array");

scanf("%d" ,&n);

printf("enter elements in an array");

for(int i=0;i<n;i++)

scanf("%d", &array[i]);

n--;

for(int i=0;i<n;i++)

array[i]=array[i+1];
printf("\nafter deletion ");

for(int i=0;i<n;i++)

printf("\n%d" , array[i]);

At the End:

#include<stdio.h>

int main()

int n,array[10];

printf("enter the size of an array");

scanf("%d" ,&n);

printf("enter elements in an array");

for(int i=0;i<n;i++)

scanf("%d", &array[i]);
printf("\nafter deletion array elements are");

for(int i=0;i<n-1;i++)

printf("\n%d" , array[i]);

You might also like