Nikhil Uu

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

S.No: 1 Exp. Name: All operations on Singly linked list.

Date: 2023-03-20

Aim:

Page No: 1
Write a program that uses functions to perform the following operations on singly linked list
i)Creationii)insertioniii)deletioniv) Traversal

Source Code:

ID: 21AG1A6666
singlelinkedlistalloperations.c

2021-2025-CSM-B
ACE Engineering College
#include<stdio.h>
struct node
{
int data;
struct node *next;

Page No: 2
};
//int d;
struct node *head=NULL;
//Insertion module
void insertion(int d)

ID: 21AG1A6666
{
struct node *p,*q;
p=(struct node*)malloc(sizeof(struct node));
p->data=d;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
q=head;

2021-2025-CSM-B
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
}
}
//Deletion module

ACE Engineering College


void deletion(int pos)
{
int c = 1;
struct node *p,*q;
p=head;
q=head;
if(head==NULL)
{
printf("Empty\n");
return;
}
else
{
q=head;
while(c<pos)
{
p=q;
q=q->next;
c++;
}
p->next=q->next;
free(q);
printf("Deleted successfully\n");
}
}
{
struct node *q=head;
if (q==NULL)
{
printf("Empty");

Page No: 3
return;
}
else
{
q=head;

ID: 21AG1A6666
printf("The elements in the linked list are : ");
//for(q=head;q->next!=NULL;q++)
while(q!=NULL)
{
printf("%d ",q->data);
q=q->next;
}
}
printf("\n");

}
//Counter module

2021-2025-CSM-B
void count()
{
int s=0;
struct node *w;
if (head==NULL)
{
printf("Empty\n");
return;

ACE Engineering College


}
else
{
w=head;
while(w!=NULL)
{
w=w->next;
s++;
}
printf("No of elements in the linked list are : %d\n",s);
}
}
int main()
{
int op,d,pos;
printf("Singly Linked List Example - All Operations\n");
while(1)
{
//printf("Singly Linked List Example - All Operations");
printf("Options\n");
printf("1 : Insert elements into the linked list\n");
printf("2 : Delete elements from the linked list\n");
printf("3 : Display the elements in the linked list\n");
printf("4 : Count the elements in the linked list\n");
printf("5 : Exit()\n");
switch(op)
{
case 1:

printf("Enter elements for inserting into linked list : ");


scanf("%d",&d);

Page No: 4
insertion(d);
break;

case 2:

ID: 21AG1A6666
printf("Enter position of the element for deleteing the element :
");
scanf("%d",&pos);
deletion(pos);
break;

case 3:display();
break;

case 4:count();
break;

2021-2025-CSM-B
case 5:exit(0);
default:printf("Enter options from 1 to 5\n");
return;

}
}
}

ACE Engineering College


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Singly Linked List Example - All Operations
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :
1
Enter elements for inserting into linked list :
100
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :
1
Enter elements for inserting into linked list :

200
Options

Page No: 5
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list

ID: 21AG1A6666
5 : Exit()
Enter your option :
1
Enter elements for inserting into linked list :

300
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()

2021-2025-CSM-B
Enter your option :
1
Enter elements for inserting into linked list :

400
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list

ACE Engineering College


3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :
1
Enter elements for inserting into linked list :

500
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :
2
Enter position of the element for deleteing the element :

3
Deleted successfully
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :

3
The elements in the linked list are : 100 200 400 500
Options

Page No: 6
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list

ID: 21AG1A6666
5 : Exit()
Enter your option :

4
No of elements in the linked list are : 4
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :

2021-2025-CSM-B
Test Case - 2

User Output
Singly Linked List Example - All Operations

ACE Engineering College


Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :

1
Enter elements for inserting into linked list :
500
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :

10
Enter options from 1 to 5

Test Case - 3

User Output
Singly Linked List Example - All Operations
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list

Page No: 7
4 : Count the elements in the linked list
5 : Exit()
Enter your option :
1

ID: 21AG1A6666
Enter elements for inserting into linked list :
54
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :
1
Enter elements for inserting into linked list :

2021-2025-CSM-B
68
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()

ACE Engineering College


Enter your option :
1
Enter elements for inserting into linked list :
32
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :
3
The elements in the linked list are : 54 68 32
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :
4
No of elements in the linked list are : 3
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()

Page No: 8
Enter your option :
2
Enter position of the element for deleteing the element :

ID: 21AG1A6666
Deleted successfully
Options
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :

3
The elements in the linked list are : 54 68
Options

2021-2025-CSM-B
1 : Insert elements into the linked list
2 : Delete elements from the linked list
3 : Display the elements in the linked list
4 : Count the elements in the linked list
5 : Exit()
Enter your option :

ACE Engineering College


Enter options from 1 to 5
S.No: 2 Exp. Name: All operations on Doubly linked list. Date: 2023-03-20

Aim:

Page No: 9
Write a program that uses functions to perform the following operations on double linked list
i) Creationii)insertioniii)deletioniv) Traversal

Note: In the displayed test cases, "->" indicates the Tab space, and "." indicates the space.

ID: 21AG1A6666
Source Code:

AllOperationsDLL.c

2021-2025-CSM-B
ACE Engineering College
#include<stdio.h>
#include<stdlib.h>
//defining a node structure
struct dllnode
{

Page No: 10
int data;
struct dllnode *prev, *next;
};
struct dllnode *head=NULL;
int d,x;

ID: 21AG1A6666
int s=1;
void count()
{
struct dllnode *w;
w=head;
if(head==NULL)
{
return;
}
else
{
while(w!=NULL)

2021-2025-CSM-B
{
w=w->next;
s++;
}
}
}
void insert(int d)
{

ACE Engineering College


struct dllnode *p , *q;
p=(struct dllnode *)malloc(sizeof(struct dllnode));
p->data=d;
p->next=NULL;
if(head==NULL)
{
p->prev=NULL;
head=p;
}
else
{
q=head;
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
p->prev=q;
}
}

void deleteion(int x)
{
struct dllnode *p,*q;
p=(struct dllnode *)malloc(sizeof(struct dllnode));
while(q->next!=NULL && q->data!=x)
{
p=q;
q=q->next;
}

Page No: 11
if(q->data==x)
{
p->next=q->next;
q->next->prev=p;

ID: 21AG1A6666
free(q);
return;
}
else if(q->next==NULL)
{
printf("%d not found.\n",x);
return;
}

}
void display()
{

2021-2025-CSM-B
struct dllnode *q;
if(head==NULL)
{
printf("Empty\n");
return;
}
else
{

ACE Engineering College


q=head;
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->next;
}
}
printf("\n");
}
int main()
{
head=NULL;
int ch,d,x;
while(1)
{
printf("Operations on doubly linked list\n");
printf("1. Insert \n");
printf("2.Remove\n");
printf("3. Display\n");
printf("0. Exit\n");
printf("Enter Choice 0-4? : ");
scanf("%d",&ch);
switch(ch)
{
case 0:
printf("Enter number: ");
scanf("%d",&d);
insert(d);
break;
case 2:

Page No: 12
printf("Enter number to delete: ");
scanf("%d",&x);
deleteion(x);
break;
case 3:

ID: 21AG1A6666
display();
break;
}
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

2021-2025-CSM-B
User Output
Operations on doubly linked list
1. Insert
2.Remove
3. Display

ACE Engineering College


0. Exit
Enter Choice 0-4? :
1
Enter number:

15
Operations on doubly linked list
1. Insert
2.Remove
3. Display
0. Exit
Enter Choice 0-4? :
1
Enter number:
16
Operations on doubly linked list
1. Insert
2.Remove
3. Display
0. Exit
Enter Choice 0-4? :

1
Enter number:
17
Operations on doubly linked list
1. Insert
2.Remove
3. Display
0. Exit
Enter Choice 0-4? :
1

Page No: 13
Enter number:
18
Operations on doubly linked list
1. Insert

ID: 21AG1A6666
2.Remove
3. Display
0. Exit
Enter Choice 0-4? :

3
15 16 17 18
Operations on doubly linked list
1. Insert
2.Remove
3. Display

2021-2025-CSM-B
0. Exit
Enter Choice 0-4? :
2
Enter number to delete:

19
19 not found.
Operations on doubly linked list

ACE Engineering College


1. Insert
2.Remove
3. Display
0. Exit
Enter Choice 0-4? :
3
15 16 17 18
Operations on doubly linked list
1. Insert
2.Remove
3. Display
0. Exit
Enter Choice 0-4? :

2
Enter number to delete:
16
Operations on doubly linked list
1. Insert
2.Remove
3. Display
0. Exit
Enter Choice 0-4? :
0
S.No: 3 Exp. Name: All operations on Circular linked list. Date: 2023-03-20

Aim:

Page No: 14
Write a program that uses functions to perform the following operations on circular linked list
i) Creation ii)insertion iii)deletion iv) Traversal

Source Code:

ID: 21AG1A6666
AlloperationsinCLL.c

2021-2025-CSM-B
ACE Engineering College
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;

Page No: 15
};

void insert(struct node **head,int data,int pos){


struct node *p=(struct node*)malloc(sizeof(struct node));
p->data=data;

ID: 21AG1A6666
if(*head==NULL){
*head=p;
p->next= *head;
return;
}
if(pos==1){
struct node *temp = *head;
while(temp->next!= *head){
temp=temp->next;
}
temp->next= p;
p->next = *head;

2021-2025-CSM-B
*head = p;
return;
}
struct node *temp= *head;
int i;
for(int i=0;i<pos-2;i++){
temp=temp->next;
}

ACE Engineering College


p->next=temp->next;
temp->next=p;
}
void deleteNode(struct node **head,int data)
{
if(*head==NULL)
{
printf("Element does not exit!!!\n");
return;
}
struct node *temp = *head;
if(temp->data==data)
{
if(temp->next == *head)
{
*head = NULL;
free(temp);
return;
}
while(temp->next != *head)
{
temp= temp->next;

}
temp->next = (*head)->next;
return;

}
struct node *prev = temp;
temp = temp->next;

Page No: 16
while(temp->next != *head)
{
if(temp->data == data)
{
prev->next = temp->next;

ID: 21AG1A6666
free(temp);
printf("Element deleted\n");
return;
}
prev = temp;
temp = temp->next;
}
if(temp->data == data)
{
prev->next = temp->next;
free(temp);
printf("Element deleted\n");

2021-2025-CSM-B
return;

}
printf("Element does not exist!!!\n");
}
void find(struct node **head, int
data){struct node *temp = *head;
do{

ACE Engineering College


if(temp->data == data){
printf("Element exist!!!\n");
return;
}
temp=temp->next;
}while(temp!=(*head));
printf("Element does not exist!!!\n");
}
void print(struct node **head){
struct node *temp = *head;
do{
printf("%d -> ",temp->data);
temp=temp->next;
}while(temp!= *head);
printf("\n");
}
int main(){
struct node *head=NULL;
int choice,data,pos;
printf("CIRCULAR LINKED LIST IMPLEMENTATION OF LIST ADT\n");
while(1){
printf("1. INSERT\t");
printf("2. DELETE\t3. FIND\t4. PRINT\t5. QUIT\n");
printf("Enter the choice :: ");
scanf("%d", &choice);
printf("Enter the element to be inserted :: ");
scanf("%d",&data);
printf("Enter the position of the element :: ");
scanf("%d",&pos);
insert(&head, data, pos);

Page No: 17
break;
case 2:
printf("Enter the element to be deleted :: ");
scanf("%d", &data);
deleteNode(&head, data);

ID: 21AG1A6666
break;
case 3:
printf("Enter the element to be searched :: ");
scanf("%d",&data);
find(&head, data);
break;
case 4:
printf("The list element are :: ");
print(&head);
break;
case 5:
exit(0);

2021-2025-CSM-B
}
}
}

Execution Results - All test cases have succeeded!

ACE Engineering College


Test Case - 1

User Output
CIRCULAR LINKED LIST IMPLEMENTATION OF LIST ADT
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::

1
Enter the element to be inserted ::
12
Enter the position of the element ::
1
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::
1
Enter the element to be inserted ::
13
Enter the position of the element ::

1
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::

1
Enter the element to be inserted ::
14
Enter the position of the element ::
2
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::
1

Page No: 18
Enter the element to be inserted ::

15
Enter the position of the element ::
3

ID: 21AG1A6666
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::

4
The list element are :: 13 -> 14 -> 15 -> 12 ->
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::

2
Enter the element to be deleted ::
14
Element deleted
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT

2021-2025-CSM-B
Enter the choice ::
4
The list element are :: 13 -> 15 -> 12 ->
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::
3

ACE Engineering College


Enter the element to be searched ::

12
Element exist!!!
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::
5

Test Case - 2

User Output
CIRCULAR LINKED LIST IMPLEMENTATION OF LIST ADT
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::
1
Enter the element to be inserted ::
54
Enter the position of the element ::

1
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::

2
Enter the element to be deleted ::
1
Element does not exist!!!
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::
4
The list element are :: 54 ->

Page No: 19
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::
1
Enter the element to be inserted ::

65

ID: 21AG1A6666
Enter the position of the element ::
2
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::

4
The list element are :: 54 -> 65 ->
1. INSERT 2. DELETE 3. FIND 4. PRINT 5. QUIT
Enter the choice ::
5

2021-2025-CSM-B
ACE Engineering College
S.No: 4 Exp. Name: Stack using Arrays Date: 2023-01-11

Aim:

Page No: 20
Write a program to implement stack(its operations) using arrays.

Sample Input and Output:


1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 4

ID: 21AG1A6666
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 2
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 3
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 5
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 1
Enter element : 25
Successfully pushed.

2021-2025-CSM-B
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 1
Enter element : 26
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 3
Elements of the stack are : 26 25
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit

ACE Engineering College


Enter your option : 2
Popped value = 26
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 4
Stack is not empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 5
Peek value = 25
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option : 6

Source Code:

StackUsingArray.c
#include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 10
#include "StackOperations.c"

Page No: 21
int main() {
int op, x;
while(1) {
printf("1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit\n");
printf("Enter your option : ");

ID: 21AG1A6666
scanf("%d", &op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d", &x);
push(x);
break;
case 2:
pop();
break;
case 3:
display();

2021-2025-CSM-B
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:

ACE Engineering College


exit(0);
default:
printf("Invalid input\n");
}
}
}

StackOperations.c
int a[10],top=-1;
void push(int x)
{
if(top==9)
{

Page No: 22
printf("stack overflow\n");
return;
}
else
{

ID: 21AG1A6666
top++;
a[top]=x;
printf("Successfully pushed\n");
}
}
void pop()
{
if(top==-1)
{
printf("Stack is underflow\n");
return;
}

2021-2025-CSM-B
else
{
printf("Popped value = %d\n",a[top]);
top--;
}
}
void display()
{

ACE Engineering College


int i;
if(top==-1)
{
printf("Stack is empty\n");
return;
}
else
{
printf("Elements of the stack are: ");
for(i=top;i>=0;i--)
printf("%d ",a[i]);
}
printf("\n");
}
void isEmpty()
{
if(top==-1)
{
printf("Stack is empty\n");
return;
}
else
{
printf("Stack is not empty\n");
}
{
if(top==-1)
{
printf("Stack is underflow\n");
return;

Page No: 23
}
else
{
printf("Peek value = %d\n",a[top]);
}

ID: 21AG1A6666
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

2021-2025-CSM-B
4
Stack is empty
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Stack is underflow
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit

ACE Engineering College


Enter your option :
2
Stack is underflow
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Stack is empty
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

Test Case - 2

User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :

10
Successfully pushed
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

1
Enter element :
20
Successfully pushed
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

Page No: 24
1
Enter element :
30
Successfully pushed

ID: 21AG1A6666
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
40
Successfully pushed
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

3
Elements of the stack are: 40 30 20 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit

2021-2025-CSM-B
Enter your option :

4
Stack is not empty
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

ACE Engineering College


Peek value = 40
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 40
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are: 30 20 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
15
Invalid input
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6
Exp. Name: Stack operations using dynamic
S.No: 5 Date: 2023-01-18
arrays(pointer).

Aim:

Page No: 25
Write a program to implement stack using dynamic arrays(pointer).

In this stack implementation has


1. a pointer 'arr' to a dynamically allocated array (used to hold the contents of the stack)

ID: 21AG1A6666
2. an integer 'maxSize' that holds the size of this array (i.e the maximum number of data that can be held in
this array)
3. an integer 'top' which stores the array index of the top element in the stack.
Sample Input and Output:
Enter the maximum size of the stack : 3
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 2
Stack is underflow.
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 3
Stack is empty.
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 1

2021-2025-CSM-B
Enter element : 11
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 1
Enter element : 12
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 1

ACE Engineering College


Enter element : 13
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 1
Enter element : 14
Stack is overflow.
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 3
Elements of the stack are : 13 12 11
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 2
Popped value = 13
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 2
Popped value = 12
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 3
Elements of the stack are : 11
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 1
Enter element : 16
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 3
Elements of the stack are : 16 11
1.Push 2.Pop 3.Display 4.Exit
Enter your option : 4

Source Code:
StackUsingDynamicArray.c

#include <stdio.h>
#include <stdlib.h>
#include "StackUsingDynamicArray1.c"

Page No: 26
int main() {
int op, x;
printf("Enter the maximum size of the stack : ");
scanf("%d", &maxSize);
initStack();

ID: 21AG1A6666
while(1) {
printf("1.Push 2.Pop 3.Display 4.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d", &x);
push(x);
break;
case 2:
pop();

2021-2025-CSM-B
break;
case 3:
display();
break;
case 4:
exit(0);
}

ACE Engineering College


}
}

StackUsingDynamicArray1.c
int maxSize;
int t=0;
struct Node
{
int data;

Page No: 27
struct Node *next;
};
typedef struct Node *NODE;
NODE top;
void initStack()

ID: 21AG1A6666
{
top=NULL;
}
void push(int d)
{
NODE p;
p=(NODE)malloc(sizeof(struct Node));
p->data=d;
if(t>=maxSize)
{
printf("Stack is overflow.\n");
return;

2021-2025-CSM-B
}
if(t<maxSize && top==NULL)
p->next=NULL;
else
p->next=top;
top=p;
t++;
printf("Successfully pushed.\n");

ACE Engineering College


}
void pop()
{
if(top==NULL)
{
printf("Stack is underflow.\n");
return;
}
else
{
NODE p;
p=top;
printf("Popped value = %d\n",p->data);
top=top->next;
t--;
free(p);
}
}
void display()
{
NODE temp = top;
if(temp == NULL)
{
printf("Stack is empty.\n");
return;
{
printf("Elements of the stack are : ");
while(temp!=NULL)
{
printf("%d ",temp -> data);

Page No: 28
temp=temp->next;
}
printf("\n");
}
}

ID: 21AG1A6666
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter the maximum size of the stack :
3
1.Push 2.Pop 3.Display 4.Exit

2021-2025-CSM-B
Enter your option :
2
Stack is underflow.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
3

ACE Engineering College


Stack is empty.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
1
Enter element :

11
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :

1
Enter element :
12
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
1
Enter element :

13
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :

1
Enter element :
14
Stack is overflow.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
3
Elements of the stack are : 13 12 11

Page No: 29
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
2
Popped value = 13
1.Push 2.Pop 3.Display 4.Exit

ID: 21AG1A6666
Enter your option :
2
Popped value = 12
1.Push 2.Pop 3.Display 4.Exit
Enter your option :

3
Elements of the stack are : 11
1.Push 2.Pop 3.Display 4.Exit
Enter your option :

2021-2025-CSM-B
Enter element :
16
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
3

ACE Engineering College


Elements of the stack are : 16 11
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
4

Test Case - 2

User Output
Enter the maximum size of the stack :

2
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
1
Enter element :

111
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
1
Enter element :
222
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
3
Elements of the stack are : 222 111
1.Push 2.Pop 3.Display 4.Exit
Enter your option :

Page No: 30
1
Enter element :
333
Stack is overflow.

ID: 21AG1A6666
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
3
Elements of the stack are : 222 111
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
2
Popped value = 222
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
2

2021-2025-CSM-B
Popped value = 111
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
2
Stack is underflow.
1.Push 2.Pop 3.Display 4.Exit

ACE Engineering College


Enter your option :

3
Stack is empty.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :

1
Enter element :
15
Successfully pushed.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
3
Elements of the stack are : 15
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
4

Test Case - 3

User Output
Enter the maximum size of the stack :

1
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
1
Enter element :

1
Successfully pushed.

Page No: 31
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
1
Enter element :
2

ID: 21AG1A6666
Stack is overflow.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
1
Enter element :

3
Stack is overflow.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
2

2021-2025-CSM-B
Popped value = 1
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
2
Stack is underflow.
1.Push 2.Pop 3.Display 4.Exit

ACE Engineering College


Enter your option :
3
Stack is empty.
1.Push 2.Pop 3.Display 4.Exit
Enter your option :
4
Exp. Name: Write a C program to implement different
S.No: 6 Date: 2023-01-18
Operations on Queue using Array representation

Aim:

Page No: 32
Write a program to implement queue(its operations) using arrays.

Sample Input and Output:


1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

ID: 21AG1A6666
Enter your option : 1
Enter element : 23
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 1
Enter element : 56
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 3
Elements in the queue : 23 56
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 4
Queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

2021-2025-CSM-B
Enter your option : 5
Queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 2
Deleted element = 23
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 2
Deleted element = 56

ACE Engineering College


1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 4
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 6

Source Code:

QueueUsingArray.c
#include <conio.h>
#include <stdio.h>
#include "QueueOperations.c"
int main() {
int op, x;

Page No: 33
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {

ID: 21AG1A6666
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:

2021-2025-CSM-B
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}

ACE Engineering College


}

QueueOperations.c
int Q[5];
int front=-1,rear=-1;
void enqueue(int d)
{
if(rear==4)

Page No: 34
{
printf("Queue is overflow.\n");
return;
}
else

ID: 21AG1A6666
{
rear++;
Q[rear]=d;
if(front==-1)
front=0;
printf("Successfully inserted.\n");
}
}
void dequeue()
{
if(front==-1)
{

2021-2025-CSM-B
printf("Queue is underflow.\n");
return;
}
else if(front==rear)
{
printf("Deleted element = %d\n",Q[front]);
front=-1;
rear=-1;

ACE Engineering College


}
else
{
int i;
printf("Deleted element = %d\n",Q[front]);
for(i=front;i<rear;i++)
Q[i]=Q[i+1];
rear--;
}
}
void display()
{
int i;
if(front==-1)
printf("Queue is empty.\n");
else
{
printf("Elements in the queue : ");
for(i=front;i<=rear;i++)
printf("%d ",Q[i]);
printf("\n");
}
}
void isEmpty()
{
else
printf("Queue is not empty.\n");
}
void size()
{

Page No: 35
if(rear==-1)
printf("Queue size : 0\n");
else
printf("Queue size : %d\n",rear+1);
}

ID: 21AG1A6666
Execution Results - All test cases have succeeded!
Test Case - 1

2021-2025-CSM-B
User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

ACE Engineering College


Enter your option :

3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

4
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
14
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :

78
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :

53
Successfully inserted.

Page No: 36
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 14 78 53

ID: 21AG1A6666
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
6

Test Case - 2

User Output

2021-2025-CSM-B
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
25
Successfully inserted.

ACE Engineering College


1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 25
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

1
Enter element :
65
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 65
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

Page No: 37
2
Deleted element = 65
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

ID: 21AG1A6666
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
63

2021-2025-CSM-B
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 1
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

ACE Engineering College


Enter your option :

6
S.No: 7 Exp. Name: Queue implementation using pointers. Date: 2023-01-25

Aim:

Page No: 38
Write a program to implement queue(its operations) using dynamic array(pointers).

In this queue implementation has


4. a pointer 'queue' to a dynamically allocated array (used to hold the contents of the queue)
5. an integer 'maxSize' that holds the size of this array (i.e the maximum number of data that can be held in

ID: 21AG1A6666
this array)
6. an integer 'front' which stores the array index of the first element in the queue
7. an integer 'rear' which stores the array index of the last element in the queue.
Sample Input and Output:
Enter the maximum size of the queue : 3
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 1

2021-2025-CSM-B
Enter element : 15
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 1
Enter element : 16
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 1
Enter element : 17

ACE Engineering College


Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 1
Enter element : 18
Queue is overflow.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 3
Elements in the queue : 15 16 17
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 2
Deleted element = 15
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 2
Deleted element = 16
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 3
Elements in the queue : 17
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 2
Deleted element = 17
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option : 4

Source Code:
QueueUsingDynamicArray.c

#include <conio.h>
#include <stdio.h>
#include "QueueUsingDynamicArray1.c"

Page No: 39
int main() {
int op, x;
printf("Enter the maximum size of the queue : ");
scanf("%d", &maxSize);
initQueue();

ID: 21AG1A6666
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();

2021-2025-CSM-B
break;
case 3:
display();
break;
case 4:
exit(0);
}

ACE Engineering College


}
}

QueueUsingDynamicArray1.c
#include<stdio.h>
#include<stdlib.h>
int maxSize;
int t=0;
struct node

Page No: 40
{
int data;
struct Node *next;
};
typedef struct node *NODE;

ID: 21AG1A6666
NODE front;
NODE rear;
void initQueue()
{
front=rear=NULL;
}
void enqueue(int d)
{
NODE p;
p=(NODE)malloc(sizeof(struct node));
p->data=d;
p->next=NULL;

2021-2025-CSM-B
if(t>=maxSize)
{
printf("Queue is overflow.\n");
return;
}
if(t<maxSize && rear==NULL)
{
front=rear=p;

ACE Engineering College


t++;
printf("Successfully inserted.\n");
}
else
{
rear->next=p;
rear=p;
t++;
printf("Successfully inserted.\n");
}
}
void dequeue()
{
if(front==NULL)
{
printf("Queue is underflow.\n");
return;
}
else if(front==rear)
{
NODE p;
p=front;
printf("Deleted element = %d\n",p->data);
front=rear=NULL;
free(p);
{
NODE p;
p=front;
printf("Deleted element = %d\n",p->data);
front=front->next;

Page No: 41
t--;
free(p);
}
}
void display()

ID: 21AG1A6666
{
NODE p;
p=front;
if(front==NULL)
{
printf("Queue is empty.\n");
return;
}
else
{
printf("Elements in the queue : ");
while(p!=NULL)

2021-2025-CSM-B
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
}

ACE Engineering College


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter the maximum size of the queue :
3
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :
2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :
3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :
1
Enter element :

15
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :

1
Enter element :
16

Page No: 42
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :
1

ID: 21AG1A6666
Enter element :

17
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :

1
Enter element :
18
Queue is overflow.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :

2021-2025-CSM-B
3
Elements in the queue : 15 16 17
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :
2
Deleted element = 15

ACE Engineering College


1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :

2
Deleted element = 16
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :

3
Elements in the queue : 17
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :

2
Deleted element = 17
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :
3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :
2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Exit
Enter your option :
4
S.No: 8 Exp. Name: Bubble Sort Date: 2023-03-20

Aim:

Page No: 43
Write a program to sort the given elements in ascending order using the Bubble sort technique.

Source Code:

BubbleSortDemo3.c

ID: 21AG1A6666
#include<stdio.h>

#include<stdio.h>
void bubblesort(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])

2021-2025-CSM-B
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return;

ACE Engineering College


}

void main()
{
int a[12],n,i;
printf("Number of elements: ");
scanf("%d",&n);
printf("Elements: ");
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubblesort(a,n);
printf("Array after implementing bubble sort: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output
Number of elements:
3
Elements:

16

Page No: 44
2
0
Array after implementing bubble sort: 0 2 16

ID: 21AG1A6666
2021-2025-CSM-B
ACE Engineering College
S.No: 9 Exp. Name: Selction Sort Date: 2023-03-20

Aim:

Page No: 45
Write a program to Sort the given elements in ascending order using Selection sort technique.

Source Code:

selectionsort.c

ID: 21AG1A6666
#include<stdio.h>
void selectionsort(int a[],int n)
{
int i,j,t;
for (i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>=a[j])
{
t=a[i];

2021-2025-CSM-B
a[i]=a[j];
a[j]=t;
}
}
}
return;
}
///

ACE Engineering College


void main()
{
int a[12],n,i;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
selectionsort(a,n);
printf("After sorting the elements in the array are\n");
for (i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter value of n :
9
2
5
3

2 5 9
After sorting the elements in the array are

ACE Engineering College 2021-2025-CSM-B ID: 21AG1A6666 Page No: 46


S.No: 10 Exp. Name: Insertion Sort Date: 2023-03-20

Aim:

Page No: 47
Write a program to Sort the given elements in ascending order using Insertion sort technique.

Source Code:

insertionsort.c

ID: 21AG1A6666
#include<stdio.h>
void insertionsort(int a[],int n)
{
int i,t,pos;
for(i=0;i<n;i++)
{
t=a[i];
pos=i;
while(pos>0&&a[pos-1]>t)
{
a[pos]=a[pos-1];

2021-2025-CSM-B
pos--;
}
a[pos]=t;
}
return;
}
void main()
{

ACE Engineering College


int a[12],n,i;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
insertionsort(a,n);
printf("After sorting the elements in the array are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter value of n :

3
2
9
6

2 6 9
After sorting the elements in the array are

ACE Engineering College 2021-2025-CSM-B ID: 21AG1A6666 Page No: 48


S.No: 11 Exp. Name: Non-recursive Linearsearch Date: 2023-03-20

Aim:

Page No: 49
Write a program that use non recursive functions to perform the Linearsearch searching operations for a Key
value in a given list of integers.

Source Code:

ID: 21AG1A6666
nonrecursive-linearsearch.c

#include<stdio.h>
int linearSearch(int a[],int n, int key)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==key)
{
return i+1;
}

2021-2025-CSM-B
}
return -1;
}
//
int main()
{
int a[100],key,i,n,position;
printf("enter n value");

ACE Engineering College


scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter search key element");
scanf("%d",&key);
position=linearSearch(a,n,key);
if(position!=-1)
{
printf("element found at %d position",position);
}
else
{
printf("element not found");
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
enter n value
3
3
6
9
enter search key element

Page No: 50
6
element found at 2 position

Test Case - 2

ID: 21AG1A6666
User Output
enter n value
3
3
6
9
enter search key element

5
element not found

2021-2025-CSM-B
ACE Engineering College
S.No: 12 Exp. Name: Recursive Linearsearch Date: 2023-03-20

Aim:

Page No: 51
Write a program that use recursive functions to perform the Linearsearch searching operations for a Key value in
a given list of integers.

Source Code:

ID: 21AG1A6666
recursive-linearsearch.c

#include<stdio.h>

#include<stdio.h>
int linearSearch(int a[],int n, int key)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==key)
{

2021-2025-CSM-B
return i+1;
}
}
return -1;
}
int main()
{
int a[100],key,i,n,position;

ACE Engineering College


printf("Enter n: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to search \n");
scanf("%d",&key);
position=linearSearch(a,n,key);
if(position!=-1)
{
printf("Element found at pos %d ",position);
}
else
{
printf("Element not found");
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter n:
3
3
6
9

Page No: 52
Enter the element to search

6
Element found at pos 2

ID: 21AG1A6666
Test Case - 2

User Output
Enter n:

3
3
6
9
Enter the element to search
2

2021-2025-CSM-B
Element not found

ACE Engineering College


S.No: 13 Exp. Name: Non-recursive Binary search Date: 2023-03-20

Aim:

Page No: 53
Write a program that use non recursive functions to perform the Binary search searching operations for a Key
value in a given list of integers.

Source Code:

ID: 21AG1A6666
nonrecursive-binarysearch.c

#include<stdio.h>
int main()
{
int c,first,last,middle,n,search,array[100];
printf("enter n value");
scanf("%d",&n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("enter the key element");
scanf("%d",&search);

2021-2025-CSM-B
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(array[middle]<search)
first=middle+1;

ACE Engineering College


else if (array[middle]==search)
{
printf("element is found at %d",middle+1);
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
printf("element is not found");
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
enter n value
3
3
6
9
enter the key element
6
element is found at 2

Page No: 54
Test Case - 2

User Output
enter n value

ID: 21AG1A6666
3
3
6
9
enter the key element

2
element is not found

2021-2025-CSM-B
ACE Engineering College
S.No: 14 Exp. Name: Recursive Binary search Date: 2023-03-20

Aim:

Page No: 55
Write a program that use recursive functions to perform the Binary search searching operations for a Key value
in a given list of integers.

Source Code:

ID: 21AG1A6666
recursive-binarysearch.c

#include<stdio.h>
int bs(int array[],int sindex,int lindex,int element)
{
if(lindex>=sindex)
{
int middle=sindex+(lindex-sindex)/2;
if(array[middle]==element)
return middle;
if(array[middle]>element)
return bs(array,sindex,middle-1,element);

2021-2025-CSM-B
return bs(array,middle+1,lindex,element);
}
return -1;
}
int main(void)
{
int n,array[100];
printf("Enter n value ");

ACE Engineering College


scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
int element;
printf("enter the key element");
scanf("%d",&element);
int findex=bs(array,0,n-1,element);
if(findex==-1)
{
printf("The element is not found");
}
else
{
printf("The element is found at %d",findex+1);
}
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1

User Output

Page No: 56
Enter n value
3
3
6
9

ID: 21AG1A6666
enter the key element

3
The element is found at 1

Test Case - 2

User Output
Enter n value

2021-2025-CSM-B
3
6
9
enter the key element
5
The element is not found

ACE Engineering College


S.No: 15 Exp. Name: Tree traversal methods. Date: 2023-03-20

Aim:

Page No: 57
Write a program to implement the tree traversal methods.

Source Code:

bst.c

ID: 21AG1A6666
2021-2025-CSM-B
ACE Engineering College
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left,*right;

Page No: 58
};

struct node *createNode(int data)


{
struct node *p=(struct node*)malloc(sizeof(struct node));

ID: 21AG1A6666
p->data=data;
p->right=NULL;
p->left=NULL;
return p;
}
struct node* insert(struct node* root,int data)
{
if(root==NULL)
{
root=createNode(data);
}
else if(data<root->data)

2021-2025-CSM-B
{
root->left=insert(root->left,data);
}
else
{
root->right=insert(root->right,data);
}
return root;

ACE Engineering College


}
void preorder(struct node *root)
{
if(root==NULL)
{
return;
}
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node *root)
{
if(root==NULL)
{
return;
}
else
{
postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
}
void inorder(struct node *root)
{
return;
}
else
{
inorder(root->left);

Page No: 59
printf("%d ",root->data);
inorder(root->right);
}
}

ID: 21AG1A6666
int main()
{
int n,x;
struct node *root=NULL;
printf("Enter n value: ");
scanf("%d ",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
printf("Preorder Traversal: ");

2021-2025-CSM-B
preorder(root);
printf("\n");
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
printf("Postorder Traversal: ");
postorder(root);
}

ACE Engineering College


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter n value:
3
12
25
36
Preorder Traversal: 12 25 36
Inorder Traversal: 12 25 36
Postorder Traversal: 36 25 12

Test Case - 2

User Output
Enter n value:

5
12
02
26
35
188

Page No: 60
Preorder Traversal: 12 2 26 35 188
Inorder Traversal: 2 12 26 35 188
Postorder Traversal: 2 188 35 26 12

ID: 21AG1A6666
2021-2025-CSM-B
ACE Engineering College
S.No: 16 Exp. Name: Breadth First Search (BFS) Date: 2023-03-20

Aim:

Page No: 61
Write a program to implement Breadth First Search (BFS) graph traversal methods.

Source Code:

ID: 21AG1A6666
GraphsBFS.c

2021-2025-CSM-B
ACE Engineering College
#include<stdio.h>
#include<stdbool.h>
#define MAX_VERTICES 100

int queue[MAX_VERTICES];

Page No: 62
int rear=-1,front=-1;
int graph[MAX_VERTICES][MAX_VERTICES];
bool visited[MAX_VERTICES];

void enqueue(int vertex)

ID: 21AG1A6666
{
if(front==-1)front=0;
rear++;
queue[rear]=vertex;
}

int dequeue()
{
int vertex=queue[front];
front++;
if(front>rear)
{

2021-2025-CSM-B
front=rear=-1;
}
return vertex;
}

bool isQueueEmpty()
{
return front==-1;

ACE Engineering College


}

void bfs(int start,int n)


{
visited[start]=true;
enqueue(start);
while(!isQueueEmpty())
{
int currentVertex=dequeue();
printf("%d\n",currentVertex);
for(int i=start;i<=n;i++)
{
if(graph[currentVertex][i]==1 && !visited[i])
{
visited[i]=true;
enqueue(i);
}
}
}
}

int main()
{
int n,start,m,v1,v2;
printf("Enter the number of vertices : ");
scanf("%d",&m);
for(int i=0;i<m;i++)
{
printf("Enter source : ");
scanf("%d",&v1);

Page No: 63
printf("Enter destination : ");
scanf("%d",&v2);
graph[v1][v2]=1;
graph[v2][v1]=1;
}

ID: 21AG1A6666
printf("Enter Start Vertex for BFS : ");
scanf("%d",&start);
printf("BFS of graph : \n");
bfs(start,n);
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

2021-2025-CSM-B
User Output
Enter the number of vertices :
5
Enter the number of edges :

ACE Engineering College


Enter source :
1
Enter destination :

2
Enter source :
1
Enter destination :
4
Enter source :

4
Enter destination :
2
Enter source :
2
Enter destination :

3
Enter source :
4
Enter destination :

5
Enter Start Vertex for BFS :
1
BFS of graph :
1
2
4
3
5

Page No: 64
Test Case - 2

User Output

ID: 21AG1A6666
Enter the number of vertices :
4
Enter the number of edges :

3
Enter source :
1
Enter destination :
2
Enter source :

2021-2025-CSM-B
Enter destination :
3
Enter source :

3
Enter destination :
4
Enter Start Vertex for BFS :

ACE Engineering College


2
BFS of graph :
2
3
4

Test Case - 3

User Output
Enter the number of vertices :
9
Enter the number of edges :

12
Enter source :
0
Enter destination :
1
Enter source :

0
Enter destination :
3
Enter source :
0
Enter destination :
4
Enter source :

Page No: 65
Enter destination :
2
Enter source :

ID: 21AG1A6666
Enter destination :
3
Enter source :
3
Enter destination :

6
Enter source :
6
Enter destination :
4

2021-2025-CSM-B
Enter source :

6
Enter destination :
7
Enter source :

7
Enter destination :

ACE Engineering College


8
Enter source :
4
Enter destination :

5
Enter source :
2
Enter destination :
5
Enter source :

7
Enter destination :
5
Enter Start Vertex for BFS :

0
BFS of graph :
0
1
3
4
2
6
5
8
7
ACE Engineering College 2021-2025-CSM-B ID: 21AG1A6666 Page No: 66
S.No: 17 Exp. Name: Depth First Search (DFS) Date: 2023-03-20

Aim:

Page No: 67
Write a program to implement Depth First Search (DFS) graph traversal methods.

Source Code:

GraphsDFS.c

ID: 21AG1A6666
2021-2025-CSM-B
ACE Engineering College
# include <stdlib.h>
# include <stdbool.h>

struct Graph{
int v;

Page No: 68
struct Node** adjList;
};

struct Node{
int dest;

ID: 21AG1A6666
struct Node* next;
};

struct Node* createNode(int dest)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}

struct Graph* createGraph(int n)

2021-2025-CSM-B
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->v = n;
graph->adjList = (struct Node*) malloc(n * sizeof(struct Node));
int i;
for(i=0;i<n;i++)
{
graph->adjList[i] = NULL;

ACE Engineering College


}
return graph;
}

void addEdge(struct Graph* graph,int src,int dest)


{
struct Node* newNode = createNode(dest);
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;
}

void DFS(struct Graph* graph,int v)


{
int* stack = (int*) malloc(graph->v * sizeof(int));
int top = -1;
bool* visited = (bool*) malloc(graph->v * sizeof(bool));
int i;
for(i=0;i<graph->v;i++)
{
visited[i] = false;
}
stack[++top] = v;
while(top!=-1)
{
int s = stack[top--];
printf("%d\n",s);
visited[s] = true;
}
struct Node* temp = graph->adjList[s];
while(temp)

Page No: 69
{
int adj = temp->dest;
if(!visited[adj])
{
stack[++top] = adj;

ID: 21AG1A6666
}
temp=temp->next;
}
}
}
int main()
{
int numvertices,numedges;
printf("Enter the number of vertices : ");
scanf("%d",&numvertices);
printf("Enter the number of edges : ");
scanf("%d",&numedges);

2021-2025-CSM-B
struct Graph* graph = createGraph(numedges);
for(int i=0;i<numedges;i++)
{
int src,dest;
printf("Enter source : ");
scanf("%d",&src);
printf("Enter destination : ");
scanf("%d",&dest);

ACE Engineering College


addEdge(graph,src,dest);
}
int s;
printf("Enter Start Vertex for DFS : ");
scanf("%d",&s);
printf("DFS of graph : \n");
DFS(graph,s);
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the number of vertices :
6
Enter the number of edges :

7
Enter source :
1
Enter destination :
2
Enter source :
1
Enter destination :

Page No: 70
Enter source :
4
Enter destination :

ID: 21AG1A6666
Enter source :
2
Enter destination :
3
Enter source :

4
Enter destination :
5
Enter source :
4

2021-2025-CSM-B
Enter destination :

3
Enter source :
3
Enter destination :

6
Enter Start Vertex for DFS :

ACE Engineering College


1
DFS of graph :
1
2
3
6
4
5

Test Case - 2

User Output
Enter the number of vertices :
5
Enter the number of edges :
5
Enter source :

1
Enter destination :
2
Enter source :
1
Enter destination :
4
Enter source :

4
Enter destination :

Page No: 71
2
Enter source :

2
Enter destination :

ID: 21AG1A6666
3
Enter source :
4
Enter destination :

5
Enter Start Vertex for DFS :
1
DFS of graph :
1
2
3

2021-2025-CSM-B
4
5

Test Case - 3

User Output

ACE Engineering College


Enter the number of vertices :

4
Enter the number of edges :
4
Enter source :
1
Enter destination :

1
Enter source :
1
Enter destination :

3
Enter source :
2
Enter destination :
2
Enter source :

4
Enter destination :
3
Enter Start Vertex for DFS :
1
3
1
DFS of graph :

ACE Engineering College 2021-2025-CSM-B ID: 21AG1A6666 Page No: 72

You might also like