0% found this document useful (0 votes)
40 views28 pages

DS Lab Codes

Uploaded by

si546844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views28 pages

DS Lab Codes

Uploaded by

si546844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structure Lab programs

Directed by Pappu

Topic No. Topic Title Page No.


1 Array Insertion 2
2 Array Deletion 3
3 Binary Search 4
4 Linear Search 5
5 Linked List 6
6 Tree 12
7 Stack 14
8 Queue 17
9 Factorial Using Recursion 19
10 Sum of Fibonacci Series 19
11 Fibonacci Using Recursion 20
12 CGPA 21
13 Palindromic Number 22
14 LCM-GCD 23
15 ODD-EVEN 23
16 Bubble Sort 24
17 Selection Sort 25
18 Insertion Sort 26
19 Quick sort 27
Array_Insertion:
#include<stdio.h>
int main (){
int a[100],i,j,pos,x,value,ctr=0;
for(i=0;;i++)
{
printf("Enter element: ");
scanf("%d",&a[i]);
printf("If you want add more value press 1 otherwise
press zero: ");
scanf("%d",&x);
if(x==0) break;
}
printf("\nYour array: ");
for(j=0;j<=i;j++)
printf("%d ",a[j]);
printf("\nEnter the position to be inserted: ");
scanf("%d",&pos);
printf("\nEnter value to be inseted: ");
scanf("%d",&value);
ctr=i;
while(ctr>=pos-1)
{
a[ctr+1]=a[ctr];
ctr--;
}
a[pos-1]=value;
printf("\nArray after insertion: ");
for(j=0;j<=i+1;j++)
printf("%d ",a[j]);
return 0; }
Array_Deletion:
#include<stdio.h>
int main (){
int a[100],i,j,pos,x,value,ctr=0;
for(i=0;;i++)
{
printf("Enter element: ");
scanf("%d",&a[i]);
printf("If you want add more value press 1 otherwise
press zero: ");
scanf("%d",&x);
if(x==0) break;
}
printf("\nYour array: ");
for(j=0;j<=i;j++)
printf("%d ",a[j]);

printf("\nEnter the position to be deleted: ");


scanf("%d",&pos);
ctr=pos-1;
while(ctr<=i-1)
{
a[ctr]=a[ctr+1];
ctr++;
}
printf("\nArray after deletion: ");
for(j=0;j<=i-1;j++)
printf("%d ",a[j]);
return 0;
}
Binary Search:
#include<stdio.h>
int main(){
int array[100],j,n,value,i, left,right,mid,target=0,k;
for (i=0;;i++){
printf("Enter element: ");
scanf("%d",&array[i]);
printf("If you want to add more values press 1
otherwise press 0: ");
scanf("%d",&k);
if(k==0) break;
}
n=i;
printf("\nYour array : ");
for(j=0;j<=n;j++){
printf("%d",array[j]);
if(j<n) printf(",");
else printf(".");
}
printf("\n\nEnter the value to be found using a binary
search: ");
scanf("%d",&value);
left=0;
right=n-1;
mid=(left+right)/2;
while(left<=right)
{
if(array[mid]==value){
printf("The value found at %d index.",mid);
return 0;
}
else if (array[mid]>value){
right=mid-1;
mid=(left+right)/2;
}
else {
left=mid+1;
mid=(left+right)/2;
}
}
printf("The value is not present.");
return 0;
}

Linear Search:
#include<stdio.h>
int main ()
{
int i,item,a[]={4,1,7,5,9,19,3,20,-5,42};
printf("Enter the item you want to search for:");
scanf("%d",&item);
for(i=0;i<=10;i++)
{
if(a[i]==item)
{printf("The item %d found at index %d.",item,i);
break;
}
}
}
Linked List:

#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node*next;
};
typedef struct Node node;
node*start=NULL;
node*getNode()
{
node*newnode;
newnode=(node*)malloc(sizeof(node));
printf("Enter Data: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
return newnode;
}

void creatList (int n)


{ int i;
for (i=0;i<n;i++)
{
node*newnode=getNode();
if(start==NULL)
{
start=newnode;
}
else {
node*temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
}
}
void traversal()
{
node*temp=start;
while(temp!=NULL)
{
printf("%d -> ",temp->data);
temp=temp->next;
}
}
void insert_beg()
{
node*newnode=getNode();
if(start==NULL) start=newnode;
else {
newnode->next=start;
start=newnode;
printf("After insert at begining point, New linked
list: ");
traversal();
}
}
void insert_end()
{
node*newnode=getNode();
node*temp;
if(start==NULL) start=newnode;
else {
temp=start;
while(temp->next != NULL)
temp = temp->next;
temp->next=newnode;
}
printf("After insert at END, New linked list: ");
traversal();
}
void insert_pos(int n)
{
int pos,count=1;
printf("Enter the position to be inserted: ");
scanf("%d",&pos);
node*temp,*prev,*newnode=getNode();
if(pos>1&&pos<n){
temp=prev=start;
while(count<pos)
{
prev=temp;
temp=temp->next;
count++;
}
prev->next=newnode;
newnode->next=temp;
}
printf("Your linked list after insert at %d position:
",pos);
traversal();
}
void delete_beg()
{
if(start == NULL) {
printf("\nThe Linked List is Empty.\n");
}
else {
node*temp=start;
start=temp->next;
free(temp);
}
printf("Node deleted successfully.\n");
printf("Linked List After Deletion of Beginning Node:
");
traversal();
}
void delete_end()
{
if(start==NULL) printf("Empty List.\n");
else {
node*prev,*temp=start;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
printf("Node deleted successfully.\n");
printf("Your linked list after delete the node: ");
traversal();
}

}
void delete_spec(int n)
{
int pos,ctr=1;
if(start==NULL)
{
printf("Empty List.\n");
return ;
}
else {
node*temp,*prev;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos>n)
{
printf("Node does not exist.\n");
return ;
}
if(pos>1)
{
temp=prev=start;
while(ctr<pos)
{
prev=temp;
temp=temp->next;
ctr++;
}
prev->next=temp->next;
free(temp);
printf("Node Deleted successfully.\n");
printf("Your Linked list after delete the node:
");
traversal();
}
}
}
int main ()
{
int n,x;
printf("\nEnter the number of node: ");
scanf("%d",&n);
printf("\n");
creatList(n);
printf("\nLinked List is : ");
traversal();
printf("\nIf you want to add a node at begining then
press 1: ");
printf("\nIf you want to add a node at END then press 2:
");
printf("\nIf you want to add a node at specific position
then press 3: ");
printf("\nIf you want to delete a node at begining then
press 4: ");
printf("\nIf you want to delete a node at End then press
5: ");
printf("\nIf you want to delete a specificnode then
press 6: ");
scanf("%d",&x);
switch(x){
case 1: insert_beg();
case 2: insert_end();
case 3: insert_pos(n);
case 4: delete_beg();
case 5: delete_end();
case 6: delete_spec(n);}
return 0; }
Tree Traversal:

#include<stdio.h>
#include<stdlib.h>
struct Node {
char data;
struct Node*left;
struct Node*right;
};
typedef struct Node node;
node* insert(char x )
{
node *temp = (node*)malloc(sizeof(node));
temp->data=x;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void Pre_order(node*t)
{
if(t==NULL) return;
printf(" %c",t->data);
Pre_order(t->left);
Pre_order(t->right);
}
void Post_order(node*t)
{
if(t==NULL) return;
Pre_order(t->left);
Pre_order(t->right);
printf(" %c",t->data);
}
void In_order(node*t)
{
if(t==NULL) return;
Pre_order(t->left);
printf(" %c",t->data);
Pre_order(t->right);
}
int main() {
node* nA=insert('A');
node* nB=insert('B');
node* nC=insert('C');
node* nD=insert('D');
node* nE=insert('E');
node* nG=insert('G');
node* nH=insert('H');
node* nK=insert('K');
node* nL=insert('L');
node* nM=insert('M');

nA->right=nC;
nC->left=nE;
nA->left=nB;
nB->left=nD;
nD->left=nG;
nD->right=nH;
nH->left=nL;
nH->right=nM;
nG->right=nK;
printf("\nPreOrder Traversal: ");
Pre_order(nA);
printf("\nInOrder Traversal: ");
In_order(nA);
printf("\nPostOrder Traversal: ");
Post_order(nA);
printf("\n\n");
free(nA); free(nB);
free(nC); free(nD); free(nE); free(nG);
free(nH); free(nK); free(nL); free(nM);
printf("\n");
}

Note: If the elements of tree are integer number, then you have to use %d instead of %c.
If the elements of tree are float number, then you have to use %f instead of %c.

STACK:
#include<stdio.h>
#define N 100
int stack[N];
int top = -1;
void push()
{
int x;
printf("Enter the data: ");
scanf("%d",&x);
if(top==N-1) printf("Over Flow .");
else {
top++;
stack[top]=x;
}
}
void pop()
{
int x;
if(top == -1) printf("Underflow .\n");
else {
x=stack[top];
top--;
printf("Popped element %d \n",x);
}
}
void peek()
{
if(top==-1) printf("Stack is empty.\n");
else printf("Top element(using peek)= %d",stack[top]);
}
void display()
{
int i=top;
while(i>=0)
{
printf("%4d",stack[i]);
i--;
}
}
int main ()
{
int size,k=0,n;
printf("Enter the size of stack: ");
scanf("%d", &size);
while(k<size)
{
push();
k++;
}
printf("\nElements in Stack are:\n");
display();
printf("\nEnter what operation you want to
perform?\n");
printf("If you want push operation press 1: \n");
printf("If you want pop operation press 2: \n");
printf("If you want peek operation press 3: \n");
scanf("%d",&n);
switch(n)
{
case 1: {
push();
break;}
case 2: {
pop();
break;
}
case 3: {
peek();
break;
}
}
printf("\nAfter operation Elements in Stack are:\n");
display():
}
Queue:
#include<stdio.h>
#define N 5
int queue[N];
int rear=-1;
int front=-1;
void enqueue()
{
int x;
printf("Enter element to be added : ");
scanf("%d",&x);
if(rear==N-1) printf("\nOverflow Condition.\n");
else if(rear==-1 & front==-1)
{
front=rear=0;
queue[rear]=x;
}
else {
rear++;
queue[rear]=x;
}
}
void dequeue()
{
if(rear==-1&front==-1) printf("\nUnderFlow.\n");
else if (front==rear) front=rear=-1;
else front++;
}
void display()
{
int i=front;
if(i==-1) printf("\nQueue if Empty.\n");
else {
while(i<rear+1)
{
printf("%d-> ",queue[i]);
i++;
}
}
}
int main ()
{
int size,j=1;
printf("Enter the size of Queue: ");
scanf("%d",&size);
while(j<=size)
{
enqueue();
j++;
}
printf("\nElements in Queue are:\n");
display();
dequeue();
printf("\nDeleting first element Queue is: ");
display();
return 0;
}
Factorial:
#include<stdio.h>
int factorial(int n)
{
if(n==0||n==1) return 1;
else return n*factorial(n-1);
}
int main ()
{
int n;
printf("Enter the number: ");
scanf("%d",&n);
printf("Factorial of %d is %d",n,factorial(n));
}

Sum of Fibonacci Series:


#include<stdio.h>
int main ()
{
int n,a=0,b=1,c,i,sum=0+1;
printf("Enter the number of terms: ");
scanf("%d",&n);
for(i=3;i<=n;i++){
c=a+b;
sum=sum+c;
a=b;
b=c; }
printf("\nSum of %d terms is =%d",n,sum);
}
Fibonacci Series Using Recursion:

#include<stdio.h>
int sum=0;
int fibo(int n)
{
if(n<=1) return n;
else return fibo(n-1)+fibo(n-2);
}
void display_fibo(int n)
{ int i,m;
printf("Fibonacci Series: ");
for(i=0;i<n;i++)
{
m=fibo(i);
printf("%d ",m);
sum=sum+m;
}
}
int main ()
{
int n;
printf("Enter the number of terms: ");
scanf("%d",&n);
display_fibo(n);
printf("\nSum of Series: %d",sum);
return 0;
}
CGPA:
#include<stdio.h>
float gPoint(float mark) {
if (mark >= 80 && mark < 85) return 4.00;
else if (mark >= 75 && mark < 80) return 3.75;
else if (mark >= 70 && mark < 75) return 3.50;
else if (mark >= 65 && mark < 70) return 3.25;
else if (mark >= 60 && mark < 65) return 3.00;
else if (mark >= 55 && mark < 60) return 2.75;
else if (mark >= 50 && mark < 55) return 2.50;
else if (mark >= 45 && mark < 50) return 2.25;
else if (mark >= 40 && mark < 45) return 2.00;
else return 0.00;
}
int main ()
{
int gCredit,n,sumCredit=0,i;
float sum=0,mark,k,CGPA;
printf("Enter the number of subjects: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Marks in subject (Out of 100): ");
scanf("%f",&mark);
k= gPoint(mark);
printf("Your gread point %.2f",k);
printf("\nEnter credit hours: ");
scanf("%d",&gCredit);
sumCredit=sumCredit+gCredit;
sum=sum+(k*gCredit);
}
printf("\nTotal Credit: %d",sumCredit);
printf("\nTotal Point: %.2f",sum);
CGPA= sum/sumCredit;
printf("\nYour CGPA: %0.2f",CGPA);
}

Palindromic Number:
#include<stdio.h>
int main()
{
int n,s=0,N,a;
printf("Enter the number to check Palendrom or Not:");
scanf("%d",&n);
N=n;
while(n!=0)
{
a=(n%10);
s=s*10+a;
n=(n/10);
}
if(N==s)
printf("Given number is a Palendrom Number.");
else
printf("Given number is not a Palendrom Number.");
return 0;
}
LCM-GCD:

#include<stdio.h>
int main ()
{
int num1,num2;
printf("Enter the first number: ");
scanf("%d",&num1);
printf("Enter the second number: ");
scanf("%d",&num2);
int n1=num1,n2=num2;
while(n2!=0)
{
int rem=n1%n2;
n1=n2;
n2=rem;
}
int LCM= (num1*num2)/n1;
printf("GCD of %d and %d is %d.\n",num1,num2,n1);
printf("LCM of %d and %d is %d.\n",num1,num2,LCM);
return 0;
}
Odd_Even:
#include<stdio.h>
int main (){
int num1;
printf("Enter the first number: ");
scanf("%d",&num1);
if(num1%2==0) printf("%d is an Even Number.");
else printf("%d is an Odd number."); }
Bubble Sort:
#include<stdio.h>
int main ()
{
int i,j,n=5,a[5]={-2,3,1,7,0};
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Selection Sort:
#include<stdio.h>
int main ()
{
int i,j,temp,min,n=5,a[5]={7,-1,-3,5,0};
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[i]) min=j;
if(i!=min)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("Sorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Insertion Sort:
#include<stdio.h>
int main ()
{
int i,j,temp,n=5,a[5]={7,-1,-3,5,0};
for(i=0;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0&&a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("Sorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Quick_Sort:
#include<stdio.h>
void quicksort(int *a,int f,int l)
{
int temp,i=f,j=l,pivot=a[(f+l)/2];
while(i<=j){
while(a[i] < pivot) i++;
while(a[j] > pivot) j--;
if(i<=j){
temp = a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
}
if(f<j) quicksort(a,f,j);
if(l>i) quicksort(a,i,l);
}
int main ()
{
int a[]={8,-3,0,-4,7,3},i;
quicksort(a,0,5);
printf("Sorted array is: ");
for(i=0;i<6;i++){
printf("%d ",a[i]);
}
Return 0;
}

You might also like