DS File-Anmol (It-3)
DS File-Anmol (It-3)
DS File-Anmol (It-3)
DS FILE – 3 SEMESTER
ANMOL GANDHI
IT-3
01276803120
INDEX
void Insert(int[],int,int,int);
int main()
{
int A[MAX],N,k,item;
printf("Enter the number of elements of 1-D array:\n");
scanf("%d",&N);
for(int i=1;i<=N;i++)
{
printf("Enter the element at A[%d]:\n",i);
scanf("%d",&A[i]);
}
for(int j=1;j<=N;j++)
{
printf("%d ",A[j]);
}
printf("\nEnter the element which is to be inserted in 1-D array:\n");
scanf("%d",&item);
printf("Enter the position at which insertion is to be done:\n");
scanf("%d",&k);
Insert(A,N,k,item);
return 0;
}
for(int i=1;i<=N;i++)
{
printf("%d ",A[i]);
}
}
OUTPUT:
CODE:
#define MAX 10
#include<stdio.h>
#include<conio.h>
void Delete(int[],int,int);
int main()
{
int A[MAX],N,k,item;
printf("Enter the number of elements of 1-D array:\n");
scanf("%d",&N);
for(int i=1;i<=N;i++)
{
printf("Enter the element at A[%d]:\n",i);
scanf("%d",&A[i]);
}
for(int j=1;j<=N;j++)
{
printf("%d ",A[j]);
}
printf("\nEnter the position from which deletion is to be done:\n");
scanf("%d",&k);
Delete(A,N,k);
return 0;
}
OUTPUT:
CODE:
#define MAX 10
#include<stdio.h>
#include<conio.h>
int main()
{
int A[MAX],N;
int pos;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element A[%d]:",i);
scanf("%d",&A[i]);
}
pos=Lin_Search(A,N);
if(pos==-1)
printf("Element not found \\\n");
else
printf("Element found at position %d\n" ,pos+1);
getch();
return loc;
}
OUTPUT:
QUES 4. Write a program to implement Binary Searching.
CODE:
#include<iostream>
using namespace std;
while(s<=e){
int mid = (s+e)/2;
if(arr[mid] == key){
return mid;
}
else if(arr[mid] > key){
e = mid - 1;
}
else{
s = mid + 1;
}
}
return -1;
}
int main(){
int arr[1000] ;
int n;
printf("Enter the number of elements of 1-D array::\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the element B[%d]:",i);
scanf("%d",&arr[i]);
}
int key;
printf("Enter the data to be searched:");
scanf("%d",&key);
printf("\n");
return 0;
}
OUTPUT:
QUES 5. Write a program to implement Bubble Sort.
CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void BSORT(int[],int);
int main ()
{
int Arr[MAX],N;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element Arr[%d]:",i);
scanf("%d",&Arr[i]);
}
BSORT(Arr,N);
printf("Elements after bubble sorting :\n");
for( int k=0; k<N ; k++)
{
printf("%d ",Arr[k]);
}
return 0;
}
OUTPUT:
QUES 6. Write a program to check whether the input matrix is
sparse matrix or not.
CODE:
#include<stdio.h>
#include<conio.h>
void Check_Sparse(int[10][10],int,int);
int main ()
{
int Arr[10][10],M,N;
printf("Enter the number of rows and columns of 1-D array\n");
scanf("%d\n%d",&M,&N);
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
printf("Enter the element Arr[%d][%d]:",i,j);
scanf("%d",&Arr[i][j]);
}
}
for(int k=0;k<M;k++)
{
for(int l=0;l<N;l++)
{
printf("%d ",Arr[k][l]);
}
printf("\n");
}
Check_Sparse(Arr,M,N);
return 0;
}
printf("%d",sparse_ctr);
OUTPUT:
QUES 7. Program to implement matrix multiplication.
CODE:
#define MAX 10
#include<stdio.h>
#include<conio.h>
int main()
{
int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX],R,C,i,j;
printf("Enter the number of rows and columns for 2-D array \n");
scanf("%d\n%d",&R,&C);
printf("--------FIRST ARRAY----------\n");
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
{
printf("Enter element -> A[%d][%d]:",i,j);
scanf("%d",&A[i][j]); //inputting the array elements
}
}
printf("\n");
printf("-------SECOND ARRAY----------\n");
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
{
printf("Enter element -> B[%d][%d]:",i,j);
scanf("%d",&B[i][j]); //inputting the array elements
}
}
Multiply(A,B,C,R,C);
getch();
}
OUTPUT:
QUES 8. Implementation of some operations on a stack
Push
Pop
Traversal
CODE:
#include<stdio.h>
#include<conio.h>
void push();
void pop();
void display();
int stack[100],n,i,x;
int top=-1; // first element of Stack
int main()
{
void push()
{
if(top==n-1)
{
printf("Overflow...STACK is FULL\n");
}
else
{
printf("Enter a value to pe pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf("Underflow....STACK is empty\n");
}
else
{
printf("The popped element is %d\n",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("The elements are as follows:\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
printf("Enter another choice\n");
}
else
{
printf("Stack is empty.....\n");
}
}
OUTPUT:
void insert();
void delete();
void display();
int queue[100],n,i,x;
int front=-1,rear=-1; // first and last elements of Queue respectively
void main()
{
printf("Enter the size of QUEUE\n");
scanf("%d",&n);
printf("\n\t QUEUE OPERATIONS USING ARRAY");
printf("\n\t ----------------------------");
printf("\n\t 1.INSERT\n\t 2.DELETE\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice;
do
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert(); break;
case 2 : delete(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n");
void insert()
{
if(rear==n-1)
{
printf("Overflow...Queue is FULL\n");
}
else if ( rear == -1 && front == -1)
{
front=rear=0; // first insert
printf("Enter a value to pe inserted:");
scanf("%d",&x);
queue[rear]=x;
}
else
{
rear++;
printf("Enter a value to pe inserted:");
scanf("%d",&x);
queue[rear]=x;
}
}
void delete()
{
if(front==-1 && rear==-1)
{
printf("Underflow...Queue is empty\n");
}
else if( front == rear)
{
front=rear=-1;
printf("The deleted element is %d\n",queue[front]);
}
else
{
front++;
printf("The deleted element is %d\n",queue[front]);
}
}
void display()
{
if(rear>=0)
{
printf("The elements are as follows:\n");
for(i=front;i<=rear;i++) // FIFO
{
printf("%d\n",queue[i]);
}
printf("Enter another choice\n");
}
else
{
printf("Queue is empty.....\n");
}
}
OUTPUT:
QUES 10. Implementation of operations on a circular queue
Insertion
Deletion
Traversing
CODE:
#include<stdio.h>
#include<conio.h>
int insert();
int delete();
int display();
int queue[100],n,i,x;
int front=-1,rear=-1; // first and last elements of Queue respectively
int main()
{
printf("Enter the size of CIRCULAR QUEUE\n");
scanf("%d",&n);
printf("\n\t QUEUE OPERATIONS USING ARRAY");
printf("\n\t ----------------------------");
printf("\n\t 1.INSERT\n\t 2.DELETE\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice;
do
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert(); break;
case 2 : delete(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n");
int insert()
{
if(front==(rear+1)%n)
{
printf("Overflow...Queue is FULL\n");
}
else if (front== -1 && rear == -1)
{
front=rear=0; // first insert
printf("Enter a value to pe inserted:");
scanf("%d",&x);
queue[rear]=x;
}
else
{
rear=(rear+1)%n;
printf("Enter a value to pe inserted:");
scanf("%d",&x);
queue[rear]=x;
}
}
int delete()
{
if(front==-1)
{
printf("Underflow...Queue is empty\n");
}
else if( front == rear)
{
front=rear=-1;
printf("The deleted element is %d\n",queue[front]);
}
else
{
printf("The deleted element is %d\n",queue[front]);
front=(front+1)%n;
}
}
int display()
{
if(front==-1 && rear==-1)
{
printf("Underflow...No queue inserted\n");
}
else
{
for(int i=front;;i=i+1%n)
{
printf("%d\n",queue[i]);
if(i==rear)
break;
}
printf("%d\n",queue[i]);
}
return 0;
}
OUTPUT:
QUES 11. Implement the following operations on a linked list ->
Insert a node at beginning
Insert a node at end
Delete a node from beginning
Delete a node from end
Traversing through the list
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void insert_at_begin(int);
void insert_at_end(int);
void traverse();
void delete_from_begin();
void delete_from_end();
int count=0;
int main()
{
int i,data;
for(;;)
{
printf("1.Insertion at begin\n");
printf("2.Insertion at end\n");
printf("3.Deletion from begin\n");
printf("4.Deletion from end\n");
printf("5.Traversing thorugh the linked list\n");
printf("6.Exit\n");
scanf("%d",&i);
if(i==1)
{
scanf("%d",&data);
insert_at_begin(data);
}
else if(i==2)
{
scanf("%d",&data);
insert_at_end(data);
}
else if(i==3)
delete_from_begin();
else if(i==4)
delete_from_end();
else if(i==6)
break;
else
printf("Enter valid input\n");
}
return 0;
}
void insert_at_begin(int x)
{
struct node* t;
t=(struct node*)malloc(sizeof(struct node));
t-> data = x;
count++;
if(start==NULL)
{
start=t;
start->next= NULL;
return;
}
else
{
t -> next = start;
start=t;
}
}
if(start==NULL)
{
start=t;
start->next=NULL;
return;
}
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
temp->next=t;
t->next=NULL;
}
void delete_from_begin()
{
struct node*t;
int n;
if(start==NULL)
{
printf("Linked list is empty...\n");
return;
}
n= start->data;
t=start->next;
free(start);
start=t;
count--;
void delete_from_end()
{
struct node *t,*u;
int n;
if(start==NULL)
{
printf("Linked list is empty...\n");
return;
}
count--;
if(start->next==NULL)
{
n=start->data;
free(start);
start=NULL;
printf("%d deleted successfully\n",n);
}
t=start;
while(t->next!=NULL)
{
u=t;
t=t->next;
}
n=t->data;
u->next=NULL;
free(t);
printf("%d deleted successfully\n",n);
}
void traverse()
{
struct node *t;
t=start;
if ( t==NULL)
{
printf("Empty linked list...nothing to search\n");
return;
}
printf("%d nodes are present in the linked list\n",count);
void insert_at_begin(int);
void insert_at_end(int);
void traverse();
void delete_from_begin();
void delete_from_end();
int count=0;
int main()
{
int i,data;
for(;;)
{
printf("1.Insertion at begin\n");
printf("2.Insertion at end\n");
printf("3.Deletion from begin\n");
printf("4.Deletion from end\n");
printf("5.Traversing thorugh the linked list\n");
printf("6.Exit\n");
scanf("%d",&i);
if(i==1)
{
scanf("%d",&data);
insert_at_begin(data);
}
else if(i==2)
{
scanf("%d",&data);
insert_at_end(data);
}
else if(i==3)
delete_from_begin();
else if(i==4)
delete_from_end();
else if(i==6)
break;
else
printf("Enter valid input\n");
}
return 0;
}
void insert_at_begin(int x)
{
struct node* t;
t=(struct node*)malloc(sizeof(struct node));
t-> data = x;
count++;
if(start==NULL)
{
start=t;
start->next= NULL;
return;
}
else
{
t -> next = start;
start=t;
}
}
if(start==NULL)
{
start=t;
start->next=NULL;
return;
}
temp=start;
while(temp->next!=start)
{
temp=temp->next;
}
temp->next=t;
t->next=NULL;
}
void delete_from_begin()
{
struct node*t;
int n;
if(start==NULL)
{
printf("Linked list is empty...\n");
return;
}
n= start->data;
t=start->next;
free(start);
start=t;
count--;
void delete_from_end()
{
struct node *t,*u;
int n;
if(start==NULL)
{
printf("Linked list is empty...\n");
return;
}
count--;
if(start->next==NULL)
{
n=start->data;
free(start);
start=NULL;
printf("%d deleted successfully\n",n);
}
t=start;
while(t->next!=start)
{
u=t;
t=t->next;
}
n=t->data;
u->next=NULL;
free(t);
printf("%d deleted successfully\n",n);
}
void traverse()
{
struct node *t;
t=start;
if ( t==NULL)
{
printf("Empty linked list...nothing to search\n");
return;
}
printf("%d nodes are present in the linked list\n",count);
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* next;
};
struct node *top;
void push();
void pop();
void display();
int n;
void main()
{
printf("\n\t LINKED LIST IMPLEMENTATION USING STACK");
printf("\n\t ----------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice=0;
while(choice!=4)
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : push(); break;
case 2 : pop(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n"); break;
void push()
{
int info;
struct node * ptr= (struct node*) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("Overlow memory is full\n");
}
else
{
printf("Enter the value\n");
scanf("%d",&info);
if(top == NULL)
{
ptr->info=info;
ptr->next=NULL; // first insertion
top=ptr;
}
else
{
ptr->info=info;
ptr->next=top;
top=ptr;
}
printf("Value pushed\n");
}
void pop()
{
else
{
item=top->info;
ptr=top;
top=top->next;
free(ptr);
printf("Value popped\n");
}
}
void display()
{
struct node*ptr;
ptr=top;
if(ptr==NULL)
{
printf("Empty stack...");
}
else
{
printf("Stack elements are as follow\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->next;
}
}
}
OUTPUT:
QUES 14. Program to implement a queue using linked list.
CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node* next;
}*front,*rear;
int insert();
int delete();
int display();
int n;
int main()
{
printf("\n\t LINKED LIST IMPLEMENTATION USING QUEUE");
printf("\n\t ----------------------------");
printf("\n\t 1.INSERT\n\t 2.DELETE\n\t 3.DISPLAY\n\t 4.EXIT\n");
int choice=0;
while(choice!=4)
{
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert(); break;
case 2 : delete(); break;
case 3 : display(); break;
case 4 : printf("Exit..\n"); break;
default : printf("Enter a valid choice 1/2/3/4\n");
}
}
}
int insert()
{
struct node *ptr=(struct node*)malloc(sizeof(struct node));
int item;
if(ptr==NULL)
{
printf("Overflow..memory is full");
}
else
{
printf("Enter the item\n");
scanf("%d",&item);
ptr->item=item;
if( front == NULL && rear==NULL)
{
front=rear=ptr; // first insertion
front->next=NULL;
rear->next=NULL;
}
else
{
rear->next=ptr;
rear=ptr;
rear->next=NULL;
}
printf("Item inserted\n");
}
}
int delete()
{
struct node *ptr;
if(front == NULL && rear==NULL)
{
printf("Underflow..memory is empty");
}
else
{
if(front==rear)
{
ptr=front;
front=rear=NULL;
free(ptr);
}
else
{
ptr=front;
front=front->next;
free(ptr);
}
printf("Item deleted\n");
}
}
int display()
{
struct node* ptr;
ptr=front;
if(front == NULL && rear==NULL )
{
printf("Queue is empty...");
}
else
{
printf("Queue elements are as follows\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->item);
ptr=ptr->next;
}
}
return 0;
}
OUTPUT:
Ques 15: Program showing different tree traversals of a tree
Preorder tree traversal
Inorder tree traversal
Postorder tree traversal
Code:
#include<stdio.h>
#include<stdlib.h>
node* create()
{
node *p;
int x;
printf("Enter data(-1 for no data ) \n");
scanf("%d",&x);
if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->data=x;
return p;
}
void inorder(node * t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d\n",t->data);
inorder(t->right);
}
}
void postorder(node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%d\n",t->data);
}
}
int main ()
{
node *root;
root=create();
int choice;
do
{
printf("\t\t----------MENU-----------\n");
printf("\t\t1.Preorder tree traversal\n");
printf("\t\t2.Inorder tree traversal\n");
printf("\t\t3.Postorder tree traversal\n");
printf("\t\t4.Exit\n");
return 0;
}
OUTPUT:
Ques 16: Write a program to implement Heap Sort.
Code:
#include<stdio.h>
void heapify(int arr[], int n, int i)
{
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && arr[left]>arr[largest])
largest=left;
if(right<n && arr[right]>arr[largest])
largest=right;
if (largest!=i)
// if root node is not largest then swap a[i] and a[largest]
{
int temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
heapify(arr,n,largest);
}
}
int temp=a[0];
a[0]=a[i];
a[i]=temp;
heapify(a,i,0);
}
}
int main()
{
int a[]={23,45,44,87,90,12,36,77};
int n=sizeof(a)/sizeof(a[0]);
printf("Array before sorting:-\n");
printArray(a,n);
Heapsort(a,n);
printf("\nArray after sorting:-\n");
printArray(a,n);
return 0;
}
OUTPUT:
OUTPUT:
QUES 18. Write a program to implement Selection Sort.
CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void SSORT(int[],int);
int main ()
{
int Arr[MAX],N;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element Arr[%d]:",i);
scanf("%d",&Arr[i]);
}
SSORT(Arr,N);
printf("Elements after selection sorting :\n");
for( int k=0; k<N ; k++)
{
printf("%d ",Arr[k]);
}
getch();
}
void SSORT(int A[], int n)
{
int i,j,pos,temp,small;
for(i=0;i<n-1;i++)
{
small=A[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(small>A[j])
{
small=A[j];
pos=j;
}
}
temp=A[i];
A[i]=A[pos];
A[pos]=temp;
}
}
OUTPUT:
QUES 19. Write a program to implement Insertion Sort.
CODE:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void ISORT(int[],int);
int main ()
{
int Arr[MAX],N;
printf("Enter the number of elements of 1-D array\n");
scanf("%d",&N);
for(int i=0;i<N;i++)
{
printf("Enter the element Arr[%d]:",i);
scanf("%d",&Arr[i]);
}
ISORT(Arr,N);
printf("Elements after insertion sorting :\n");
for( int k=0; k<N ; k++)
{
printf("%d ",Arr[k]);
}
getch();
}
A[j+1]=temp;
}
}
OUTPUT:
OUTPUT: