DS LAB Cycle 1
DS LAB Cycle 1
CYCLE - 1
ARRAY, STACK, QUEUE & SEARCHING
1
S3 CS DATA STRUCTURE LAB CEK
EXPERIMENT NO: 1
IMPLEMENTATION OF LINEAR SEARCH
AIM: To develop a program that can implement the linear searching technique.
ALGORITHM:
Step 1 : Start
Step 2 : Read the limit of the array
Step 3 : Read the elements a[i] into the array for i=0 to limit.
Step 4 : Read the value to be searched
Step 5 : For i=0 to limit, repeat steps 6 & 7.
Step 6 : If searched value = a[i] then return “element found at position i+1 and then exit.
Step 7 : Else, continue with i=i+1 and set flag =1, whenever searched element is found.
Step 8 : If flag=0, then print searched element is not found.
Step 9 : stop
PROGRAM
#include <stdio.h>
int main()
{
int n, s, flag = 0;
printf("Enter array limit: ");
scanf("%d", &n);
int a[n];
printf("Enter array elements: \n");
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Array elements are: \n");
for (int i = 0; i < n; i++)
printf("%d\n", a[i]);
printf("Enter the array element to be searched: ");
scanf("%d", &s);
for (int i = 0; i < n; i++)
if (s == a[i])
2
S3 CS DATA STRUCTURE LAB CEK
{
printf("%d found at position %d", s, i + 1);
flag = 1;
break;
}
if (flag == 0)
printf("%d not found.\n", s);
return 0;
}
OUTPUT
RESULT
The program to implement the linear searching technique is executed successfully and output is verified.
3
S3 CS DATA STRUCTURE LAB CEK
EXPERIMENT NO: 2
IMPLEMENTATION OF BINARY SEARCH
AIM: To develop a program that can implement the binary searching technique.
ALGORITHM:
Step 1 : Start
Step 2 : Read the limit of the array
Step 3 : Read the elements a[i] into the array for i=0 to limit.
Step 4 : Read the value to be searched.
Step 5 : Set low=0, high=n-1, mid=(low+high)/2.
Step 6 : While (low<=high)
Step 7 : If array[mid] < key, then low=mid+1.
Step 8 : Else if array[mid]=key
Step 9 : Print searched element found at location mid+1 and exit
Step 10 : Else high=mid-1
Step 11 : Mid=(low+high)/2
Step 12 : If low>high, print searched element not found
Step 13 : Stop
PROGRAM
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter array limit: ");
scanf("%d", &n);
printf("Enter array elements: \n");
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter the array element to be searched: ");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low + high) / 2;
while (low <= high)
{
if (array[mid] < key)
4
S3 CS DATA STRUCTURE LAB CEK
low = mid + 1;
else if (array[mid] == key)
{
printf("%d found at position %d.\n", key, mid + 1);
break;
}
else
high = mid - 1;
mid = (low + high) / 2;
}
if (low > high)
printf("%d not found.\n", key);
return 0;
}
OUTPUT
Enter array limit: 8
Enter array elements:
2 4 6 8 9 10 23 34
Enter the array element to be searched: 23
23 found at position 7.
Enter array limit: 8
Enter array elements:
2 4 6 8 9 10 23 34
Enter the array element to be searched: 4
4 found at position 2.
Enter array limit: 8
Enter array elements:
2 4 6 8 9 10 23 34
Enter the array element to be searched: 1
1 not found.
RESULT
The program to implement the binary searching technique is executed successfully and output is verified.
5
S3 CS DATA STRUCTURE LAB CEK
EXPERIMENT NO: 3
POLYNOMIAL ADDITION
AIM: To develop a program that represents polynomial and implements addition of polynomials using array of
structures.
ALGORITHM:
Step 1 : Start
Step 2 : Declare variables to hold the highest degrees of polynomial.
Step 3 : Read the polynomial terms from the user from constant term to the highest degree.
Read coefficient from user.
Exponent is same as index.
Step 4 : Display the polynomials.
Step 5 : Add two polynomial.
Step 6 : Check highest degree of polynomial 1 is greater than or equal to polynomial 2.
Step 7 : If true, add coefficients of polynomial 1 and 2 whenever corresponding exponents are same until
degree 2.
Step 8 : If any remaining terms in polynomial 1, just append it to the resultant polynomial.
Step 9 : If step 6 is false, add coefficients of polynomial 1 & 2 whenever corresponding exponents are
same until degree 1.
Step 10 : If any remaining terms in polynomial 2, just append it to the resultant polynomial.
PROGRAM
#include <stdio.h>
struct poly
{
int coeff;
int exp;
};
void main()
{
struct poly a[50], b[50], c[50];
int i, deg1, deg2;
printf("Enter the degree for polynomial 1: ");
scanf("%d", °1);
printf("Enter the coefficients:\n");
for (i = 0; i <= deg1; i++)
{
6
S3 CS DATA STRUCTURE LAB CEK
7
S3 CS DATA STRUCTURE LAB CEK
{
for (i = 0; i <= deg1; i++)
{
c[i].coeff = a[i].coeff + b[i].coeff;
c[i].exp = a[i].exp;
}
for (i = deg1 + 1; i <= deg2; i++)
{
c[i].coeff = b[i].coeff;
c[i].exp = b[i].exp;
}
}
printf("\nExpression after addition = \t%d", c[0].coeff);
for (int j = 1; j < i; j++)
printf(" + %dx^%d", c[j].coeff, c[j].exp);
}
OUTPUT
Enter the degree for polynomial 1: 3
Enter the coefficients:
x^0: 1
x^1: 3
x^2: 4
x^3: 2
Enter the degree for polynomial 2: 2
Enter the coefficients:
x^0: 1
x^1: 2
x^2: 3
Expression 1 = 1 + 3x^1 + 4x^2 + 2x^3
Expression 2 = 1 + 2x^1 + 3x^2
Expression after addition = 2 + 5x^1 + 7x^2 + 2x^3
RESULT
Polynomial addition using array of structures has been implemented and executed successfully.
8
S3 CS DATA STRUCTURE LAB CEK
EXPERIMENT NO: 4
IMPLEMENTATION OF SPARSE MATRIX USING ARRAY
ALGORITHM:
Step 1 : Start
Step 2 : Declare a 2D array for sparse matrix, initialize a variable size as 0, to count the number of non-
zero elements.
Step 3 : Read the size of sparse matrix from user and display the sparse matrix.
Step 4 : Check every ith & jth element, whether it is non-zero or not; if yes increment the value of “size”
by 1.
Step 5 : Declare a resultant triplet representation of sparse matrix with 3 rows and column size equal to
“size”.
Step 6 : Iterate through every ith & jth element, on every non-zero element corresponding row value
and column value along with the element is saved to resultant representation.
Step 7 : Stop
PROGRAM
#include<stdio.h>
int main()
{
int S[10][10],m,n,i,k=0,size=0;
printf("Enter number of rows in the matrix : ");
scanf("%d",&m);
printf("Enter number of columns in the matrix : ");
scanf("%d",&n);
printf("Enter elements in the matrix : ");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&S[i][j]);
printf("The matrix is \n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf(" %d ",S[i][j]);
9
S3 CS DATA STRUCTURE LAB CEK
if (S[i][j] != 0)
size++;
}
printf("\n");
}
int M[3][size];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (S[i][j] != 0)
{
M[0][k] = i;
M[1][k] = j;
M[2][k] = S[i][j];
k++;
}
printf("Triplet representation of the matrix is \n");
for (int i=0; i<3; i++)
{
for (int j=0; j<size; j++)
printf(" %d ", M[i][j]);
printf("\n");
}
return 0;
}
OUTPUT
Enter number of rows in the matrix : 4
Enter number of columns in the matrix : 3
Enter elements in the matrix : 0 0 0 0 1 8 0 4 0 0 0 0
The matrix is
0 0 0
0 1 8
0 4 0
0 0 0
10
S3 CS DATA STRUCTURE LAB CEK
RESULT
Program for sparse matrix implementation has been executed successfully.
11
S3 CS DATA STRUCTURE LAB CEK
EXPERIMENT NO: 5
IMPLEMENTATION OF STACK USING ARRAY
AIM: To develop a program that implements stack operations on a one dimensional array.
ALGORITHM:
main function:
Step 1 : Start
Step 2 : Initialize the top of the stack as -1.
Step 3 : Read the stack size from user (n).
Step 4 : Display the list of choices for the user to choose.
1. Push 2. Pop 3. Display 4. Exit
Step 5 : If choice=1, call the push()
Step 6 : If choice=2, call the pop()
Step 7 : If choice=3, call the display()
Step 8 : If choice=4, then go to step 9.
Step 9 : Stop
Push ()
Step 1 : If top of stack is greater than or equal to n-1, then display “overflow”.
Step 2 : Else increment the value of top by 1.
Step 3 : Store the item read from the user in the top position of the stack as follows:
Stack[top]=x
Pop ()
Step 1 : If top of stack is -1, then display “underflow”.
Step 2 : Else store the element at top of the stack to x.
Step 3 : Decrement the value of top by 1 and return value of x.
Display ()
Step 1 : Check whether the stack is empty, if yes, display stack is empty else go to step 2.
Step 2 : Display the elements in the stack using for loop
PROGRAM
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push();
void pop();
void display();
int main()
{
top=-1;
printf("\n Enter the size of STACK:");
scanf("%d",&n);
12
S3 CS DATA STRUCTURE LAB CEK
13
S3 CS DATA STRUCTURE LAB CEK
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
OUTPUT
Enter the size of STACK:5
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:1
Enter the Choice:1
Enter a value to be pushed:2
Enter the Choice:1
Enter a value to be pushed:3
Enter the Choice:1
Enter a value to be pushed:4
Enter the Choice:1
14
S3 CS DATA STRUCTURE LAB CEK
3
2
1
Press Next Choice
Enter the Choice:4
EXIT POINT
RESULT
Program for stack operations using array has been implemented and executed successfully.
15
S3 CS DATA STRUCTURE LAB CEK
EXPERIMENT NO: 6
IMPLEMENTATION OF AN APPLICATION OF STACK: INFIX TO POSTFIX CONVERSION
AIM: to develop a program that can convert an infix expression to a postfix expression.
ALGORITHM:
Step 1 : Start
Step 2 : Declare character array to hold the infix expression
Step 3 :
16