Professional Documents
Culture Documents
Nikhil Uu
Nikhil Uu
Nikhil Uu
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
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;
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;
}
}
}
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
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
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()
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 :
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)
{
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
{
Page No: 12
printf("Enter number to delete: ");
scanf("%d",&x);
deleteion(x);
break;
case 3:
ID: 21AG1A6666
display();
break;
}
}
}
2021-2025-CSM-B
User Output
Operations on doubly linked list
1. Insert
2.Remove
3. Display
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
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
};
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;
}
}
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{
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
}
}
}
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
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.
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
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:
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()
{
Page No: 23
}
else
{
printf("Peek value = %d\n",a[top]);
}
ID: 21AG1A6666
}
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
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 :
Aim:
Page No: 25
Write a program to implement stack using dynamic arrays(pointer).
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
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);
}
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");
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
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
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
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
Aim:
Page No: 32
Write a program to implement queue(its operations) using arrays.
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
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);
}
}
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;
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
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.
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
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).
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
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);
}
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;
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");
}
}
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
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;
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]);
}
}
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;
}
///
User Output
Enter value of n :
9
2
5
3
2 5 9
After sorting the elements in the array are
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()
{
User Output
Enter value of n :
3
2
9
6
2 6 9
After sorting the elements in the array are
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");
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;
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
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;
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 ");
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
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
};
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;
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);
}
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];
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;
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;
}
2021-2025-CSM-B
User Output
Enter the number of vertices :
5
Enter the number of edges :
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 :
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 :
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;
};
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;
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);
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 :
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
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 :