ds-1 Vikrant
ds-1 Vikrant
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10], i,n;
clrscr();
cout<<"Enter the number of element of array: ";
cin>>n;
cout<<"Enter the value of array: ";
for(i=0;i<n;i++)
{ cin>>a[i];}
cout<<"\nDISPLAY OF ONE DIMMENSIONAL ARRAY: \n";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
getch();}
OUTPUT.
4. PROGRAM OF TWO DIMMENSIONAL
ARRAY.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10];
int i,r,c,j;
clrscr();
cout<<"Enter the number of ROWS AND COLUMN of Array:
";
cin>>r;
cin>>c;
cout<<"Enter the value of array: ";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"\nDISPLAY OF TWO DIMMENSIONAL ARRAY: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
getch();
}
OUTPUT.
5. PROGRAM OF INSERTION IN ARRAY.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,item,loc;
clrscr();
cout<<"Enter the array value : ";
for(i=0;i<6;i++)
{
cin>>a[i];
}
for(i=0;i<6;i++)
{
cout<<"\t"<<a[i];
}
cout<<endl;
for(i=0;i<6+1;i++)
{
cout<<"\t"<<a[i];
}
getch();
}
OUTPUT.
6. PROGRAM OF DELETION IN ARRAY
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],n,item,loc,i,j;
clrscr();
cout<<”DELETION OF ELEMENT IN ARRAY: \n“;
cout<<"enter the number of item: ";
cin>>n;
cout<<"enter the item of array: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
cout<<"\nenter the loc of item which you want to delete: ";
cin>>loc;
for(i=0;i<n;i++)
{
if(i==loc)
{
for(j=loc;j<n-1;j++)
{
a[j]=a[j+1];
}
}
}
for(i=0;i<n-1;i++)
{
cout<<"\t"<<a[i];
}
getch();
}
OUTPUT.
7. PROGRAM OF LINEAR SEARCH .
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,loc,item;
clrscr();
cout<<”LINEAR SEARCH \n”;
cout<<"Enter the SEVEN VALUE OFarray: ";
for(i=0;i<7;i++)
{
cin>>a[i];
}
for(i=0;i<7;i++)
{
cout<<" "<<a[i];
}
cout<<"\nEnter the item which you want search: ";
cin>>item;
for(i=0;i<7;i++)
{
if(a[i]==item)
{
loc=i;
cout<<"Location of item is: "<<loc<<endl;
break;
}
}
getch();
}
OUTPUT.
8. PROGRAM OF BINARY SEARCH.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],n,i,beg,end,mid,item;
clrscr();
cout<<"BINARY SEARCH\n";
cout<<"Enter the element of array: ";cin>>n;
cout<<"Enter the sorted values of array: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
beg=0;end=n-1;
cout<<"Enter the item: ";
cin>>item;
do{
mid=(beg+end)/2;
if(a[mid]==item)
{ cout<<"LOCATION ITEM: "<<mid<<endl;
break;
}
else if(a[mid]>item)
{
end=mid-1;
}
else if(a[mid]<item)
{
beg=mid+1;
}
}
while(beg<=end);
getch();
}
OUTPUT.
9. PROGRAM OF BUBBLE SORT.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,j,n,temp;
clrscr();
cout<<"Enter the number of element: ";
cin>>n;
cout<<"Enter the values of array : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
cout<<"\nDISPLAY OF SORTED ARRAY: \n";
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i]; }
getch();
}
OUTPUT.
10. PROGRAM OF INSERTION SORT.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],n,i,target,j;
clrscr();
cout<<”INSERTED SORTED\n”;
cout<<"Enter the element of array: ";cin>>n;
cout<<"Enter the values of array: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Display of array: "<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];}
for(i=1;i<n;i++)
{
target=a[i];
j=i-1;
while(target<=a[j]&&j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=target;
}
cout<<"\nDISPLAY OF SORRTED ARRAY: "<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
getch();
}
OUTPUT.
11. PROGRAM OF SELECTION SORT.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],n,loc,i,j,temp;
clrscr();
cout<<"Enter the number of element: ";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nDisplay of array: "<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
for(i=0;i<n-1;i++)
{
loc=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[loc])
{
loc=j;} }
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"\nSELECTION SORTED ARRAY: "<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];}
getch();
}
OUTPUT.
12. PROGRAM OF QUICK SORT.
#include<iostream.h>
#include<conio.h>
int i,j,pivot,temp;
pivot=a[p];
i=p;
j=r;
while(1)
while(a[i]<pivot&&a[i]!=pivot)
i++;
while(a[j]>pivot&&a[j]!=pivot)
j--;
if(i<j)
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
return j;
if(p<r)
int q;
q=partition(a,p,r);
quicksort(a,p,q);
quicksort(a,q+1,r);
int i;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
cout<<endl;
void main()
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
display(a,n);
quicksort(a,0,n-1);
display(a,n);
getch();
}
OUTPUT.
13. PROGRAM OF MERGE SORT.
#include<iostream.h>
#include<conio.h>
void merge(int a[],int low,int high,int mid)
{
int
b[100],i,k,n,j;
i=low;
j=mid;
k=low;
n=high;
while(i<mid&&j<n)
{
if(a[i]<a[j])
{
b[k]=a[i];
i++;
k++;
}
else if(a[j]<a[i])
{
b[k]=a[j];
j++;
k++;
}
}
while(i<mid)
{
b[k]=a[i];
i++;
k++;
}
while(j<n)
{
b[k]=a[j];
j++;
k++;
}
}
for(i=0;i<n;i++)
{
a[i]=b[i];
int mid;
if(low<high)
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid,high);
merge(a,low,high,mid);
int i; for(i=low;i<high;i+
+)
cout<<"\t"<<a[i];
}
cout<<endl;}
void main()
int a[20],n,low,high;
clrscr();
cout<<”\tMERGE SORT\n”;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
}
cout<<”Display of unsorted array: \n”;
display(a,0,n);
mergesort(a,0,n);
display(a,0,n);
getch();
}
OUTPUT.
14. PROGRAM OF TRAVERSING AND CREATION OF
LINKED LIST.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
int info;
};
ptr->link=head->link;
while(ptr->link!=NULL)
ptr=ptr->link;
void main()
{
struct node *head;
clrscr();
three=(struct node*)malloc(sizeof(struct
node)); head->link=one;
one->link=two;
two->link=three;
three->link=four;
four->link=NULL;
one->info=10;
two->info=20;
three->info=30;
four->info=40;
traverse(head);
getch();
}
OUTPUT.
15. PROGRAM OF INSERTION AT BEGINNING
OF LINKED LIST.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;};
void traverse(struct node *head)
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct
node)); ptr->link=head->link;
while(ptr->link!=NULL)
{
ptr=ptr->link;
cout<<"\nElement is: "<<ptr->info;}}
void insert(struct node *head)
{
struct node *ptr; //for insert element
ptr=(struct node*)malloc(sizeof(struct
node)); ptr->link=head->link;
ptr->info=5;
head->link=ptr;
}
void main()
{
struct node *head;
struct node *one,*two,*three,*four;
clrscr();
cout<<"Insertion at first element: \n";
head=(struct node*)malloc(sizeof(struct node));
one=(struct node*)malloc(sizeof(struct node));
two=(struct node*)malloc(sizeof(struct node));
three=(struct node*)malloc(sizeof(struct
node)); four=(struct node*)malloc(sizeof(struct
node)); head->link=one;
one->link=two;
two->link=three;
three->link=four;
four->link=NULL;
one->info=10;
two->info=20;
three->info=30;
four->info=40;
void main()
{
struct node *head;
struct node *one,*two,*three,*four;
int data;
clrscr();
cout<<"INSERTION AT LAST OF LINKED LIST: \n";
head=(struct node*)malloc(sizeof(struct node));
one=(struct node*)malloc(sizeof(struct node));
two=(struct node*)malloc(sizeof(struct node));
three=(struct node*)malloc(sizeof(struct
node)); four=(struct node*)malloc(sizeof(struct
node)); head->link=one;
one->link=two;
two->link=three;
three->link=four;
four->link=NULL;
one->info=10;
two->info=20;
three->info=30;
four->info=40;
cout<<"LINKED LIST DISPLAY\n";
traverse(head);
cout<<"\nEnter the data item at location LAST: ";
cin>>data;
cout<<"\nLINKED LIST AFTER INSERTION \n";
insert(head,data);
traverse(head);
getch();
}
OUTPUT.
18. PROGRAM OF SEARCHING IN LINKED LIST.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
void traverse(struct node *head)
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct
node)); ptr->link=head->link;
while(ptr->link!=NULL)
{
ptr=ptr->link;
cout<<"\nElement is: "<<ptr->info;
}
}
void searching(struct node *head,int item)
{
struct node *ptr;
int loc=-1,i;
ptr=(struct node*)malloc(sizeof(struct
node)); ptr->link=head->link,i=1;
while(ptr->link!=NULL)
{
ptr=ptr->link;
if(ptr->info==item)
{
loc=i;
cout<<"LOCATION OF ITEM IS: "<<loc<<endl;
break;
}
i++;
}
}
void main()
{
struct node *head,*one,*two,*three,*four;
int item;
clrscr();
cout<<"PROGRAM OF SEARCHING : \n";
head=(struct node*)malloc(sizeof(struct node));
one=(struct node*)malloc(sizeof(struct node));
two=(struct node*)malloc(sizeof(struct node));
three=(struct node*)malloc(sizeof(struct node));
four=(struct node*)malloc(sizeof(struct node));
head->link=one;
one->link=two;
two->link=three;
three->link=four;
four->link=NULL;
one->info=10;
two->info=20;
three->info=30;
four->info=40;
traverse(head);
cout<<"\nEnter the item for searching: ";
cin>>item;
searching(head,item);
getch();
}
OUTPUT.
19. PROGRAM OF DELETION IN LINKED LIST.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;};
void traverse(struct node *head)
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct
node)); ptr->link=head->link;
while(ptr->link!=NULL)
{
ptr=ptr->link;
cout<<"\nElement is: "<<ptr->info; }}
void deletion(struct node *head,struct node *one)
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct
node)); ptr->link=head->link;
if(ptr->link==one)
{
head->link=one->link;
}
}
void main()
{
struct node *head;
struct node *one,*two,*three,*four;
clrscr();
cout<<"DELETION AT THE FIRST NODE IS: \n";
head=(struct node*)malloc(sizeof(struct node));
one=(struct node*)malloc(sizeof(struct node));
two=(struct node*)malloc(sizeof(struct node));
three=(struct node*)malloc(sizeof(struct node));
four=(struct node*)malloc(sizeof(struct node));
head->link=one;
one->link=two;
two->link=three;
three->link=four;
four->link=NULL;
one->info=10;
two->info=20;
three->info=30;
four->info=40;
cout<<"\nBEFORE DELETION: FIRST NODE \n";
traverse(head);
cout<<"\nAFTER DELETION: FIRST NODE \n";
deletion(head,one);
traverse(head);
getch();
}
OUTPUT.
20. PROGRAM OF STACK WITH IT’S
OPERATION
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#define MAX 14
char stack[MAX];
int top=-1;
void push(char ch)
{
top=top+1;
stack[top]=ch;
cout<<stack[top];
}
int pop(char ch)
{
top=MAX-1;
stack[top]=ch;
return(ch);}
void reverse(char ch)
{
top=MAX-1;
stack[top]=ch;
cout<<stack[top];}
void main()
{
char a[MAX];
int i;
clrscr();
cout<<"enter the string: "; gets(a); for(i=0;i<MAX;i+
+)
{
if(i==MAX-1)
{
cout<<"\tstack is full: "<<endl;
break;
}
push(a[i]);
}
cout<<"\nREVERSE OPERATION: "<<endl;
for(i=MAX-1;i>-1;i--)
{
reverse(a[i]);
}
cout<<"\nPOP OPERATION: "<<endl;
for(i=MAX-1;i>-1;i--)
{
if(i==0)
{
cout<<"\nstack is underflow: "<<endl;
}
pop(a[i]);
}
getch();