0% found this document useful (0 votes)
7 views16 pages

Data Structure Practical File

The document is a practical file for BCA 1st year students at Shree Sai Institute of Technology, focusing on Data Structures. It includes an introduction to data structures, their types, and various programming implementations such as array insertion, stack, queue, linear search, bubble sort, singly linked list, and recursion for factorial calculation. Each section provides theoretical background, algorithms, and sample code for practical understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Data Structure Practical File

The document is a practical file for BCA 1st year students at Shree Sai Institute of Technology, focusing on Data Structures. It includes an introduction to data structures, their types, and various programming implementations such as array insertion, stack, queue, linear search, bubble sort, singly linked list, and recursion for factorial calculation. Each section provides theoretical background, algorithms, and sample code for practical understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SHREE SAI INSTITUTE

OF TECHNOLOGY
RATLAM

BCA 1ST year


PRACTICAL FILE
DATA STRUCTURE

GUIDED BY: SUBMITTED BY:


Prof. Anchal Tiwari

Affiliated to Samrat Vikramaditya University (SVU),


Ujjain, M.P.
INDEX

Sr. No Programs
1 Introduction to Data Structures
2 Array Insertion.

3 Stack using Array.

4 Queue using Array.

5 Linear Search.

6 Bubble Sort.

7 Singly Linked List.

8 Factorial using Recursion.


Introduction to Data Structures


What is 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.
The choice of a good data structure makes it possible to perform a variety of critical operations
effectively. An efficient data structure also uses minimum memory space and execution time to
process the structure. A data structure is not only used for organising the data. It is also used for
processing, retrieving, and storing data. There are different basic and advanced types of data
structures that are used in almost every program or software system that has been developed. So
we must have good knowledge of data structures.

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.
Please refer DSA Tutorial for complete tutorial on Data Strictures with guide on every
topic, practice problems and top interview questions.
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. 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

Applications of Data Structures:


Data structures are used in various fields such as:
 Operating system
 Graphics
 Computer Design
 Blockchain
 Genetics
 Image Processing
 Simulation,
Array Insertion

Aim: To insert an element at a given position in an array.

Theory: Array is a linear data structure where elements are stored in contiguous memory locations.

Algorithm:
1. Read number of elements.
2. Input array elements.
3. Read position and value.
4. Shift elements.
5. Insert value.
6. Display updated array.

Program:
#include<stdio.h>
int main()
{
int a[50], n, i, pos, value;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("Enter position and value: ");
scanf("%d%d", &pos, &value);
for(i=n;i>=pos;i--)
a[i]=a[i-1];
a[pos-1]=value;
n++;
printf("Updated Array:\n");
for(i=0;i<n;i++)
printf("%d ", a[i]);
return 0;
}

Result: Element inserted successfully.


Stack using Array

Aim: To implement stack using array.

Theory: Stack follows LIFO (Last In First Out) principle.

Program:
#include<stdio.h>
#define MAX 5
int stack[MAX], top=-1;

void push(int value)


{
if(top==MAX-1)
printf("Stack Overflow\n");
else
stack[++top]=value;
}

void pop()
{
if(top==-1)
printf("Stack Underflow\n");
else
printf("Deleted element: %d\n", stack[top--]);
}

int main()
{
push(10);
push(20);
pop();
return 0;
}

Result: Stack operations performed successfully.


Queue using Array

Aim: To implement queue using array.

Theory: Queue follows FIFO (First In First Out) principle.

Program:
#include<stdio.h>
#define MAX 5
int queue[MAX], front=-1, rear=-1;

void enqueue(int value)


{
if(rear==MAX-1)
printf("Queue Overflow\n");
else
{
if(front==-1) front=0;
queue[++rear]=value;
}
}

void dequeue()
{
if(front==-1 || front>rear)
printf("Queue Underflow\n");
else
printf("Deleted: %d\n", queue[front++]);
}

int main()
{
enqueue(5);
enqueue(15);
dequeue();
return 0;
}

Result: Queue operations performed successfully.


Linear Search

Aim: To search an element using Linear Search.

Theory: Linear search checks each element sequentially.

Program:
#include<stdio.h>
int main()
{
int a[10], n, i, key, found=0;
printf("Enter size: ");
scanf("%d",&n);
printf("Enter elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter element to search: ");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
found=1;
break;
}
}
if(found)
printf("Element found at position %d", i+1);
else
printf("Element not found");
return 0;
}

Result: Element searched successfully.


Bubble Sort

Aim: To sort elements using Bubble Sort.

Theory: Bubble sort swaps adjacent elements if they are in wrong order.

Program:
#include<stdio.h>
int main()
{
int a[10], n, i, j, temp;
printf("Enter size: ");
scanf("%d",&n);
printf("Enter elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("Sorted Array:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}

Result: Array sorted successfully.


Singly Linked List

Aim: To create and display singly linked list.

Theory: Linked list is a dynamic data structure using pointers.

Program:
#include<stdio.h>
#include<stdlib.h>

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

int main()
{
struct node *head=NULL, *newnode, *temp;
int choice=1;
while(choice)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
head=temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
}
printf("Add more? (0/1): ");
scanf("%d",&choice);
}
temp=head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
return 0;
}

Result: Linked list created successfully.


Factorial using Recursion

Aim: To find factorial using recursion.

Theory: Recursion is a function calling itself until base condition is met.

Program:
#include<stdio.h>

int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}

int main()
{
int num;
printf("Enter number: ");
scanf("%d",&num);
printf("Factorial = %d", fact(num));
return 0;
}

Result: Factorial calculated successfully.

You might also like