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

Module 1 Notes

Uploaded by

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

Module 1 Notes

Uploaded by

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

Module 1

Introduction to Data Structures

Contents
• Introduction to Data Structures

• Concept of ADT

• Types of Data Structure

❖ Linear and Non-Linear

• Operations on Data Structures

A. VASAVI
Introduction to Data Structures

Data Structures:

Data structures are a specific way of organizing data in a specialized format on a


computer so that the information can be organized, processed, stored, and
retrieved quickly and efficiently.
• Data structures make it easy for users to access and work with the data they
need in appropriate ways.
• Data Structure is simply a way to store the data.
Depending on your requirement, it is important to choose the right data structure
for your project.
For example, if you want to store data sequentially in the memory, then you can
go for the Array data structure.

Data Structures mainly specifies the following four things:


1. Organization of data
2. Accessing methods
3. Processing methods
4. Degree of association

A. VASAVI
1. Organization of data: (How we store data inside memory)
The collection of data you work within a program have some kind of structure or
organization.
Data structures are divided into two fundamental types.
• Contiguous structure
• Non-Contiguous structure
Contiguous structure:
In contiguous structures, terms of data are kept together in memory.
An array is an example of a contiguous structure. Since each element in the array
is located next to one or two other elements.

Types of contiguous structures:

❖ Fixed size partitioning contains data items of same size


❖ In Variable partitioning the size of data items may differ.

A. VASAVI
Non-Contiguous structure:
In this technique, each process is allocated a series of non-contiguous blocks of
memory that can be located anywhere in the physical memory.
Non-contiguous allocation involves the use of pointers to link the non-
contiguous memory blocks allocated to a process.

Non-contiguous structures are implemented as a collection of data-items, called


nodes, where each node can point to one or more other nodes in the collection.
The simplest kind of non- contiguous structure is linked list.

A. VASAVI
2. Accessing methods:
Every data structure has different accessing methods.
Stack – top()
Queue – front(), rear()
Linked list – head()

3. Processing methods:
Every data structure has different processing methods based on its type.
Stack – push(x), pop()
Queue – Enqueue, Dequeue
Linked list – head()

4. Degree of association:
Degree represents the number of entities that are associated with a
particular node.

A. VASAVI
Types of Data Structures

Primitive Data Structures:

• Primitive data structures are those which have predefined way of storing
data by the system.
• All system supported datatypes are called primitive data structures.
• They are built into the programming language.
• They occupy a fixed amount of memory space, making it easier to manage.
• The primitive data structure consists of fundamental data types like float,
character, integer, etc.

Non-Primitive Data Structures:


• There are certain situations when primitive data structures are not sufficient
for our task to be performed.

A. VASAVI
• Non-primitive data structures are constructed using primitive data types
and can hold multiple values or objects.
• They provide the flexibility to manage complex data scenarios effectively.
• The size of the non-primitive data structure is not fixed.
Linear Data Structures:
• In a linear data structure, values are arranged in a linear order (sequence).
• In these structures, we can perform operations such as insertion or deletion
in a linear sequence.
Linear Data Structures are divided into two types:
Static Data Structures &
Dynamic Data Structures
Static Data Structure:
• In static data structure, the size of the structure is fixed.
• The content of the data structure can be modified but without changing the
memory space allocated to it.
• Arrays are the best example of static data structures as they are of a fixed
size and we can modify their data later.
• Memory is allocated at compile time.
It is easier to access the elements in a static data structure.

Arrays:
• An array is a linear data structure where it stores multiple items of the
same type together in one place.
• It is a group of data elements arranged in adjacent memory locations.

A. VASAVI
• The index gives the direct address of each element, making arrays a highly
efficient data structure for accessing different elements.

Dynamic Data Structure:


In dynamic data structure, the size of the structure is not fixed and can be modified
during the operations performed on it.
Dynamic data structures are designed to facilitate change of data structures in the
run time.
There is no memory loss and we can allocate space equal to the required number
of elements.
Examples of this data structure are stack, queue, Linked list.

A. VASAVI
Stack:
• A Stack is a linear data structure in which all the insertions and deletions
are done at only one end.
• A Stack is also referred to as LIFO list i.e., the data item which is entered
last is deleted first.
• Insertion into the stack is referred to as the “PUSH” operation.
• Deletion from the stack is referred to as the “POP” operation.
• The top most element of the stack is referred with the variable “top”
• If we want to insert an element into the stack, when the stack is already full
then that condition is called “Stack Overflow”
• If we want to delete an element from the stack when the stack is empty then
that condition is called “Stack Underflow”

Applications of Stack:
1. Expression evaluation.
2. Expression conversion.
3. Parsing
4. Backtracking

A. VASAVI
Queue:
• A Queue is a linear data structure in which all insertions are done at one
end and deletions are done at another end.
• A queue is also referred to as a FIFO list i.e., the data item which is entered
first is deleted first.
• Insertion into queue is referred to as the Enqueue operation.
• Deletion from the queue is referred to as the Dequeue operation.
• Elements are inserted at one end called the “rear” end.
• Elements are deleted from another end called “front” end.

Applications of Queue:
Queues make it useful in following kinds of scenarios:
1. When a resource is shared among multiple consumers, example include
CPU scheduling.
2. Operating systems often maintain a queue of processes that are ready to
execute or that are waiting for a particular event to occur.
3. Printer queues: In printing systems, queues are used to manage the order
in which print jobs are processed. Jobs are added to the queue as they are
submitted, and the printer processes them in the order they were received.

A. VASAVI
Linked List:
• A linked list is a linear data structure which can store a collection of
"nodes" connected via links i.e. pointers.
• A node consists of the data part and next part.
• Data part stores information and next part stores address of the next node.
• A linked list is a dynamic linear data structure whose memory size can be
allocated or de-allocated at run time based on the operation insertion or
deletion, this helps in using system memory efficiently.

struct node
{
int data;
struct node *next;
}

Types of Linked Lists:


1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
Non-Linear Data Structures:
• In the non-linear data structure, the elements are stored in a non-linear or
non-sequential manner.

A. VASAVI
• Data elements of the non-linear data structure could be connected to more
than one element to reflect a special relationship among them.
Tree Data Structure:
• The tree is a non-linear data structure that is comprised of various nodes.
• The nodes in the tree data structure are arranged in hierarchical order.
• The top node is the “root,” and the descendants of this node are “children.”
• Nodes with no children are “leaf nodes”
• Trees are used in various applications, including hierarchical data
representation, databases, and searching algorithms.
• The tree itself is a very broad data structure and is divided into various
categories like Binary tree, Binary search tree, AVL trees, Heap, max
Heap, min-heap, etc.

A. VASAVI
Graph Data Structure:
• Graphs are fundamental types of nonlinear data structures.
• A graph is a collection of nodes connected by edges.
• Graphs are often chosen to represent networks of information, such as social
media relationships or geographical maps.

A. VASAVI
Operations on Data Structures
There are different types of operations that can be performed for the
manipulation of data in every data structure which are listed below:
1. Traversing
2. Searching
3. Insertion
4. Deletion
5. Sorting
6. Merging

1. Traversing:
• A data structure's elements can be visited by traversing through it.
• Traversing a Data Structure means to visit the element stored in it.
• It visits data in a systematic manner.
• This can be done with any type of DS.
Example:
//Write a Program which performs traversing operation.
#include <stdio.h>
void main()
{
int A[] = {3,5,7,9,11};
int i, n = 5;
printf("The array elements are:\n");
for(i = 0; i < n; i++)
{
printf("A[%d] = %d \n", i, A[i]);
}
}

A. VASAVI
2. Searching:
• It is finding the location of data within the data structure that satisfies the
searching condition or the criteria.
• Searching is locating a specific element within a given data structure.
• When the necessary element is located, the attempt becomes successful.
• We may execute searches on various data structures, including arrays,
linked lists, trees, graphs, etc.
Example:
#include <stdio.h>
int linearSearch(int arr[], int size, int element)
{
for(int i = 0; i < size; i++)
{
if(arr[i] == element)
{
return i;
}
}
return -1;
}
int main()
{
int arr[] = {10, 45, 32, 98, 67};
int size = sizeof(arr) / sizeof(arr[0]);
int element = 32;
int index = linearSearch(arr, size, element);
if(index != -1)
{
printf("Element found at index: %d", index);
}
else
{
printf("Element not found");
}
return 0;
}

A. VASAVI
3. Insertion:
• Adding new data in the data structure is referred to as insertion.
• When the required element is added to the required data structure, the
insertion operation is successful.
• In other instances, it fails because there is no more space to add any more
elements to the data structure because it is already full.
Example:
// C Program to insert an element at the beginning of an array
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[100], size, elem;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the Elements of the array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to be inserted: ");
scanf("%d", &elem);
// Shift array elements the right side by one place
for (int i = size; i > 0; i--)
{
arr[i] = arr[i - 1];
}
// Insert element at the beginning
arr[0] = elem;
size = size + 1; // Increment size by 1
printf("Array after inserting element: ");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}

A. VASAVI
4. Deletion:
• Deletion deletes an element from the given data structure.
• The operation of deletion is successful when the required element is deleted
from the data structure.
Example:
// Program to delete an element from an array
void main()
{
int i, size, pos;
int a[] = {2, 4, 6, 8, 12};
size = sizeof(a)/sizeof(a[0]);
printf("The array elements before deletion operation:\n");
for(i = 0; i < size; i++)
printf("a[%d] = %d\n", i, a[i]);
printf("\nEnter the position from where you wish to delete the
element: ");
scanf("%d", &pos);
printf("\nThe array elements after deletion operation are:\n");
for(i = pos - 1; i < size; i++)
a[i] = a[i+1];
size = size - 1;
for(i = 0; i < size; i++)
printf("a[%d] = %d\n", i, a[i]);
}

A. VASAVI
Abstract Datatype (ADT)
Abstraction:
The process of providing only the essential details and hiding the internal
details is known as abstraction .
ADT:
• An Abstract Data Type (ADT) is a programming concept that defines a
high-level view of a data structure, without specifying the implementation
details.
• It is a blueprint for creating a data structure that defines the behaviour and
interface of the structure, without specifying how it is implemented.
• Examples of abstract data type in data structures are List, Stack, Queue,
etc.

List ADT:
• A list is a finite, ordered sequence of data items known as elements.
• All elements of the list are usually assumed to have the same data type.
• For example, the list ADT can be used for lists of integers, lists of
characters…
• A list is said to be empty when it contains no elements.
• The number of elements currently stored is called the length of the list.

A. VASAVI
• The beginning of the list is called the head, the end of the list is called the
tail.

Advantages of ADT in Data Structures:

• Provides abstraction, which simplifies the complexity of the data structure


and allows users to focus on the functionality.
• Enhances program modularity by allowing the data structure
implementation to be separate from the rest of the program.
• Enables code reusability as the same data structure can be used in multiple
programs with the same interface.
• Promotes the concept of data hiding by encapsulating data and operations
into a single unit, which enhances security and control over the data.
• Supports polymorphism, which allows the same interface to be used with
different underlying data structures, providing flexibility and adaptability
to changing requirements.

A. VASAVI
Important Questions Module 1
1. Define Data Structure. Explain the use of a data structure.
2. Explain about types of data structures in brief.
3. Distinguish Static and Dynamic Data structures.
4. What kind of operations are performed on a Data structure? Explain briefly
with example.
5. Explain about ADT and its types in brief.
6. What are the advantages of ADT in data structures.
7. Distinguish linear and Non-linear data structures.
8. Explain how the data is organised inside memory in data structure.

A. VASAVI

You might also like