0% found this document useful (0 votes)
79 views62 pages

Practical File of DS

The document is a practical file submitted by Rupinder Kaur to Deepika Mam for the course BCA (3rd Sem.) with registration number 72011019. It contains an index listing 26 programming problems to be solved related to data structures including linked lists, stacks, queues, trees, sorting, and searching. Each problem is numbered and includes a space for signature after completion.

Uploaded by

Ritik Gendu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views62 pages

Practical File of DS

The document is a practical file submitted by Rupinder Kaur to Deepika Mam for the course BCA (3rd Sem.) with registration number 72011019. It contains an index listing 26 programming problems to be solved related to data structures including linked lists, stacks, queues, trees, sorting, and searching. Each problem is numbered and includes a space for signature after completion.

Uploaded by

Ritik Gendu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Practical File

Data Structure (Lab)


Course Code: CAP202

Submitted By: Rupinder Kaur


Submitted To: Deepika Mam
Course: BCA (3rd Sem.)
Registration No: 72011019

INDEX
S.No: Practical name Signature
1. WAP to generate Fibonacci Series using recursion.
2. Write a function that interchanges the first element
with last element, second element with second last
element and so on.
3. WAP to multiply two Matrices.
4. Write a Function that removes all duplicate
elements from an Array.
5. WAP that insert an element in beginning of Linear
Link List.
6. WAP that delete an element from the beginning of
the Linear Link List.
7. WAP that delete an element from the end of the
Linear Link List.
8. WAP that delete an element after a given element
of the given Linear Link List.
9. WAP that reverse the element of the Linear Link
List.
10. WAP that concatenate two Linear Linked List.
11. WAP to remove the Top element of Stack.
12. WAP to insert (or push) an element at the Top of
Stack.
13. WAP to insert an element at the end of queue.
14. WAP to remove the first element of the queue.
15. WAP to illustrate the implementation of Binary
Search Tree
16. WAP to sort an array of integer in Ascending Order
using Bubble Sort.
17. WAP to sort an array of integer in Ascending Order
using Insertion Sort.
18. WAP to sort an array of integer in Ascending Order
using Quick Sort.
19. WAP to search an element using Linear Search
Method
20. WAP to search an element using Binary Search
Method.
21. WAP to check weather number is palindrome or
not.
22. WAP to insert an element in array
23. WAP to delete an element from array.
24. WAP to add to two matrix.
25. WAP to subtract two matrix.
26. WAP to implement string operation in c.

1.WAP to generate a Fibonacci Series using recursion.


Input
#include <stdio.h>
int main()
{
int first = 0, second = 1, sum = 0, n;
printf("Enter the end term for the series: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", first, second);
sum = first + second;
while(sum <= n)
{
printf("%d, ",sum);
first = second;
second = sum;
sum = first + second;
}
return 0;
}

OUTPUT
2.Write a function that interchanges the first element
with last element, second element with second last
element and so on.
INPUT
#include <stdio.h>
void Array_Swap(int *array , int n)
{
int i=0,temp=0;
for(i=0 ; i<n/2 ; i++)
{
temp = array[i];
array[i] = array[n-i-1];
array[n-i-1] = temp;
}
}
int main()
{
int array_1[30] = {0};
int i=0 ,n=0;
printf("\nEnter the number of elements for the array : ");
scanf("%d",&n);
printf("\nEnter the elements for array_1..\n");
for(i=0 ; i<n ; i++)
{
printf("array_1[%d] : ",i);
scanf("%d",&array_1[i]);
}
Array_Swap(array_1 , n);
printf("\nThe array after swap is..\n");
for(i=0 ; i<n ; i++)
{
printf("\narray_1[%d] : %d",i,array_1[i]);
}
return 0;
}

OUTPUT

3.WAP to multiply two matrix.


INPUT
#include<stdio.h
int main(void)
{
int c, d, p, q, m, n, k, tot = 0;
int fst[10][10], sec[10][10], mul[10][10];
printf(" Please insert the number of rows and columns for first matrix \n ");
scanf("%d%d", &m, &n);
printf(" Insert your matrix elements : \n ");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &fst[c][d]);
printf(" Please insert the number of rows and columns for second matrix\n");
scanf(" %d %d", &p, &q);
if (n != p)
printf(" Your given matrices cannot be multiplied with each other. \n ");
else
{
printf(" Insert your elements for second matrix \n ");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &sec[c][d] );
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
tot = tot + fst[c][k] * sec[k][d];
}
mul[c][d] = tot;
tot = 0;
}
}
printf(" The result of matrix multiplication or product of the matrices is: \n ");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d \t", mul[c][d] );
printf(" \n ");
}
}
return 0;
}

OUTPUT

4.WAP to that remove all duplicate element from an array.


INPUT
#include <stdio.h>
int main()
{
int arr[10], i, j, k, Size;
printf("\n Please Enter Number of elements in an array : ");
scanf("%d", &Size);
printf("\n Please Enter %d elements of an Array \n", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < Size; i++)
{
for(j = i + 1; j < Size; j++)
{
if(arr[i] == arr[j])
{
for(k = j; k < Size; k++)
{
arr[k] = arr[k + 1];
}
Size--;
j--;
}
}
}
printf("\n Final Array after Deleteing Duplicate Array Elements is:\n");
for (i = 0; i < Size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}

OUTPUT
5.WAP that insert an element in the beginning of Linear
linked list.
INPUT
#include <stdio.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void createNodeList(int n);
void NodeInsertatBegin(int num);
void displayList();
int main()
{
int n,num;
printf("\n\n Linked List : Insert a new node at the beginning of a Singly
Linked List:\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert at the beginning of the list : ");
scanf("%d", &num);
NodeInsertatBegin(num);
printf("\n Data after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void NodeInsertatBegin(int num)
{
struct node *fnNode;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num;
fnNode->nextptr = stnode;
stnode = fnNode;
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}

OUTPUT
6.WAP to delete an element an element from beginning of
linear linked list.
INPUT
#include<stdio.h>
#include<stdlib.h>
typedef struct nodetype
{ int info;
struct nodetype *next;
} node ;
void linked_list(node **,int );
void deletion_at_beg(node **);
void display(node *);
void main()
{
node *start;
int item,n,p;
start=NULL;
printf("Enter number of nodes :\n");
scanf("%d",&n);
for(p= 0 ;p< n ;p++)
{
printf("Enter item for node %d :\n",p+1);
scanf("%d",&item);
linked_list(&start,item) ;
}
printf("The list is like that after inserting element :\n");
display(start);
printf("\nPress any key to delete first node");
deletion_at_beg(&start);
printf("\nThe list after the deletion at first node is :\n");
display(start);
}
void linked_list(node **start,int item)
{
node *ptr,*last;
ptr =(node*)malloc(sizeof(node));
ptr->info = item ;
ptr->next = NULL;
if(*start == NULL)
*start = ptr;
else
{
last = *start;
while(last->next !=NULL)
{
last = last->next;
}
last->next = ptr ;
}
}
void deletion_at_beg(node **start)
{
node *ptr;
int temp;
ptr = *start ;
temp = ptr->info;
*start = ptr->next ;
free(ptr);
printf("\nDeleted item of first node is = %d",temp);
}
void display(node *start)
{
int n = 0;
while(start !=NULL)
{
printf("\t %d",start->info);
n++;
start = start->next;
}
printf("\nTotal number of nodes : %d",n);
}

OUTPUT
7.WAP to delete an element from the end of linear linked
list.
INPUT
#include <stdio.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void deleteLastNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete last node: ");
scanf("%d", &choice);
if(choice == 1)
deleteLastNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;

temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteLastNode()
{
struct node *toDelete, *secondLastNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
secondLastNode = head;
while(toDelete->next != NULL)
{
secondLastNode = toDelete;
toDelete = toDelete->next;
}
if(toDelete == head)
{
head = NULL;
}
else
{
secondLastNode->next = NULL;
}
free(toDelete);
printf("SUCCESSFULLY DELETED LAST NODE OF LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
OUTPUT

8.Write a program to delete an element at a given location in


a given linked list.
INPUT
#include <stdio.h>
struct Node {
int data;
struct Node* next;
};
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(struct Node** head_ref, int position)
{
if (*head_ref == NULL)
return;
struct Node* temp = *head_ref;
if (position == 0) {
*head_ref = temp->next;
free(temp);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++)
temp = temp->next;
if (temp == NULL || temp->next == NULL)
return;
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}
void printList(struct Node* node)
{
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
push(&head, 8);
puts("Created Linked List: ");
printList(head);
deleteNode(&head, 4);
puts("\nLinked List after Deletion at position 4: ");
printList(head);
return 0;
}

OUTPUT

9. WAP that reverse the element of the Linear Link List.


INPUT
#include <stdio.h>
struct node
{
int num;
struct node *next;
};
void create(struct node **);
void reverse(struct node **);
void release(struct node **);
void display(struct node *);
int main()
{
struct node *p = NULL;
int n;
printf("Enter data into the list\n");
create(&p);
printf("Displaying the nodes in the list:\n");
display(p);
printf("Reversing the list...\n");
reverse(&p);
printf("Displaying the reversed list:\n");
display(p);
release(&p);
return 0;
}
void reverse(struct node **head)
{
struct node *p, *q, *r;
p = q = r = *head;
p = p->next->next;
q = q->next;
r->next = NULL;
q->next = r;
while (p != NULL)
{
r = q;
q = p;
p = p->next;
q->next = r;
}
*head = q;
}
void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
void display(struct node *p)
{
while (p != NULL)
{
printf("%d\t", p->num);
p = p->next;
}
printf("\n");
}
void release(struct node **head)
{
struct node *temp = *head;
*head = (*head)->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}
OUTPUT

10. Write a program to concatenate two linear linked list.


INPUT
#include<stdio.h>
struct node
{
int info;
struct node *link;
};
struct node *create_list(struct node *);
struct node *concat( struct node *start1,struct node *start2);
struct node *addatbeg(struct node *start, int data);
struct node *addatend(struct node *start,int data);
void display(struct node *start);
main()
{
struct node *start1=NULL,*start2=NULL;
start1=create_list(start1);
start2=create_list(start2);
printf("First list is : ");
display(start1);
printf("Second list is : ");
display(start2);
start1=concat(start1, start2);
printf("Concatenated list is : ");
display(start1);
}
struct node *concat( struct node *start1,struct node *start2)
{
struct node *ptr;
if(start1==NULL)
{
start1=start2;
return start1;
}
if(start2==NULL)
return start1;
ptr=start1;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=start2;
return start1;
}
struct node *create_list(struct node *start)
{
int i,n,data;
printf("Enter the number of nodes : ");
scanf("%d",&n);
start=NULL;
if(n==0)
return start;

printf("Enter the element to be inserted : ");


scanf("%d",&data);
start=addatbeg(start,data);

for(i=2;i<=n;i++)
{
printf("Enter the element to be inserted : ");
scanf("%d",&data);
start=addatend(start,data);
}
return start;
}
void display(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("List is empty\n");
return;
}
p=start;
while(p!=NULL)
{
printf("%d ", p->info);
p=p->link;
}
printf("\n");
}
struct node *addatbeg(struct node *start,int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}
struct node *addatend(struct node *start, int data)
{
struct node *p,*tmp;
tmp= (struct node *)malloc(sizeof(struct node));
tmp->info=data;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=tmp;
tmp->link=NULL;
return start;
}

OUTPUT

11. WAP to remove the Top element of Stack.


INPUT
#include<stdio.h>
#define MAX 3
typedef struct
{
int TOP;
int ele[MAX];

}Stack;
void init(Stack *s)
{
s->TOP = -1;
}
int isFull(Stack *s)
{
if(s->TOP == MAX-1)
return 0;
return -1;
}
int isEmpty(Stack *s)
{
if(s->TOP == -1)
return 0;
return -1;
}
void push(Stack *s, int item)
{
if( !isFull(s) )
{
printf("\nStack is full");
return;
}
s->TOP = s->TOP + 1;
s->ele[s->TOP] = item;
}
int pop(Stack *s, int *item)
{
if(!isEmpty(s))
{
printf("\nStack is empty");
return -1;
}
*item = s->ele[s->TOP];
s->TOP = s->TOP - 1;

return 0;

}
int main()
{
Stack s;
int item;
init(&s);
push(&s,10);
push(&s,20);
push(&s,30);
pop(&s,&item);
printf("\nPoped Item : %d",item);
pop(&s,&item);
printf("\nPoped Item : %d",item);
pop(&s,&item);
printf("\nPoped Item : %d",item);
return 0;
return 0;
}

OUTPUT
12. Write a program to insert an element at the top of stack.
INPUT
#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
void display(void);
int pop(void);
void main ()
{
int choice;
s.top = -1;
do
{
push();
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &choice);
}while(choice==1);
pop();
display(); }
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to push\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n Current stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
} printf ("\n");}
OUTPUT

13. WAP to insert an element at the end of queue.


INPUT
#include <stdio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int items[SIZE], front = -1, rear = -1;
int main() {
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
display();return 0;
}
void enQueue(int value) {
if (rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (front == -1)
front = 0;
rear++;
items[rear] = value;
printf("\nInserted -> %d", value);
}
}
void deQueue() {
if (front == -1)
printf("\nQueue is Empty!!");
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}

// Function to print the queue


void display() {
if (rear == -1)
printf("\nQueue is Empty!!!");
else {
int i;
printf("\nQueue elements are:\n");
for (i = front; i <= rear; i++)
printf("%d ", items[i]);
}
printf("\n");
}

OUTPUT

14.WAP to remove the first element of queue.


INPUT
#include <stdio.h>
#define MAX 10
void addFront(int *, int, int *, int *);
void addRear(int *, int, int *, int *);
int delFront(int *, int *, int *);
int delRear(int *, int *, int *);
void display(int *);
int count(int *);
int main() {
int arr[MAX];
int front, rear, i, n;
front = rear = -1;
for (i = 0; i < MAX; i++)
arr[i] = 0;
addRear(arr, 5, &front, &rear);
addFront(arr, 12, &front, &rear);
addRear(arr, 11, &front, &rear);
addFront(arr, 5, &front, &rear);
addRear(arr, 6, &front, &rear);
addFront(arr, 8, &front, &rear);
printf("\nElements in a deque: ");
display(arr);
i = delFront(arr, &front, &rear);
printf("\nremoved item: %d", i);
printf("\nElements in a deque after deletion: ");
display(arr);
addRear(arr, 16, &front, &rear);
addRear(arr, 7, &front, &rear);
printf("\nElements in a deque after addition: ");
display(arr);
i = delRear(arr, &front, &rear);
printf("\nremoved item: %d", i);
printf("\nElements in a deque after deletion: ");
display(arr);
n = count(arr);
printf("\nTotal number of elements in deque: %d", n);
}
void addFront(int *arr, int item, int *pfront, int *prear) {
int i, k, c;
if (*pfront == 0 && *prear == MAX - 1) {
printf("\nDeque is full.\n");
return;
}
if (*pfront == -1) {
*pfront = *prear = 0;
arr[*pfront] = item;
return;
}
if (*prear != MAX - 1) {
c = count(arr);
k = *prear + 1;
for (i = 1; i <= c; i++) {
arr[k] = arr[k - 1];
k--;
}
arr[k] = item;
*pfront = k;
(*prear)++;
} else {
(*pfront)--;
arr[*pfront] = item;
}
}
void addRear(int *arr, int item, int *pfront, int *prear) {
int i, k;
if (*pfront == 0 && *prear == MAX - 1) {
printf("\nDeque is full.\n");
return;
}
if (*pfront == -1) {
*prear = *pfront = 0;
arr[*prear] = item;
return;
}
if (*prear == MAX - 1) {
k = *pfront - 1;
for (i = *pfront - 1; i < *prear; i++) {
k = i;
if (k == MAX - 1)
arr[k] = 0;
else
arr[k] = arr[i + 1];
}
(*prear)--;
(*pfront)--;
}
(*prear)++;
arr[*prear] = item;
}
int delFront(int *arr, int *pfront, int *prear) {
int item;
if (*pfront == -1) {
printf("\nDeque is empty.\n");
return 0;
}
item = arr[*pfront];
arr[*pfront] = 0;
if (*pfront == *prear)
*pfront = *prear = -1;
else
(*pfront)++;
return item;
}
int delRear(int *arr, int *pfront, int *prear) {
int item;
if (*pfront == -1) {
printf("\nDeque is empty.\n");
return 0;
}item = arr[*prear];
arr[*prear] = 0;
(*prear)--;
if (*prear == -1)
*pfront = -1;
return item;
}
void display(int *arr) {
int i;
printf("\n front: ");
for (i = 0; i < MAX; i++)
printf(" %d", arr[i]);
printf(" :rear");
}
int count(int *arr) {
int c = 0, i;
for (i = 0; i < MAX; i++) {
if (arr[i] != 0)
c++;
}
return c;
}

OUTPUT

15.WAP to illustrate the implementation of Binary search


Tree.
INPUT
#include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements:\n");
scanf("%d",&n);
printf("Enter %d integers:\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter the value to find:\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d is present at index %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.\n", search);
return 0;
}

OUTPUT
16. WAP to sort an array of integer in Ascending Order
using Bubble Sort
INPUT
#include<stdio.h>
#define MAX 5
void bubble_sort(int arr[]);
int main(void)
{
int arr[MAX];
for(int i = 0; i < MAX; i++)
{
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("\nUnsorted array: \n");
for(int i = 0; i < MAX; i++)
{
printf("%d ", arr[i]);
}
bubble_sort(arr);
printf("\n\nSorted array: \n");
for(int i = 0; i < MAX; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
void bubble_sort(int arr[])
{
int tmp,
is_swapped;
for(int i = 0; i < MAX; i++)
{
is_swapped = 0;
for(int j = 0; j < MAX - 1 - i; j++)
{
if(arr[j] > arr[j+1])
{
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
is_swapped = 1;
}
}
if (!is_swapped)
{
break;
}
}
}

OUTPUT

17. WAP to sort an array of integer in Ascending Order


using Insertion Sort.
INPUT
#include <stdio.h>
int main()
{ int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && arr[j-1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}

OUPUT

18. WAP to sort an array of integer in Ascending Order


using Quick Sort.
INPUT
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void quick_sort(int a[],int l,int u)
{
int j;
if(l<u)
{
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}}
int partition(int a[],int l,int u)
{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
}
OUTPUT
19. WAP to search an element using Linear Search Method.
INPUT
#include <stdio.h>
void main()
{ int num;
int i, keynum, found = 0;
printf("Enter the number of elements ");
scanf("%d", &num);
int array[num];
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the element to be searched ");
scanf("%d", &keynum);
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array at position %d",i+1);
else
printf("Element is not present in the array\n");
}
OUTPUT

20. WAP to search an element using Binary Search Method.


INPUT
#include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}

OUTPUT
21.WAP for Palindrome in c.
INPUT
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
OUTPUT

22.WAP to insert an element in array.


INPUT
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
return 0;
}

OUTPUT

23.WAP to delete the element in an array.


INPUT
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if (position >= n+1)
printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];
printf("Resultant array:\n");
for (c = 0; c < n - 1; c++)
printf("%d\n", array[c]);
}
return 0;
}

OUPUT

24. WAP to implement addition of two matrix.


INPUT
#include <stdio.h>
int main()
{
int a[2][3],b[2][3],c[2][3],i,j;

printf("\nENTER VALUES FOR MATRIX A:\n");


for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRIX B:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nTHE VALUES OF MATRIX C ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("\n");
}
return 0;
}

OUTPUT

25. WAP for subtraction of two matrix.


INPUT
#include <stdio.h>
int main()
{
int a[2][3],b[2][3],c[2][3],i,j;
printf("\nENTER VALUES FOR MATRIX A:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRIX B:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
printf("\nTHE VALUES OF MATRIX C ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("\n");
}
return 0;
}

OUTPUT
26. Implementation of string operation in c.
INPUT
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}

OUTPUT

You might also like