DSA Lab Fil
DSA Lab Fil
SCHOOL OF TECHNOLOGY
SESSION 2024-25
SUBMITTED BY
DIVISION : 7
****************
INDEX
10
Theory:
Data Type
Data type is a way to classify various types of data such as integer, string, etc. which
determines the values that can be used with the corresponding type of data, the type of
operations that can be performed on the corresponding type of data. There are two data types
Those data types for which a language has built-in support are known as Built-in Data types.
For example, most of the languages provide the following built-in data types.
• Integers
• Boolean (true, false)
• Floating (Decimal numbers)
• Character and Strings
Those data types which are implementation independent as they can be implemented in one
or the other way are known as derived data types. These data types are normally built by the
combination of primary or built-in data types and associated operations on them. For example
−
• List
• Array
• Stack
• Queue
Basic Operations
The data in the data structures are processed by certain operations. The particular data
structure chosen largely depends on the frequency of the operation that needs to be performed
on the data structure.
• Traversing
• Searching
• Insertion
• Deletion
• Sorting
• Merging
EXP 1: ARRAY
An array is a type of linear data structure that is defined as a collection of elements with same
or different data types. They exist in both single dimension and multiple dimensions. These
data structures come into picture when there is a necessity to store multiple elements of
similar nature together at one place.
Syntaxe:
Implementation:
Output:
EXP 2: SWAPING NUMBER USING POINTER
Theory: Swapping the values of two numbers is a very common operation in programming,
which is often used to understand the basics of variables, pointers, and function calls. In this
article, we will learn how to swap two numbers using pointers in C.
Example:
Input :
int x=10 ;
int y=20 ;
Output :
int x=20 ;
int y=10 ;
Implementation:
Output:
Theory: A linked list is a way to store a collection of elements. Like an array these can be
character or integers. Each element in a linked list is stored in the form of a node.
struct LinkedList{
int data;
};
Creating a Node:
typedef struct LinkedList *node; //Define node as pointer of data type struct LinkedList
node createNode(){
sizeof() is used to determine size in bytes of an element in C. Here it is used to determine size
of each node and sent as a parameter to malloc.
temp = createNode();//createNode will return a new node with data = value and next
pointing to NULL.
if(head == NULL){
else{
p = head;//assign head to p
while(p->next != NULL){
p = p->next;//traverse the list until p is the last node.The last node always points to
NULL.
p->next = temp;//Point the previous last node to the new node created.
return head;
The linked list can be traversed in a while loop by using the head node as a starting
reference:
node p;
p = head;
while(p != NULL){
p = p->next;
Implementation:
Output:
Theory:
Stack is a linear data structure which follows LIFO principle. In this article, we will learn
how to implement Stack using Arrays. In Array-based approach, all stack-related operations
are executed using arrays. Let’s see how we can implement each operation on the stack
utilizing the Array Data Structure.
To implement a stack using an array, initialize an array and treat its end as the stack’s top.
Implement push (add to end), pop (remove from end), and peek (check end) operations,
handling cases for an empty or full stack.
Output
Declaration:
struct Node {
type data;
Node* next;
}
Implementation:
Stack is generally implemented using an array but the limitation of this kind of stack is that
the memory occupied by the array is fixed no matter what are the number of elements in the
stack. In the stack implemented using linked list implementation, the size occupied by the
linked list will be equal to the number of elements in the stack. Moreover, its size is dynamic.
It means that the size is gonna change automatically according to the elements present.
Output:
EXP 6: QUEUE WITH ARRAY
Theory: A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle
which means the elements added first in a queue will be removed first from the queue.
Declaration:
struct Queue {
int queue[MAX_SIZE];
int front;
int rear;
};
Implementation:
peek() or front()- Acquires the data element available at the front node of the
queue without deleting it.
size(): This operation returns the size of the queue i.e. the total number of
elements it
Output:
Implementation:
if (isFull(queue))
return;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
//function to Dequeue
void queueDequeue()
// If queue is empty
if (front == rear) {
printf("\nQueue is empty\n");
return;
else {
// decrement rear
rear--;
return;
if (isempty(queue))
return INT_MIN;
return queue->arr[queue->front];
}
Output: