0% found this document useful (0 votes)
33 views99 pages

ds-1 Vikrant

Uploaded by

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

ds-1 Vikrant

Uploaded by

venomzard2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 99

INDEX

S.no Topic. Pages Signature


1 Introduction.
2 Array.
3 One Dimensional Array
Program.
4 Two Dimensional Array
Program.
5 Program of Insertion in
Array.
6 Program of Deletion in Array.
7 Program of Linear Search.
8 Program of Binary Search.
9 Program of Bubble Sort.
10 Program of Insertion Sort.
11 Program of Selection Sort.
12 Program of Quick Sort.
13 Program of Merge Sort.
14 Program of Traversing And
Creation of Linked List.
15 Program of Insertion At
Beginning of Linked List.
16 Program of Insertion At
Middle of Linked List.
S.no Topic. Pages Signature
17 Program of Insertion At Last
of Linked List.
18 Program of Searching in
Linked List.
19 Program of Deletion in
Linked List.
20 Program of Stack And it’s
Operation.
1. INTRODUCTION

DATA STRUCTURE:- Data Structure can be defined as


the group of data elements which provides an efficient way
storing and organizing data in the computer so that it can be
used efficiently. Some examples of data structures are Arrays
,Linked list, Stack, Queue ,etc. Data Structure are widely used
in almost every aspect of computer science i.e . Operating
System, Compiler Design, Artificial intelligence, Graphics and
many more.
2. ARRAY.

DEFINATION:- An Array can be defined as collection of


same data type elements and it can be stored in contiguous
memory collection .
TYPE OF ARRAY:- ONE DIMENSIONAL ARRAY AND TWO
DIMMENSIONAL ARRAY.
SYNTAX OF ARRAY:-
DATA_TYPE ARRAY_NAME[TOTAL NUMBER OF
ELEMENT];
EXAMPLE OF ARRAY:-
INT A[20];
*ONE-DIMENSIONAL ARRAY:- A One Dimensional Array is
the Simplest form of an Array in which the elements are
stored Linearly and can be accessed individually by
specifying the index value of each element stored in the
array
*TWO-DIMENSIONAL ARRAY:-A Two Dimensional Array is a
data structure that contains a collection of cells laid out in
two dimensional grid,similar to a table with rows and
columns although the values are still stored linearly in
memeory.
OUTPUT.
3. PROGRAM OF ONE DIMENSIONAL ARRAY

#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<<"\nenter the location where you want to insert: ";


cin>>loc;
cout<<"enter the item which you want to insert: ";
cin>>item;
for(i=6-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=item;

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 partition(int a[],int p,int r)

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;

void quicksort(int a[],int p,int r)

if(p<r)

int q;

q=partition(a,p,r);

quicksort(a,p,q);

quicksort(a,q+1,r);

void display(int a[],int n)

int i;

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

{
cout<<"\t"<<a[i];

cout<<endl;

void main()

int a[50],i,n; cout<<"\t\t\

tQUICK SORT:\n ";

cout<<"Enter the number of element: ";

cin>>n;

cout<<"Enter the element of array: ";

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

{
cin>>a[i];

cout<<"Display of unsorted array: \n";

display(a,n);
quicksort(a,0,n-1);

cout<<"Display of sorted array: \n";

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];

void mergesort(int a[],int low,int high)

int mid;

if(low<high)

mid=(low+high)/2;

mergesort(a,low,mid);

mergesort(a,mid,high);

merge(a,low,high,mid);

void display(int a[],int low,int high)

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”;

cout<<"Enter the number of element in array: ";

cin>>n;

cout<<"enter the value of array: ";

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

cin>>a[i];

}
cout<<”Display of unsorted array: \n”;

display(a,0,n);

mergesort(a,0,n);

cout<<”Display of sorted array: \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;

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 main()

{
struct node *head;

struct node *one,*two,*three,*four;

clrscr();

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);

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;

cout<<"Before insertion: \n";


traverse(head);
insert(head);
cout<<"\nAfter insertion: \n";
traverse(head);
getch();
}
OUTPUT.
16. PROGRAM OF INSERTION AT MID 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 *two,int data)
{
struct node *loc;
loc=(struct node*)malloc(sizeof(struct
node)); loc->info=data;
if(two->link==NULL)
{
loc->link=head->link;
head->link=loc;
}
else
{
loc->link=two->link;
two->link=loc;
}
}
void main()
{
struct node *head;
struct node *one,*two,*three,*four;
int data;
clrscr();
cout<<”INSERTION AT MID 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 three: ";
cin>>data;
cout<<"\nLINKED LIST AFTER INSERTION \n";
insert(head,two,data);
traverse(head);
getch();
}
OUTPUT.
17. PROGRAM OF INSERTION AT LAST 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,int data)
{
struct node *ptr,*NEW;
ptr=(struct node*)malloc(sizeof(struct node));
NEW=(struct node*)malloc(sizeof(struct node));
ptr->link=head->link;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
NEW->link=ptr->link;
NEW->info=data;
ptr->link=NEW;
}

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();

You might also like