0% found this document useful (0 votes)
28 views19 pages

DSA Lab Fil

DSA programing

Uploaded by

alainfabo21
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)
28 views19 pages

DSA Lab Fil

DSA programing

Uploaded by

alainfabo21
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
You are on page 1/ 19

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

SCHOOL OF TECHNOLOGY

PANDIT DEENDAYAL ENERGY UNIVERSITY

SESSION 2024-25

SUBMITTED BY

NAME : Kouadio Akotto Daniel

ROLL NO. : 23BCP471

DIVISION : 7

COURSE NAME : DATA STRUCTURE LAB

COURSE CODE : 20CP201P

Department of Computer Science and Engineering

Pandit Deendayal Energy University

****************
INDEX

Exp. Title of Lab Work Date of Page Signature


No. Lab
Work
1

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

• Built-in Data Type


• Derived Data Type

Built-in Data Type

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

Derived Data Type

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

Aim: How to implement Array

THEORY: What is an 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:

data_type array_name[array_size]={elements separated by commas} or, data_type


array_name[array_size];

Implementation:
Output:
EXP 2: SWAPING NUMBER USING POINTER

Aim: To swaping numbers 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:

EXP 3: LIKED LIST

Aim: perform linked list in c

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.

Declaring a Linked list :

In C language, a linked list can be implemented using structure and pointers .

struct LinkedList{

int data;

struct LinkedList *next;

};

Creating a Node:

typedef struct LinkedList *node; //Define node as pointer of data type struct LinkedList

node createNode(){

node temp; // declare a node

temp = (node)malloc(sizeof(struct LinkedList)); // allocate memory using malloc()


temp->next = NULL;// make next point to NULL

return temp;//return the new node

typedef is used to define a data type in C.

malloc() is used to dynamically allocate a single block of memory in C, it is available in the


header file stdlib.h.

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.

add a node to the linked list:

node addNode(node head, int value){

node temp,p;// declare two nodes temp and p

temp = createNode();//createNode will return a new node with data = value and next
pointing to NULL.

temp->data = value; // add element's value to data part of node

if(head == NULL){

head = temp; //when linked list is empty

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;

Traversing the list:

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:

EXP 4: STACK WITH ARRAY

AIM: perform Stack with array

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.

Implement Stack using Array:

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

EXP 5: STACK WITH LINKED LIST

Aim: Perform stack with linked list

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

Aim: Perform queue using 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:

enqueue() – Insertion of elements to 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.

size(): This operation returns the size of the queue i.e. the total number of
elements it
Output:

EXP 7: QUEUE WITH LINKED LIST

Aim: Perform Queue with linked list

Implementation:

// Function to add an item to the queue.

void enqueue(struct Queue* queue, int item)

if (isFull(queue))

return;

queue->rear = (queue->rear + 1) % queue->capacity;

queue->array[queue->rear] = item;

queue->size = queue->size + 1;

printf("%d enqueued to queue\n", item);

//function to Dequeue

void queueDequeue()

// If queue is empty

if (front == rear) {
printf("\nQueue is empty\n");

return;

// Shift all the elements from index 2

// till rear to the left by one

else {

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

queue[i] = queue[i + 1];

// decrement rear

rear--;

return;

// Function to get front of queue

int front(struct Queue* queue)

if (isempty(queue))

return INT_MIN;

return queue->arr[queue->front];

}
Output:

You might also like