s.
no Experiment signature Date of
submission
1. Write a C
program to
20/11/2021
insert and
delete
elements in
an array at a
given
position.
2. Write a C program
to implement queue
20/11/2021
using array with the
following
operations:
enqueue(),
dequeue(),
display(), peek().
3. Write a C program
to implement
20/11/2021
circular queue using
array.
4. Write a C
program to
20/11/2021
implement
queue using
link list with
the
following
operations:
enqueue(),
dequeue(),
display(),
peek().
5. Write a C
program to
20/11/2021
implement
stack using
array with
the
following
operations:
push(),
pop(),
display(),
peek().
6. Write a C
program to
20/11/2021
implement
stack using
link list with
the
following
operations:
push(),
pop(),
display(),
peek()
7. Write a C
program to
20/11/2021
create,
insert,
delete, count
and display
elements in a
singly link
list.
(consider all
cases - at
beg, end, at
specific
position)
8. Write a C
program to
20/11/2021
create, insert,
delete, count
and display
elements in a
doubly link list.
(consider all
cases - at beg,
end, at specific
position)
9. Write a C
program to
20/11/2021
insert,
delete, count
and display
elements in a
circular link
list.
(consider all
cases - at
beg, end, at a
position)
10. Write a C
program to
20/11/2021
reverse a
singly linked
list.
11. Write a C
program to
20/11/2021
implement
the concept
of Linear
search.
12. Write a C
program to
20/11/2021
implement
the concept
of Binary
search.
13. Write a C
program to
20/11/2021
implement
the concept
of Bubble
sort.
14. Write a C
program to
20/11/2021
implement
the concept
of Insertion
sort.
15. Write a C program
to calculate factorial
20/11/2021
of a given number
using recursion.
List of Programs:
1. Write a C program to insert and delete elements in an array at a given
position.
#include <stdio.h>
int main()
{
int n;
scanf(“%d”,&n);
int arr[n];
int i;
for(i = 0; i < n; i++)
{
scanf(“%d”,&arr[i]);
}
int p;
scanf(“%d”,&p);
int e;
scanf(“%d”,&e);
if(p > n)
printf(“Invalid Input”);
else
{
for (i = n – 1; i >= p – 1; i–)
arr[i+1] = arr[i];
arr[p-1] = e;
printf(“Array after insertion is:\n”);
for (i = 0; i <= n; i++)
printf(“%d\n”, arr[i]);
}
return 0;
}
2. Write a C program to implement queue using array with the following
operations: enqueue(), dequeue(), display(), peek().
#include<stdio.h>
#include<stdlib.h>
struct queue
{
int size;
int f;
int r;
int* arr;
};
int isEmpty(struct queue *q){
if(q->r==q->f){
return 1;
}
return 0;
}
int isFull(struct queue *q){
if(q->r==q->size-1){
return 1;
}
return 0;
}
void enqueue(struct queue *q, int val){
if(isFull(q)){
printf("This Queue is full\n");
}
else{
q->r++;
q->arr[q->r] = val;
printf("Enqued element: %d\n", val);
}
}
int dequeue(struct queue *q){
int a = -1;
if(isEmpty(q)){
printf("This Queue is empty\n");
}
else{
q->f++;
a = q->arr[q->f];
}
return a;
}
int main(){
struct queue q;
q.size = 4;
q.f = q.r = 0;
q.arr = (int*) malloc(q.size*sizeof(int));
// Enqueue few elements
enqueue(&q, 12);
enqueue(&q, 15);
enqueue(&q, 1);
printf("Dequeuing element %d\n", dequeue(&q));
printf("Dequeuing element %d\n", dequeue(&q));
printf("Dequeuing element %d\n", dequeue(&q));
enqueue(&q, 45);
enqueue(&q, 45);
enqueue(&q, 45);
if(isEmpty(&q)){
printf("Queue is empty\n");
}
if(isFull(&q)){
printf("Queue is full\n");
}
return 0;
}
3. Write a C program to implement circular queue using array.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int cqueue[6];
int front = -1, rear = -1, n=6;
void enqueue(int val){
if ((front == 0 && rear == n-1) || (front == rear+1)) {
printf("Queue Overflow \n");
return;
}
if (front == -1) {
front = 0;
rear = 0;
}
else {
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void dequeue(){
if (front == -1) {
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d ", cqueue[front]);
if (front == rear) {
front = -1;
rear = -1;
}
else {
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void display(){
int f = front, r = rear;
if (front == -1) {
printf("Queue is empty");
return;
}
printf("Queue elements are :\n");
if (f <= r) {
while (f <= r){
printf("%d", cqueue[f]);
f++;
}
}
else {
while (f <= n - 1) {
printf("%d",cqueue[f]);
f++;
}
f = 0;
while (f <= r) {
printf("%d",cqueue[f]);
f++;
}
}
}
int menu(){
int choice;
printf("\n 1.Add value to the list");
printf("\n 2. Delete value to the list");
printf("\n 3. Travesre/View List");
printf("\n 4. exit");
printf("\n Please enter your choice: \t");
scanf("%d",&choice);
return(choice);
}
void main(){
int value;
while(1){
switch(menu()){
case 1:
printf("Input for insertion: ");
scanf("%d",&value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("invalid choice");
}
}
getch();
}
4. Write a C program to implement queue using link list with the following
operations: enqueue(), dequeue(), display(), peek().
#include <stdio.h>
#include <stdlib.h>
struct QNode {
int key;
struct QNode* next;
};
struct Queue {
struct QNode *front, *rear;
};
struct QNode* newNode(int k)
{
struct QNode* temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue* q, int k)
{
struct QNode* temp = newNode(k);
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
void deQueue(struct Queue* q)
{
if (q->front == NULL)
return;
struct QNode* temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
int main()
{
struct Queue* q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
deQueue(q);
printf("Queue Front : %d \n", q->front->key);
printf("Queue Rear : %d", q->rear->key);
return 0;
}
5. Write a C program to implement stack using array with the following
operations: push(), pop(), display(), peek().
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int size;
int top;
int *arr;
}
//to check if empty
int isEmpty(struct stack* ptr)
{
if (ptr->top == -1)
{
printf ("the stack is empty\n");
return 1;
}
else
{
return 0;
}
}
//to check if full
int
isFull (struct stack *ptr)
{
if (ptr->top == ptr->size - 1)
{
printf ("the stack is full\n");
return 1;
}
else
{
return 0;
}
}
//to push elements
void push (struct stack *ptr, int val)
{
if (isFull (ptr))
{
printf ("stack overflow! can not push %d to the stack\n", val);
}
else
{
ptr->top++;
ptr->arr[ptr->top] = val;
}
}
//to pop elements
int pop (struct stack *ptr)
{
if (isEmpty (ptr))
{
printf ("stack underflow!can not pop from the stack\n");
}
else
{
int val = ptr->arr[ptr->top];
ptr->top--;
return val;
}
}
//to peek elements
int peek (struct stack *sp, int i)
{
if (sp->top - i + 1 < 0)
{
printf ("not a valid position for the stack\n");
return -1;
}
else
{
return sp->arr[sp->top - i + 1];
}
else
{
return sp->arr[sp->top - i + 1];
}
}
//to display elements
void display ()
{
for (int i = top; i > -1; i--)
{
printf ("%d\n ", stack[i]);
}
int main ()
{
struct stack *sp = (struct stack *) malloc (sizeof (struct stack));
sp->size = 10;
sp->top = -1;
sp->arr = (int *) malloc (sp->size * sizeof (int));
printf ("stack has been created successfully\n");
printf ("before pushing,full:%d\n", isFull (sp));
printf ("before popping,empty:%d\n", isEmpty (sp));
push (sp, 1);
push (sp, 10);
push (sp, 11);
push (sp, 23);
push (sp, 45);
push (sp, 56);
push (sp, 78);
push (sp, 101);
push (sp, 33);
push (sp, 61);
push (sp, 70);
printf ("after pushing,full:%d\n", isFull (sp));
printf ("after popping,full:%d\n", isEmpty (sp));
printf ("popped%d from the stack\n", pop (sp));
for (int j = 1; j <= sp->top + 1; j++)
{
printf ("the value at position %d is %d\n", j, peek (sp, j));
}
printf ("Displaying elements of the stack -\n");
display ();
return 0;
}
6. Write a C program to implement stack using link list with the following
operations: push(), pop(), display(), peek().
7. Write a C program to create, insert, delete, count and display elements in a
singly link list. (consider all cases - at beg, end, at specific position)
8. Write a C program to create, insert, delete, count and display elements in a
doubly link list. (consider all cases - at beg, end, at specific position)
9. Write a C program to insert, delete, count and display elements in a circular
link list. (consider all cases - at beg, end, at a position)
10. Write a C program to reverse a singly linked list.
11. Write a C program to implement the concept of Linear search.
#include <stdio.h>
int linearSearch(int arr[],int sizearr,int element){
for(int i=0;i<sizearr;i++)
{
if(arr[i]==element){
return i;
}
}
return -1;
}
int main()
{
int arr[]={23,45,6,7,89,90,34,5,7,123,67,89};
int sizearr=sizeof(arr)/sizeof(int);
int element=6;
int searchIndex=linearSearch(arr,sizearr,element);
printf("the element %d was found at %d\n",element,searchIndex);
return 0;
}
12. Write a C program to implement the concept of Binary search.
#include <stdio.h>
int binarySearch(int arr[],int size,int element){
int lowest,middle,highest;
lowest=0;
highest=size-1;
while(lowest<=highest){
middle=(lowest+highest)/2;
if(arr[middle]==element){
return middle;
}
if(arr[middle]<element){
lowest=middle+1;
}
else{
highest=middle-1;
}
}
}
int main(){
int arr[]={1,23,34,36,45,56,67,77,89,100,109,207,407};
int size=sizeof(arr)/sizeof(int);
int element=77;
int searchIndex=binarySearch(arr,size,element);
printf("the size of the array is %d\n",size);
printf("the elementto be found is %d\n",element);
printf("the element %d was found at %d\n",element,searchIndex);
return 0;
}
13. Write a C program to implement the concept of Bubble sort.
#include <stdio.h>
int main()
int a[100], num, i, j, temp;
printf("\n Please Enter the total Number of Elements : ");
scanf("%d", &num);
printf("\n Please Enter the Array Elements : ");
for(i = 0; i < num; i++)
scanf("%d", &a[i]);
for(i = 0; i < num - 1; i++)
for(j = 0; j < num - i - 1; j++)
if(a[j] > a[j + 1])
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
printf("\n List Sorted in Ascending Order : ");
for(i = 0; i < num; i++)
printf(" %d \t", a[i]);
printf("\n");
return 0;
14. Write a C program to implement the concept of Insertion sort.
#include<stdio.h>
void InsertionSort(int arr[], int n)
{
int j, p;
int tmp;
for(p = 1; p < n; p++)
{
tmp = arr[p];
for(j = p; j > 0 && arr[j-1] > tmp; j--)
arr[j] = arr[j-1];
arr[j] = tmp;
}
}
int main()
{
int i, n, arr[100];
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
InsertionSort(arr,n);
printf("The sorted elements are : ");
for(i = 0; i < n; i++)
printf("%d ",arr[i]);
printf("\n");
return 0;
}
15. Write a C program to calculate factorial of a given number using recursion.
#include<stdio.h>
int find_factorial(int);
int main()
{
int num, fact;
printf("\nEnter any integer number:");
scanf("%d",&num);
fact =find_factorial(num);
printf("\nfactorial of %d is: %d",num, fact);
return 0;
}
int find_factorial(int n)
{
if(n==0)
return(1);
return(n*find_factorial(n-1));
}