Professional Documents
Culture Documents
DS Lab
DS Lab
DS Lab
Aim:-
Write a program to read ‘N’ numbers of elements
into an array and also perform the following operation
on an array
a) Insert an element at given index of array
Procedure:-
a) Insertion at given index steps:-
Program:-
#include<stdio.h>
#include<conio.h>
#define max 50
void main()
{
int arr[max],size;
int element, pos, i;
arr[pos-1] = element;
size++;
printf(“Array after new element:\n”);
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
}
else
printf("Invalid Position\n");
getch();
}
Output:-
Enter Array size: 5
99
87 55 34 99 23 12
2
2. Aim:-
Write a program to read ‘N’ numbers of elements
into an array and also perform the following operation
on an array
Delete an existing element
#include<stdio.h>
#include<conio.h>
#define size 5
void main()
{
int arr[size] = {1, 20, 5, 78, 30};
int key, i, index = -1;
printf("Enter element to delete\n");
scanf("%d",&key);
for(i = 0; i < size; i++)
{
if(arr[i] == key)
{
index = i;
break;
}
}
if(index != -1)
{
for(i = index; i < size - 1; i++)
arr[i] = arr[i+1];
3
printf("New Array : ");
for(i = 0; i < size - 1; i++)
printf("%d ",arr[i]);
}
else
printf("Element Not Found\n");
getch();
}
Output:-
78
New Array: 1 20 5 30
4
3.Aim: - Write a Program to implement the Stack
operations push and pop using an array.
Procedure:-
Program:-
#include <stdio.h>
#include <conio.h>
#define MAX 3
int st[MAX], top=-1;
void push(int st[], int val);
int pop(int st[]);
void display(int st[]);
void main()
{
int val, option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
5
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st, val);
break;
case 2:
val = pop(st);
if(val != -1)
printf("\n The value deleted from stack is: %d", val);
break;
case 3:
display(st);
break;
}
}while(option != 4);
getch();
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
int pop(int st[])
{
int val;
if(top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val = st[top];
top--;
return val;
}
}
void display(int st[])
6
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n");
}
}
Output:-
*****MAIN MENU*****
1. PUSH
2. POP
3. DISPLAY
4. EXIT
*****MAIN MENU*****
1. PUSH
2. POP
3. DISPLAY
4. EXIT
*****MAIN MENU*****
1. PUSH
2. POP
7
3. DISPLAY
4. EXIT
*****MAIN MENU*****
1. PUSH
2. POP
3. DISPLAY
4. EXIT
*****MAIN MENU*****
1. PUSH
2. POP
3. DISPLAY
4. EXIT
*****MAIN MENU*****
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your option: 4
8
4..Aim: -
Write Programs to implement the Queue operations using
Linked List.
Procedure:-
Algorithm for Insertion:-
Step 1: Allocate the space for the
new node PTR Step 2: SET PTR ->
DATA = VAL
Step 3: IF FRONT =
NULL SET FRONT = REAR
= PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT =
PTR SET REAR = PTR
SET REAR -> NEXT =
NULL [END OF IF]
Step 4: END
Algorithm for Deletion:-
Step 1: IF FRONT = NULL
Write “Underflow”
Go to
Step 5
[END OF
IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT ->
NEXT Step 4: FREE PTR
Step 5: END
Program:- #include
<stdio.h>#include
<conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
9
};
struct queue
{
struct node *front;
struct node *rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue *insert(struct queue *,int);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int peek(struct queue *);
int main()
{
int val, option;
create_queue(q);
clrscr();
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. INSERT");
printf("\n 2. DELETE");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to insert in the queue:");
scanf("%d", &val);
10
q = insert(q,val);
break;
case 2:
q = delete_element(q);
break;
case 3:
val = peek(q);
if(val! = –1)
printf("\n The value at front of queue is : %d", val);
break;
case 4:
q = display(q);
break;
}
}while(option != 5);
getch();
return 0;
}
void create_queue(struct queue *q)
{
q -> rear = NULL;
q -> front = NULL;
}
struct queue *insert(struct queue *q,int val)
{
struct node *ptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr -> data = val;
if(q -> front == NULL)
{
q -> front = ptr;
q -> rear = ptr;
q -> front -> next = q -> rear -> next = NULL;
}
else
{
q -> rear -> next = ptr;
q -> rear = ptr;
q -> rear -> next = NULL;
} return q;
}
11
struct queue *display(struct queue *q)
{
struct node *ptr;
ptr = q -> front;
if(ptr == NULL)
printf("\n QUEUE IS EMPTY");
else
{
printf("\n");
while(ptr!=q -> rear)
{
printf("%d\t", ptr -> data);
ptr = ptr -> next;
}
printf("%d\t", ptr -> data);
}
return q;
}
struct queue *delete_element(struct queue *q)
{
struct node *ptr;
ptr = q -> front;
if(q -> front == NULL)
printf("\n UNDERFLOW");
else
{
q -> front = q -> front -> next;
printf("\n The value being deleted is : %d", ptr -> data);
free(ptr);
}
return q;
}
int peek(struct queue *q)
{
if(q->front==NULL)
{
printf("\n QUEUE IS EMPTY");
return –1;
}
else
return q->front->data;
12
}
Output:-
*****MAIN MENU*****
1. INSERT
2. DELETE
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 3
QUEUE IS EMPTY
Enter your option: 5
13
5. Aim:-
Write a program for polynomial addition using single linked
list.
Procedure:-
Polynomial is a mathematical expression that consists of
variables and coefficients. For example x^2 - 4x + 7
In the Polynomial linked list, the coefficients and
exponents of the polynomial are defined as the data node of
the list.
For adding two polynomials that are stored as a
linked list. We need to add the coefficients of variables
with the same power. In a linked list node contains 3
members, coefficient value link to the next node.
A linked list that is used to store
Polynomial looks like − Polynomial: 4x7 +
12x2 + 45
Program:-
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
14
int ch;
do
{
printf("\n enter coeff:");
scanf("%d",&node->coeff);
printf("\n enter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\n continue(1/0):");
scanf("%d",&ch);
}
while(ch==1);
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
16
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct
link));
poly=poly->next;
poly->next=NULL;
}
}
void main()
{
char ch;
clrscr();
do{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\nenter 1st number:");
create(poly1);
printf("\nenter 2nd number:");
create(poly2);
printf("\n1st Number:");
show(poly1);
printf("\n2nd Number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\nAdded polynomial:");
show(poly);
printf("\n add two more numbers:");
scanf("%c",&ch);
}
while(ch=='y' || ch=='Y');
getch();
}
17
Output:-
enter coeff:5
enter power:4
continue(1/0):1
enter coeff:6
enter power:3
continue(1/0):1
enter coeff:3
enter power:2
continue(1/0):1
enter coeff:8
enter power:0
continue(1/0):0
enter coeff:4
enter power:3
continue(1/0):1
enter coeff:6
enter power:2
continue(1/0):1
enter coeff:9
enter power:1
continue(1/0):0
1st Number:5x^4+6x^3+3x^2+8x^0
2nd Number:4x^3+6x^2+9x^1
Added polynomial:5x^4+10x^3+9x^2+9x^1+8x^0
add two more numbers: no
18
6. Aim:-
Write a program to search an item in a given list using
the Linear search Algorithms
Procedure:-
a. Linear Search: A linear search, also known as a
sequential search, is a method of finding an element
within a list. It checks each element of the list
sequentially until a match is found or the whole list
has been searched.
1. Begin with the leftmost element of arr[] and one by
one compare x with each element.
2. If x matches with an element then return the index.
3. If x does not match with any of the elements then
return -1.
Program:-
#include<stdio.h>
void main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
19
getch();
}
Output:-
20
7. Aim:-
Write a program to search an item in a given list using
the Binary Search Algorithms
Procedure:-
Binary search:-
#include <stdio.h>
#include<conio.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
21
return -1;
}
void main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x,result;
clrscr();
printf("Enter a element u want to search:");
scanf("%d",&x);
result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present in
array") : printf("Element is present at index %d", result);
getch();
}
Output:-
Enter a element u want to search: 10
22
8.Aim:-
Write a program for implementation of the Bubble Sort
Algorithm.
Procedure:-
The basic methodology of the working of bubble sort is as
follows:
(a) In Pass 1, A[0] and A[1] are compared, then A[1] is
compared with A[2], A[2] is compared with A[3], and so
on. Finally, A[N–2] is compared with A[N–1]. Pass 1
involves n–1 comparisons and places the biggest element
at the highest index of the array.
(b) In Pass 2, A[0] and A[1] are compared, then A[1] is
compared with A[2], A[2] is compared with A[3], and so
on. Finally, A[N–3] is compared with A[N–2]. Pass 2
involves n–2 comparisons and places the second biggest
element at the second highest index of the array.
(c) In Pass 3, A[0] and A[1] are compared, then A[1] is
compared with A[2], A[2] is compared with A[3], and so
on. Finally, A[N–4] is compared with A[N–3]. Pass 3
involves n–3 comparisons and places the third biggest
element at the third highest index of the array.
Program:-
#include <stdio.h>
void bubble_sort(int a[], int n) {
int i = 0, j = 0, tmp;
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
23
if (a[j] > a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
void main() {
int a[100], n, i, d, swap;
printf("Enter number of elements in the array:\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
bubble_sort(a, n);
printf("Printing the sorted array:\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}
Output:-
Enter number of elements in the array:
5
Enter 5 integers
8
6
4
2
9
Program:-
#include <stdio.h>
#define MAX 5
void depth_first_search(int adj[ ][MAX],int visited[ ],int
start)
{
int stack[MAX];
25
int top = –1, i;
printf("%c–>",start + 65);
visited[start] = 1;
stack[++top] = start;
while(top ! = –1)
{
start = stack[top];
for(i = 0; i < MAX; i++)
{
if(adj[start][i] && visited[i] == 0)
{
stack[++top] = i;
printf("%c–>", i + 65);
visited[i] = 1;
break;
}
}
if(i == MAX)
top––;
}
}
int main()
{
int adj[MAX][MAX];
int visited[MAX] = {0}, i, j;
printf("\n Enter the adjacency matrix: ");
for(i = 0; i < MAX; i++)
for(j = 0; j < MAX; j++)
scanf("%d", &adj[i][j]);
printf("DFS Traversal: ");
depth_first_search(adj,visited,0);
printf("\n");
return 0;
}
26
Output:-
27
10.Aim:-
Write a program to implement Breadth First Search graph
traversals algorithm
Procedure:-
Program:-
#include <stdio.h>
#define MAX 5
void breadth_first_search(int adj[][MAX],int visited[],int
start)
{
int queue[MAX],rear = -1,front =-1, i;
queue[++rear] = start;
visited[start] = 1;
28
while(rear != front)
{
start = queue[++front];
printf("%c \t",start + 65);
for(i = 0; i < MAX; i++)
{
if(adj[start][i] == 1 && visited[i] == 0)
{
queue[++rear] = i;
visited[i] = 1;
}
}
}
}
void main()
{
int visited[MAX] = {0};
int adj[MAX][MAX], i, j;
clrscr();
printf("\n Enter the adjacency matrix: ");
for(i = 0; i < MAX; i++)
for(j = 0; j < MAX; j++)
scanf("%d", &adj[i][j]);
printf(“Breadth First traversal:\n”);
breadth_first_search(adj,visited,0);
getch();
}
29
Output:-
1 0 1 0 1
1 1 1 1 0
0 0 1 0 1
0 0 0 0 1
1 1 1 1 1
A C E B D
30