DS Lab Manual
DS Lab Manual
DS Lab Manual
Aim :- Write a program using DIV(J,K) which reads a positive integer N>10 and determines whether or not
N is a
prime number.
Theory – A positive integer which is only divisible by 1 and iself is known as prime number. For example: 13
is a prime number because it is only divisible by 1 and 13 but, 15 is not prime number because it is divisible
by 1, 3, 5 and 15.
This program takes a positive integer from user and stores it in variable n. Then, for loop is executed which
checks whether the number entered by user is perfectly divisible by i or not starting with initial value of i
equals to 2 and increasing the value of i in each iteration. If the number entered by user is perfectly divisible
by i then, flag is set to 1 and that number will not be a prime number but, if the number is not perfectly
divisible by i until test condition i<=n/2 is true means, it is only divisible by 1 and that number itself and that
//PROGRAM//
#include <stdio.h>
int main()
int n, i, flag=0;
scanf("%d",&n);
for(i=2;i<=n/2;++i)
if(n%i==0)
flag=1;
break;
else
return 0;
Observation:
29 is a prime number.
Aim :- Write a program which counts the number of particular character/word in the String.
Theory – This C Program Counts the Number of Words in a given text Or Sentence.
#include <stdio.h>
main()
printf("Hello World\n");
return 0;
"Hello World"
/*
*/
#include <stdio.h>
#include <string.h>
void main()
char s[200];
int count = 0, i;
scanf("%[^\n]s", s);
count++;
OBSERVATION:
Aim :- Write the programs for traversing of n item using the array.
Theory – Given an array of size n, find all elements in array that appear more than n/k times. For example, if
the input arrays is {3, 1, 2, 2, 1, 2, 3, 3} and k is 4, then the output should be [2, 3]. Note that size of array is 8
(or n = 8), so we need to find all elements that appear more than 2 (or 8/4) times. There are two elements that
appear more than two times, 2 and 3.
A simple method is to pick all elements one by one. For every picked element, count its occurrences by
traversing the array, if count becomes more than n/k, then print the element. Time Complexity of this method
would be O(n2).
A better solution is to use sorting. First, sort all elements using a O(nLogn) algorithm. Once the array is
sorted, we can find all required elements in a linear scan of array. So overall time complexity of this method is
O(nLogn) + O(n) which is O(nLogn).
//PROGRAM//
#include<stdio.h>
#include<conio.h>
int main()
{
for(int i=0;i<s;i++)
{
scanf("%d",&mat[i]);
}
max=mat[0];
for(int i=1;i<s;i++)
{
if(max < mat[i])
max = mat[i];
}
OBSERVATION:
56
19
48
67
78
84
Aim :- Write the programs for insertion and deletion of n item using the array.
Theory –
Program:
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
array[position-1] = value;
return 0;
}
-------------------------------------------------------
Process exited with return value 0
Press any key to continue - - - -
#include<stdio.h>
void main()
scanf("%d", &a[i]);
scanf("%d", &loc);
item = a[loc-1];
a[i] = a[i+1];
n--;
printf("\n\nAfter deletion:\n");
printf("\n%d", a[i]);
getch();
Output-
Theory –
LINEAR SEARCH
Linear Search ( ):
1. Repeat For J = 1 to N
4. Return
[End of If]
5. If (J > N) Then
[End of If]
7. Exit
// PROGRAM //
#include<stdio.h>
int main(){
return 0;
}
OBSERVATION:
BINARY SEARCH
Binary Search ( ):
Description: Here A is a sorted array having N elements. ITEM is the value to be searched. BEG denotes
first element and END denotes last element in the array. MID denotes the middle value.
6. Else
[End of If]
11. Else
[End of If]
13. Exit
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0,l,u,mid;
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
return 0;
}
1. Repeat For J = 1 to N
[End of If]
5. Exit.
BUBBLE SORT
#include<stdio.h>
int main()
{
int s,temp,i,j,a[20];
return 0;
}
OBSERVATION:
Enter 5 elements: 6 2 0 11 9
After sorting: 0 2 6 9 11
Aim :- Write a program to implement singly linked list that performs various operation
Theory –
Singly linked lists contain nodes which have a data field as well as a next field, which points to the
next node in line of nodes. Operations that can be performed on singly linked lists include insertion,
deletion and traversal.
/*
* C program to illustrate the operations of singly linked list
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
struct emp_data
{
int empno;
char empName[MAX];
char designation[MAX];
struct emp_data *next;
};
/* ********************************************************************/
/* Function to insert a node at the front of the linked list. */
/* front: front pointer, id: employee ID, name: employee name */
/* desg: Employee designation */
if (newnode == NULL)
{
printf("\n Allocation failed \n");
exit(2);
}
newnode->empno = id;
strcpy(newnode->empName, name);
strcpy(newnode->designation, desg);
newnode->next = front;
front = newnode;
return(front);
}
/* End of insert() */
/* ********************************************************/
/* Function to delete Node a node based on employee number */
/* front: front pointer, id: Key value */
/* Returns: the modified list. */
/* ********************************************************/
struct emp_data* delete Node(struct emp_data *front, int id)
{
struct emp_data *ptr;
struct emp_data *bptr;
if (front->empno == id)
{
ptr = front;
printf("\n Node deleted:");
print Node(front);
front = front->next;
free(ptr);
return(front);
}
for (ptr = front->next, bptr = front; ptr != NULL; ptr = ptr->next,
bptr = bptr->next)
{
/* ****************************************************************/
/* Function to search the nodes in a linear fashion based emp ID */
/* front: front pointer, key: key ID. */
/* ****************************************************************/
void search(struct emp_data *front, int key)
{
struct emp_data *ptr;
OBSERVATION:
Welcome to demonstration of singly linked list
---------------------------------------------
Press 1 to INSERT a node into the list
Press 2 to DELETE a node from the list
Press 3 to DISPLAY the list
Press 4 to SEARCH the list
Press 5 to EXIT
---------------------------------------------
Employee Details...
Emp No : 12
Name : ram
Designation : HR
-------------------------------------
Key found:
Employee Details...
Node deleted:
Employee Details...
Emp No : 12
Name : ram
Designation : HR
-------------------------------------
Aim :- Write a program which reads words WORD1 and WORD2 and then replaces each occurrence of
word1 in text by word2
Theory –
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char a[50],b[50];
int i,j;
clrscr();
scanf("%s",a);
scanf("%s",b);
strcat(a,b);
getch();
OBSERVATION:
Aim :- Write the programs for push and pop operation using the stacks.
Theory –
Description: Here STACK is an array with MAX locations. TOP points to the top most element and ITEM is
2. Print: Overflow
3. Else
7. Exit
Description: Here STACK is an array with MAX locations. TOP points to the top most element.
2. Print: Underflow
3. Else
[End of If]
7. Exit
// PROGRAM //
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 5
int stk[MAXSIZE];
int top;
};
STACK s;
/* Function declaration/Prototype*/
int pop(void);
void main ()
int choice;
int option = 1;
clrscr ();
s.top = -1;
while (option)
printf ("------------------------------------------\n");
printf ("------------------------------------------\n");
switch (choice)
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return;
void push ()
int num;
return;
else
s.top = s.top + 1;
s.stk[s.top] = num;
int pop ()
int num;
if (s.top == - 1)
return (s.top);
else
num = s.stk[s.top];
s.top = s.top - 1;
return(num);
void display ()
int i;
if (s.top == -1)
return;
else
printf ("\n");
/*---------------------------------------------------------------------------
Output
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
23
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
78
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
78
45
23
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
poped element is = 78
------------------------------------------
1 --> PUSH
3 --> DISPLAY
4 --> EXIT
------------------------------------------
45
23
-----------------------------------------------------------------------------*/
Aim :- Write the programs for insertion and deletion of n item from the queues.
Theory –
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE.
ITEM is the value to be inserted.
2. Print: Overflow
3. Else
5. Else
7. QUEUE[REAR] = ITEM
9. Exit
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of
the QUEUE.
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
6. Else
9. Exit
// PROGRAM //
#include <stdio.h>
#include <conio.h>
void insertion()
printf(“\nQueue Overflow\n”);
else if (rear==0)
front = rear = 1;
else if(rear==n)
rear=1;
else
rear=rear+1;
q[rear] = item;
printf(“%d is inserted\n\n”,item);
void deletion()
if(front==0)
printf(“\nQueue Underflow\n\n”);
item=q[front];
if(front==rear)
front=0;
rear=0;
front=1;
else
front=front+1;
printf(“\n%d is deleted\n\n”,item);
void show()
for(int i=0;i<=rear;i++)
printf(“%d\t”,q[i]);
int main()
int op;
scanf(“%d”,&n);
do
printf(“\n1 : Insert”);
printf(“\n2 : Delete”);
printf(“\n3 : Print”);
printf(“\n4 : Exit”);
scanf(“%d”,&op);
switch(op)
case 1:
insertion();
break;
case 2:
deletion();
break;
show();
break;
//default:
}while(op!=4);
printf(“\n—THE END—\n”);
OBSERVATION:
Enter the size of the queue : 4
1 : Insert
2 : Delete
3 : Print
4 : Exit
1 : Insert
2 : Delete
3 : Print
4 : Exit
Enter your choice : 1
Enter the Item : 80
80 is Inserted
1 : Insert
2 : Delete
3 : Print
4 : Exit
Enter your choice : 2
80 is Inserted
1 : Insert
2 : Delete
3 : Print
4 : Exit
Queue underflow
5 is Deleted
1 : Insert
2 : Delete
3 : Print
4 : Exit
Enter your choice :
4. Write the programs for insertion and deletion of n item using the
array.
7. Write the programs for traversing of n item from the linked list.
8 Write a program which reads words WORD1 and WORD2 and then
replaces each occurrence of word1 in text by word2
9. Write the programs for push and pop operation using the stacks.
10. Write the programs for insertion and deletion of n item from the
queues.