0% found this document useful (0 votes)
46 views

Data Structure Lab Program

The document contains C code examples demonstrating various operations on arrays and matrices, including: 1) Defining and initializing a structure containing student data and printing values. 2) Performing string operations like length, copy, comparison and concatenation. 3) Inserting and deleting elements from an array at specified positions, and traversing arrays. 4) Merging two sorted arrays. 5) Performing matrix addition, subtraction, multiplication, and computing the transpose of a matrix by swapping row and column indexes.

Uploaded by

bosoho4313
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Data Structure Lab Program

The document contains C code examples demonstrating various operations on arrays and matrices, including: 1) Defining and initializing a structure containing student data and printing values. 2) Performing string operations like length, copy, comparison and concatenation. 3) Inserting and deleting elements from an array at specified positions, and traversing arrays. 4) Merging two sorted arrays. 5) Performing matrix addition, subtraction, multiplication, and computing the transpose of a matrix by swapping row and column indexes.

Uploaded by

bosoho4313
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

1. Program to illustrate structure.

#include<stdio.h>
#include<string.h>

struct Student
{
char name[25];
int age;
char branch[10];
char gender; //F for female and M for male
};

int main()
{
struct Student s1;
s1.age = 18;

//using string function to add name


strcpy(s1.name, "Viraaj");

//displaying the stored values


printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);

return 0;
}

2. Program to illustrate String Operations

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//strlen(), strcpy(),strcat(),strcmp()

int main()
{
char str1[30], str2[30];
int x, l;

printf("Enter the First string:\n");


gets(str1);

printf("Enter the Second string:\n");


gets(str2);

l=strlen(str1);
printf("\nThe length of first string: %d\n\n",l);

x=strcmp(str1,str2); //0,1,-1

if(x == 0)
{
printf("Both strings are equal\n\n");
}
else if(x>0)
{
printf("String 2 is greater than String 1\n\n");

}
else
{
printf("String 1 is greater than String 2\n\n");
}

printf("String 1 : %s\n", str1);

printf("String 1 : %s\n\n", str2);

printf("After the Concatenation Operation\n");


strcat(str2,str1);
printf("The second string: %s\n\n", str2);

printf("After the Copying the content of String 1 into String 2: \n");


strcpy(str2,str1);
printf("The second string: %s\n\n", str2);

return 0;
}

String Reverse Function –

#include <stdio.h>
#include <string.h>
int main()
{
char s[100];

printf("Enter a string to reverse\n");


gets(s);

strrev(s);

printf("Reverse of the string: %s\n", s);

return 0;
}

3. Program to illustrate Array Insertion at specified location, Deletion and Traversal.


//Program to insert new element at specified position in Array.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[50], i, num, pos, size;

printf("Enter the size of array: ");


scanf("%d", &size);

printf("Enter the elements:\n");


for(i=0; i<size; i++) //insertion
scanf("%d", &a[i]);

printf("The elements of Array:\n");


for(i=0; i<size; i++)
printf("a[%d] = %d\n", i,a[i]); //traversing

printf("Enter new element you want to insert: ");


scanf("%d", &num);

printf("\nEnter the position at which you want to insert: ");


scanf("%d", &pos);

for(i=size-1; i>=pos-1; i--)


a[i+1]=a[i];

a[pos-1]=num;
size++;

printf("\nThe elements of new Array:\n");


for(i=0; i<size; i++)
printf("a[%d] = %d\n", i,a[i]);
return 0;
}

4. Program to illustrate Deletion of an element in Array from specified position.

//Program to delete an element from specified position in Array.


#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[50], i, num, pos, size;

printf("Enter the size of array: ");


scanf("%d", &size);

printf("Enter the elements:\n");


for(i=0; i<size; i++) //insertion
scanf("%d", &a[i]);

printf("The elements of Array:\n");


for(i=0; i<size; i++)
printf("a[%d] = %d\n", i,a[i]); //traversing

printf("\nEnter the position from which you want to delete: ");


scanf("%d", &pos);

for(i=pos-1; i<size-1; i++)


a[i] = a[i+1];

size--;

printf("\nThe elements of new Array:\n");


for(i=0; i<size; i++)
printf("a[%d] = %d\n", i,a[i]);
return 0;
}

5. Merging 2 Arrays-

#include <stdio.h>

int main()
{
int n1,n2,n3; //Array Size Declaration
printf("\nEnter the size of first array ");
scanf("%d",&n1);
printf("\nEnter the size of second array ");
scanf("%d",&n2);

n3=n1+n2;
printf("\nEnter the elements of first array:\n");
int a[n1],b[n2],c[n3]; //Array Declaration
for(int i=0;i<n1;i++) //Array Initialized
{
scanf("%d",&a[i]);
c[i]=a[i];
}
int k=n1;
printf("\nEnter the elements of second array:\n");
for(int i=0;i<n2;i++) //Array Initialized
{
scanf("%d",&b[i]);
c[k]=b[i];
k++;
}
printf("\nThe merged array..\n");
for(int i=0;i<n3;i++)
printf("%d ",c[i]); //Print the merged array

printf("\nAfter sorting...\n");
for(int i=0;i<n3;i++) //Sorting Array
{
int temp;
for(int j=i+1; j<n3 ;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}

for(int i=0 ; i<n3 ; i++) //Print the sorted Array


{
printf(" %d ",c[i]);
}
return 0;
}

6. 2D Array

7. Matrix Addition –

#include <stdio.h>
int main() {
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

8. Matrix Subtraction –

#include <stdio.h>
int main() {
int r, c, a[10][10], b[10][10], sub[10][10], i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sub[i][j] = a[i][j] - b[i][j];
}

// printing the result


printf("\nSubtraction of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sub[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

9. Matrix Multiplication –

#include <stdio.h>

int main()
{
int m, n, p, q, i, j, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");

for (i = 0; i < m; i++)


for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);

printf("Enter number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");

for (i = 0; i < p; i++)


for (j = 0; j < q; j++)
scanf("%d", &second[i][j]);

for (i = 0; i < m; i++) {


for (j = 0; j < q; j++) {
for (k = 0; k < p; k++) {
sum = sum + first[i][k]*second[k][j];
}

multiply[i][j] = sum;
sum = 0;
}
}

printf("Product of the matrices:\n");


for (i = 0; i < m; i++) {
for (j = 0; j < q; j++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

10. Transpose of Matrix –

#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// asssigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

// printing the matrix a[][]


printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// computing the transpose


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// printing the transpose


printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
11. Linear Search –

#include<stdio.h>
#include<conio.h>

void main(){
int list[20],size,i,item;

printf("Enter size of the list: ");


scanf("%d",&size);

printf("Enter any %d integer values: ",size);


for(i = 0; i < size; i++)
scanf("%d",&list[i]);

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


scanf("%d",&item);

// Linear Search Logic


for(i = 0; i < size; i++)
{
if(item == list[i])
{
printf("Element is found at position %d\n\n", i+1);
break;
}
}
if(i == size)
printf("Given element is not found in the list!!!\n\n");

12. Binary Search –

#include <stdio.h>
#include <stdlib.h>

int main()
{
int first, last, mid, item, a[100], size, i;

printf("Enter the size of list:");


scanf("%d", &size);

printf("\nEnter %d elements in Ascending order:\n", size);


for(i=0; i<size; i++)
scanf("%d", &a[i]);

printf("\nEnter the number to be searched: ");


scanf("%d", &item);

first=0;
last= size-1;
mid = (first+last)/2;

while(first <= last)


{
if(a[mid]<item)
first=mid+1;
else if(a[mid] == item)
{
printf("Element found at index %d \n", mid);
break;
}
else
last=mid-1;

mid=(first+last)/2;
}
if(first >last)
printf("\nElement not found..\n\n");

return 0;
}

13. Bubble Sort

#include <stdio.h>

int main()
{
int a[100], n, i, j, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)


scanf("%d", &a[i]);

for (i = 0 ; i < n - 1; i++)


{
for (j = 0 ; j < n - i - 1; j++)
{
if (a[j] > a[j+1]) /* For decreasing order use '<' instead of '>' */
{
swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
}
}
}

i = 0;
while(i < n - 1) {
j = 0;
while(j < n - i - 1) {
if(a[j] > a[j + 1]) {
swap = a[j];
a[j] = a[j + 1];
a[j + 1] = swap;
}
j++;
}
i++;
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}

14. Insertion Sort:


#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("\t%d", arr[i]);
}
return 0;
}

15. Selection Sort:

#include<stdio.h>
int main()
{
int n, i, j, temp, min;
printf("Enter the no. of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n ");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++)
{

// Finding the minimum element in unsorted array


min = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min])
min = j;
}
// Swaping the found minimum element with the first element
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
printf("Sorted elements: \n");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}

16. Array and Pointer-


#include <stdio.h>

int main()
{
int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c);
printf("\n%d", *pc);
return 0;
}
----------------------------------------------------------------------
#include <stdio.h>

int main()
{
int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Output: 1
printf("\n%d", c);
return 0;
}
#include <stdio.h>

int main()
{
int* pc, c, d;
c = 5;
d = -15;
pc = &c;
printf("%d", *pc); // Output: 5
pc = &d;
printf("\n%d", *pc);
return 0;
}
#include <stdio.h>
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
// ptr is assigned the address of the third element
ptr = &x[2];
printf("*ptr = %d \n", *ptr); // 3
printf("*(ptr+1) = %d \n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
return 0;
}

17. Stack Operation –

#include <stdio.h>
#include <stdlib.h>

void push();
void pop();
void display();
int stack[5];
int top=-1;
int main()
{
int choice;

do{
printf("\n\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. Traverse");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
default: printf("\nInvalid choice..");
}
}while(choice<=3);

return 0;
}

void push()
{
int num;
if(top==4)
printf("\n Stack is Full..");
else
{
printf("\nEnter the element to be inserted: ");
scanf("%d", &num);
top=top+1;
stack[top]=num;
}
}
void pop()
{
int num;
if(top==-1)
{
printf("\nStack is Empty..");
}
else
{
num=stack[top];
top=top-1;
printf("\n Deleted element is %d", num);
}

}
void display()
{
int i;
if(top==-1)
printf("\nStack is Empty..");
else
{
for(i=top; i>=0; i--)
printf("\n %d", stack[i]);
}

18. Queue Operations –

#include <stdio.h>
#include <stdlib.h>

int queue[5];
int front, rear;

int main()
{
int choice;
//intiqueue(); //function call
front=rear=-1;

do
{
printf("\n------ Menu ------\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1: insert();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong choice");
}
}while(choice != 4);
return 0;
}
/*void intiqueue()
{
front=rear=-1;
}*/
void insert()
{
int info;
if (rear <4)
{
printf("\nEnter element: ") ;
scanf("%d", &info);
if (front==-1)
{
front=rear=0;
}
else
{
rear=rear+1;
}
queue[rear]=info;

}
else
printf("\nQueue is Full");

}
void dequeue()
{
int info;
if(front != -1)
{
info = queue[front];
if (front == rear)
{
front=rear=-1;
}
else
front=front+1;
printf("\nThe Deleted Element is: %d",info);
}
else
printf("\nQueue is Empty");
}
void display()
{
int i;
if(front == -1)
{
printf("\nQueue is Empty");
}
else
{
for(i=front; i<=rear; i++)
{
printf("%d\n", queue[i]);
}
}
}

19. Linked List -

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *link;

}*start;

void createList(int n);


void insertAtBeg(int data);
void insertAtEnd(int data);
void insertAtLoc(int data, int loc);
void deleteAtBeg();
void deleteEnd();
void deleteAtLoc(int loc);
void display();

int main()
{
int n, data, loc;

printf("\nEnter the total number of element to be inserted in list: ");


scanf("%d", &n);
createList(n);

printf("\nData in the list:");


display();

printf("\n\nEnter the data at the beginning of the list: ");


scanf("%d", &data);
insertAtBeg(data);

printf("\nData in the list:\n");


display();

printf("\n\nEnter the data at the end of the list: ");


scanf("%d", &data);
insertAtEnd(data);

printf("\nData in the list:\n");


display();

printf("\n\nEnter the location of the list to which data is to be inserted: ");


scanf("%d", &loc);
printf("\n\nEnter the data at location %d of the list: ", loc);
scanf("%d", &data);
insertAtLoc(data, loc);
printf("\nData in the list:\n");
display();

printf("\n\n.......Deletion Operation.......");
printf("\n\nDelete the first element from the list... ");
deleteAtBeg();
printf("\nNew list after deletion:\n");
display();

printf("\n\nDelete the last element from the list... ");


deleteEnd();
printf("\nNew list after deletion:\n");
display();

printf("\n\nEnter the location of the list from which data is to be deleted: ");
scanf("%d", &loc);
deleteAtLoc(loc);
printf("\nNew list after deletion:\n");
display();

return 0;
}

void createList(int n)
{
struct node *newNode, *temp;
int data, i;

start = (struct node*)malloc(sizeof(struct node));


if(start == NULL)
printf("\nUnable to allocate Memory");
else
{
printf("\nEnter the data of Node1: ");
scanf("%d", &data);

start->data = data;
start->link = NULL;

temp = start;

for(i=2; i<=n; i++)


{
newNode= (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("\nUnable to allocate Memory");
break;
}
else
{
printf("\nEnter the data of Node%d: ",i);
scanf("%d", &data);

newNode->data = data;
newNode->link = NULL;

temp->link = newNode;
temp = temp->link;
}

}
printf("\nSingle List Created Successfully");
}
}
void insertAtBeg(int data)
{
struct node *newNode;
newNode= (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("\nUnable to allocate Memory");

}
else
{
newNode->data = data;
newNode->link = start;

start=newNode;
printf("\nData inserted Successfully");

}
}
void insertAtEnd(int data)
{
struct node *newNode;
newNode= (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("\nUnable to allocate Memory");

}
else
{
newNode->data = data;
newNode->link = NULL;

struct node *temp = start;


while(temp->link != NULL){
temp = temp->link;
}

temp->link = newNode;
printf("\nData inserted Successfully");

}
}
void insertAtLoc(int data, int loc)
{
struct node *newNode;
newNode= (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("\nUnable to allocate Memory");

}
else
{
newNode->data = data;

struct node *temp = start;


for(int i = 1; i < loc-1; i ++)
{
temp=temp -> link;
}
newNode ->link=temp->link;
temp -> link = newNode;

printf("\nData inserted Successfully");

}
}
void deleteAtBeg()
{
int val;

struct node *temp;


temp = start;

val = temp->data;

if(temp->link==NULL)
{

start = NULL;
free(temp);
printf("\n\nValue %d, deleted \n",val);
//return;
}
if(temp!=NULL)
{

start = temp->link;

printf("\n\nValue %d, deleted \n",val);

free(temp);

void deleteEnd()
{ int val;

struct node *temp, *ptr;

temp=start;
while(temp->link != NULL)
{
ptr = temp;
temp = temp->link;
}
val = temp->data;
ptr->link = NULL;
free(temp);
printf("\n\nValue %d, deleted \n",val);

void deleteAtLoc(int loc)


{ int val;

struct node *temp, *ptr;

temp=start;
for(int i=0; i<loc-1; i++)
{
ptr = temp;
temp = temp->link;
if(temp == NULL)
{
printf("\nThere are less than %d elements in the list.. \n",loc);
break;
}
}
val = temp->data;
ptr->link = temp->link;
free(temp);
printf("\n\nValue %d, deleted \n",val);
}

void display()
{
struct node *temp;
if(start == NULL)
printf("\nList is empty");

else{
temp = start;
while(temp!=NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->link;
}
}
}

You might also like