Dsa PDF
Dsa PDF
LABORATORY FILE
ON
Data Structure using C Lab(KCS-351)
Lab(KCS
SUBMITTED FOR
B.TECH
IN
COMPUTER SCIENCE AND ENGINEERING
At
SESSION – 2022-23
1 Sorting Algorithms-Non-Recursive.
2 Sorting Algorithms-Recursive.
3 Searching Algorithm.
#include <stdio.h>
*xp = *yp;
*yp = temp;
printf("\n");
int main(){
int arr[] = { 5, 1, 4, 2, 8 };
bubbleSort(arr, n);
printArray(arr, n);
return 0;
OUTPUT:
Sorted array: 1 2 4 5 8
PRACTICAL-2
Sorting Algorithms--Recursive. : BUBBLE SORT
// Swap function
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
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++;
}
OUTPUT:
Sorted array :
11 12 22 25 34 64 90
PRACTICAL-3
Searching Algorithm : LINEAR SEARCHING
#include <stdio.h>
// 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();
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 :
#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 no 1:25
Enter no 2:35
Deleted Element is 25
Enter the Choice:3
#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
#include <stdlib.h>
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");
}
}
}
struct node {
int data;
struct node * next;
};
int main() {
struct node * head = NULL;
insert(head, 10);
insert(head, 20);
// Output -----
Implementation of Queue using Linked List
1. Enqueue
2. Dequeue
3. Display
4. Exit
#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
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);
}
return newNode;
}
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);
}
AMBALIKA INSTITUTE OF MANAGEMENT & TECHNOLOGY, LUCKNOW
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 embrace students towards becoming computer professionals having problem solving skills, leadership
qualities, foster research & innovative ideas inculcating moral values and social concerns
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.
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