0% found this document useful (0 votes)
81 views28 pages

Dsa PDF

This document is a laboratory file submitted for a Data Structures using C lab at Ambalika Institute of Management and Technology in Lucknow, India. It contains 16 experiments implementing various data structures and algorithms using C language, including sorting, searching, stacks, queues, trees, graphs, and their related operations. The file includes the student's details, implementations of the experiments through programs and their outputs, and is signed and submitted to the instructor for grading.

Uploaded by

Rajesh kumar
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)
81 views28 pages

Dsa PDF

This document is a laboratory file submitted for a Data Structures using C lab at Ambalika Institute of Management and Technology in Lucknow, India. It contains 16 experiments implementing various data structures and algorithms using C language, including sorting, searching, stacks, queues, trees, graphs, and their related operations. The file includes the student's details, implementations of the experiments through programs and their outputs, and is signed and submitted to the instructor for grading.

Uploaded by

Rajesh kumar
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/ 28

A

LABORATORY FILE

ON
Data Structure using C Lab(KCS-351)
Lab(KCS
SUBMITTED FOR
B.TECH
IN
COMPUTER SCIENCE AND ENGINEERING

At

Ambalika Institute of Management and Technology, Lucknow


Department of Computer Science & Engineering

DR. A.P.J KALAM TECHNICAL UNIVERSITY LUCKNOW

SESSION – 2022-23

Submitted To: Submitted By: Azhar Ali


Mr.Shivam Srivastava
Asst. Professor CSE CSE-II YEAR (3rd Sem)
AIMT LUCKNOW Roll No.- 2103630100059
INDEX
S.No. NAME OF LAB EXPERIMENT DATE SIGN REMARK

1 Sorting Algorithms-Non-Recursive.

2 Sorting Algorithms-Recursive.

3 Searching Algorithm.

4 Implementation of Stack using Array.

5 Implementation of Queue using Array.

6 Implementation of Circular Queue using


Array.
7 Implementation of Stack using Linked List.

8 Implementation of Queue using Linked List.

9 Implementation of Circular Queue using


Linked List.
10 Implementation of Tree Structures, Binary
Tree.
11 Implementation of Tree Traversal.

12 Implementation of Binary Search Tree.

13 Implementation of Insertion and Deletion in


BST.
14 Graph Implementation, BFS,

15 Graph Implementation, DFS, Minimum cost


spanning tree,
16 Graph Implementation shortest path
algorithm.
PRACTICAL-1
Sorting Algorithms-Non-Recursive. : BUBBLE SORT

// C program for implementation of Bubble sort

#include <stdio.h>

void swap(int* xp, int* yp){

int temp = *xp;

*xp = *yp;

*yp = temp;

// A function to implement bubble sort

void bubbleSort(int arr[], int n){

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

// Last i elements are already in place

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

if (arr[j] > arr[j + 1])

swap(&arr[j], &arr[j + 1]);

/* Function to print an array */

void printArray(int arr[], int size){

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

printf("%d ", arr[i]);

printf("\n");

// Driver program to test above functions

int main(){

int arr[] = { 5, 1, 4, 2, 8 };

int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

OUTPUT:

Sorted array: 1 2 4 5 8
PRACTICAL-2
Sorting Algorithms--Recursive. : BUBBLE SORT

// C program for recursive implementation


// of Bubble sort
#include <stdio.h>

// Swap function
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// A function to implement bubble sort


void bubbleSort(int arr[], int n)
{
// Base case
if (n == 1)
return;

int count = 0;
// One pass of bubble sort. After
// this pass, the largest element
// is moved (or bubbled) to end.
for (int i=0; i<n-1; i++)
if (arr[i] > arr[i+1]){
swap(&arr[i], &arr[i+1]);
count++;
}

// Check if any recursion happens or not


// If any recursion is not happen then return
if (count==0)
return;
// Largest element is fixed,
// recur for remaining array
bubbleSort(arr, n-1);
}

/* Function to print an array */


void printArray(int arr[], int n)
{
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program to test above functions


int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array : \n");
printArray(arr, n);
return 0;
}

OUTPUT:
Sorted array :
11 12 22 25 34 64 90
PRACTICAL-3
Searching Algorithm : LINEAR SEARCHING

// C code to linearly search x in arr[]. If x


// is present then return its location, otherwise
// return -1

#include <stdio.h>

int search(int arr[], int N, int x)


{
int i;
for (i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}

// Driver's code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10; //Element To be searched in array ->
int N = sizeof(arr) / sizeof(arr[0]);

// Function call
int result = search(arr, N, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}

OUTPUT :
Element is present at index 3
PRACTICAL-4
Implementation of Stack using Array.

#include<stdio.h>
#include<conio.h>

#define SIZE 10

void push(int);
void pop();
void display();

int stack[SIZE], top = -1;

void main()
{
int value, choice;
// clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return ;
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value){
if(top == SIZE-1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop(){
if(top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display(){
if(top == -1)
printf("\nStack is Empty!!!");
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}

OUTPUT :

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 25
Insertion success!!!

Enter your choice: 1


Enter the value to be insert: 20
Insertion success!!!

Enter your choice: 3


Stack elements are:
20
25

Enter your choice: 2


Deleted : 20

Enter your choice: 3


Stack elements are:
25
PRACTICAL-5
Implementation of Queue using Array.

#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x){
printf("\n Queue is Fullsss");
return 0;
}
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
return 0;
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}

OUTPUT
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit

Enter the Choice:1

Enter no 1:25

Enter the Choice:1

Enter no 2:35

Enter the Choice:3

Queue Elements are:


25
35

Enter the Choice:2

Deleted Element is 25
Enter the Choice:3

Queue Elements are:


35
PRACTICAL-6
Implementation of Circular Queue using Array.

#include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item){
if((front == 0 && rear == MAX-1) || (front == rear+1)){
printf("Queue Overflow ");
return;
}
if(front == -1){
front = 0;
rear = 0;
}
else{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion(){
if(front == -1){
printf("Queue Underflow \n");
return;
}
printf("Element deleted from queue is : %d",cqueue_arr[front]);
printf("\n");
if(front == rear){
front = -1;
rear=-1;
}
else{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}

void display(){
int front_pos = front,rear_pos = rear;
if(front == -1){
printf("Queue is empty\n");
return;
}
printf("Queue elements :");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos){
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else{
while(front_pos <= MAX-1){
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos){
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}

int main(){
int choice,item;
do{
printf("1.Insert \n");
printf("2.Delete \n");
printf("3.Display \n");
printf("4.Quit \n");
printf("Enter your choice :");
scanf("%d",&choice);
switch(choice){
case 1 :
printf("Input the element for insertion in queue : ");
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
return 0;
default:
break;
}
}
while(choice!=4);
return 0;
}
1.Insert
2.Delete
3.Display
4.Quit

Enter your choice :1


Input the element for insertion in queue : 25

Enter your choice :1


Input the element for insertion in queue : 35

Enter your choice :3


Queue elements :25 35

Enter your choice :1


Input the element for insertion in queue : 15

Enter your choice :3


Queue elements :25 35 15

Enter your choice :2


Element deleted from queue is : 25
PRACTICAL-7
Implementation of Stack using Linked List.

#include <stdlib.h>

// Structure to create a node with data and the next pointer


struct Node {
int data;
struct Node *next;
};
Node* top = NULL;

// Push() operation on a stack


void push(int value) {
struct Node *newNode;
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value; // assign value to the node
if (top == NULL) {
newNode->next = NULL;
} else {
newNode->next = top; // Make the node as top
}
top = newNode; // top always points to the newly created node
printf("Node is Inserted\n\n");
}

int pop() {
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
struct Node *temp = top;
int temp_data = top->data;
top = top->next;
free(temp);
return temp_data;
}
}
void display() {
// Display the elements of the stack
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
printf("The stack is \n");
struct Node *temp = top;
while (temp->next != NULL) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL\n\n", temp->data);
}
}

int main() {
int choice, value;
printf("\nImplementation of Stack using Linked List\n");
while (1) {
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped element is :%d\n", pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}

Implementation of Stack using Linked List


1. Push
2. Pop
3. Display
4. Exit

Enter your choice : 1

Enter the value to insert: 12


Node is Inserted

Enter your choice : 1


Enter the value to insert: 45
Node is Inserted

Enter your choice : 1


Enter the value to insert: 56
Node is Inserted

Enter your choice : 3


The stack is
56--->45--->12--->NULL

Enter your choice : 2


Popped element is :56

Enter your choice : 2


Popped element is :45

Enter your choice : 3


The stack is
12--->NULL
PRACTICAL-8
Implementation of Queue using Linked List.

#include < stdio.h >


#include < stdlib.h >

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

struct node * front;


struct node * rear;

void insert(struct node * ptr, int item) {

ptr = (struct node * ) malloc(sizeof(struct node));


if (ptr == NULL) {
printf("\nOVERFLOW\n");
return;
} else {
ptr - > data = item;
if (front == NULL) {
front = ptr;
rear = ptr;
front - > next = NULL;
rear - > next = NULL;
} else {
rear - > next = ptr;
rear = ptr;
rear - > next = NULL;
}
}
}

void deleteNode(struct node * ptr) {


if (front == NULL) {
printf("Underflow");
return;
} else {
ptr = front;
front = front - > next;
free(ptr);
}
}

int main() {
struct node * head = NULL;
insert(head, 10);
insert(head, 20);

printf("front element: %d\n", front - > data);


deleteNode(head);
printf("front element: %d", front - > data);
return 0;
}

// Output -----
Implementation of Queue using Linked List
1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice: 1


Enter the value to insert: 12
Node is Inserted

Enter your choice: 1


Enter the value to insert: 45
Node is Inserted

Enter your choice: 1


Enter the value to insert: 56
Node is Inserted
4.Exit
Enter your choice : 3
The queue is
12--->45--->56--->NULL
Dequeue Operation:
The queue is
12--->45--->56--->NULL

Enter your choice: 2


Popped element is:12

Enter your choice: 2


Popped element is:45

Enter your choice : 3


The queue is
56--->NULL

Enter your choice: 2


Popped element is:56
PRACTICAL-9
Implementation of Circular Queue using Linked List.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d) //Insert elements in Queue
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL))
{
f = r = n;
r->next = f;
}
else
{
r->next = n;
r = n;
n->next = f;
}
}
void dequeue() // Delete an element from Queue
{
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else if(f == r){
f = r = NULL;
free(t);
}
else{
f = f->next;
r->next = f;
free(t);
}

}
void print(){ // Print the elements of Queue
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else{
do{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
}
}
int main()
{
int opt,n,i,data;
printf("Enter Your Choice:-");
do{
printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in
Queue \n3 for Delete the data from the Queue\n0 for Exit");
printf("\nEnter your choice : ");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nEnter the number of data");
scanf("%d",&n);
printf("\nEnter your data");
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 0:
break;
default:
printf("\nIncorrect Choice");

}
}while(opt!=0);
return 0;
}
// Output -------
Enter the number of data 5

Enter your data


34
33
22
76
32

1 for Insert the Data in Queue


2 for show the Data in Queue
3 for Delete the data from the Queue
0 for Exit
Enter your choice : 3
Enter your choice : 2

33
22
76
32
PRACTICAL-10
Implementation of Binary Tree.

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

struct node {
int item;
struct node* left;
struct node* right;
};

// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->item);
inorderTraversal(root->right);
}

// Preorder traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Postorder traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->item);
}

// Create a new Node


struct node* create(int value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

// Insert on the left of the node


struct node* insertLeft(struct node* root, int value) {
root->left = create(value);
return root->left;
}

// Insert on the right of the node


struct node* insertRight(struct node* root, int value) {
root->right = create(value);
return root->right;
}

int main() {
struct node* root = create(1);
insertLeft(root, 4);
insertRight(root, 6);
insertLeft(root->left, 42);
insertRight(root->left, 3);
insertLeft(root->right, 2);
insertRight(root->right, 33);

printf("Traversal of the inserted binary tree \n");


printf("Inorder traversal \n");
inorderTraversal(root);

printf("\nPreorder traversal \n");


preorderTraversal(root);

printf("\nPostorder traversal \n");


postorderTraversal(root);

}
AMBALIKA INSTITUTE OF MANAGEMENT & TECHNOLOGY, LUCKNOW

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


I. Institute Vision
To nourish the students, blossom them into tomorrow’s world class professionals and good human beings by
inculcating the qualities of sincerity, integrity and social ethics.

II. Institute Mission


 To provide the finest infra structure and excellent environment for the academic growth of the
students to bridge the gap between academia and the demand of industry.

 To expose students in various co- curricular activities to convert them into skilled professionals.

 To grind very enthusiastic engineering and management student to transform him into hard working,
committed, having a zeal to excel, keeping the values of devotion, concern and honesty.

 To involve the students in extracurricular activities to make them responsible citizens.

III. CSE Department Vision

To embrace students towards becoming computer professionals having problem solving skills, leadership
qualities, foster research & innovative ideas inculcating moral values and social concerns

IV.CSE Department Mission


 To provide state of art facilities for high quality academic practices.
 To focus advancement of quality & impact of research for the betterment of society.
 To nurture extra-curricular skills and ethical values in students to meet the challenges of building a
strong nation.

V. CSE Department PEOs

PEO1:- All the graduates will become high class software professionals who could be absorbed in the
software industry on the basis of sound academic and technical knowledge gained by them on account of
adopting state of the art academic practices.

PEO2:-All the graduates will demonstrate their talent in research and development activities involving
themselves in such researches which could alleviate the existing problem of the society.

PEO3:-All the graduates shall be committed for high moral and ethical standards in solving the societal
problems by means of their exposure to various co-curricular and extra-curricular activities.
VI. Program Outcomes (POs)

PO 1 Engineering Knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and an
engineering specialization to the solution of complex engineering problems.
PO 2 Problem Analysis: Identify, formulate, review research literature, and analyze complex engineering problems
reaching substantiated conclusions using first principles of mathematics, natural sciences, and engineering sciences.
PO 3 Design/development of solutions: Design solutions for complex engineering problems and design system
components or processes that meet the specified needs with appropriate consideration for the public health and safety,
and the cultural, societal, and environmental considerations.
PO 4 Conduct investigations of complex problems: Use research-based knowledge and research methods including
design of experiments, analysis and interpretation of data, and synthesis of the information to provide valid conclusions
PO 5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern engineering and IT
tools including prediction and modeling to complex engineering activities with an understanding of the limitations.
PO 6 The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal, health,
safety, legal and cultural issues and the consequent responsibilities relevant to the professional engineering practice.
PO 7 Environment and sustainability: Understand the impact of the professional engineering solutions in societal
and environmental contexts, and demonstrate the knowledge of, and need for sustainable development.
PO 8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice
PO 9 Individual and team work: Function effectively as an individual, and as a member or leader in diverse teams,
and in multidisciplinary settings.
PO 10 Communication: Communicate effectively on complex engineering activities with the engineering community
and with society at large, such as, being able to comprehend and write effective reports and design documentation,
make effective presentations, and give and receive clear instructions.
PO 11 Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to manage projects and
in multidisciplinary environments.
PO 12 Life-long learning: Recognize the need for, and have the preparation and ability to engage in independent and
life-long learning in the broadest context of technological change.

VII.Program Specific Outcome (PSO)

To prepare students to excel in the areas of computer engineering techniques & tools through quality
education as per industry demands.
To inculcate students a multidisciplinary approach to analyze, design, apply and create innovative
products and research solutions for real life problems.
To develop professional ethics & skills in students and ability to relate computer engineering problems with
social issues

You might also like