Data Structure Lab Program
Data Structure Lab Program
Data Structure Lab Program
#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;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//strlen(), strcpy(),strcat(),strcmp()
int main()
{
char str1[30], str2[30];
int x, l;
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");
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
strrev(s);
return 0;
}
int main()
{
int a[50], i, num, pos, size;
a[pos-1]=num;
size++;
int main()
{
int a[50], i, num, pos, size;
size--;
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;
}
}
}
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);
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);
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];
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
multiply[i][j] = sum;
sum = 0;
}
}
printf("\n");
}
}
return 0;
}
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
#include<stdio.h>
#include<conio.h>
void main(){
int list[20],size,i,item;
#include <stdio.h>
#include <stdlib.h>
int main()
{
int first, last, mid, item, a[100], size, i;
first=0;
last= size-1;
mid = (first+last)/2;
mid=(first+last)/2;
}
if(first >last)
printf("\nElement not found..\n\n");
return 0;
}
#include <stdio.h>
int main()
{
int a[100], n, i, j, 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++;
}
return 0;
}
#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++)
{
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;
}
#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]);
}
#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]);
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
}*start;
int main()
{
int n, data, loc;
printf("\n\n.......Deletion Operation.......");
printf("\n\nDelete the first element from the list... ");
deleteAtBeg();
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->data = data;
start->link = NULL;
temp = start;
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;
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;
}
}
void deleteAtBeg()
{
int val;
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;
free(temp);
void deleteEnd()
{ int val;
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);
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;
}
}
}