0% found this document useful (0 votes)
2 views

CS_Datastructure_prgms

The document contains multiple C++ programs demonstrating basic data structure operations, including finding the frequency of an element in an array, inserting and deleting elements, sorting an array using insertion sort, searching using binary search, and implementing stack and queue operations. Each program includes user input for elements and outputs the results of the operations performed. The document also showcases linked list creation and node appending.

Uploaded by

ruabkje503
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)
2 views

CS_Datastructure_prgms

The document contains multiple C++ programs demonstrating basic data structure operations, including finding the frequency of an element in an array, inserting and deleting elements, sorting an array using insertion sort, searching using binary search, and implementing stack and queue operations. Each program includes user input for elements and outputs the results of the operations performed. The document also showcases linked list creation and node appending.

Uploaded by

ruabkje503
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
You are on page 1/ 18

/* Prgm No: 1

Program to find the frequency of presence of an element in an array.*/

#include<iostream.h>
#include<conio.h>

void main()
{
int A[10],n,i,freq,ELE;
clrscr();
cout<<"Program to find the frequency of presence of an element in an
array"<<"\n";
cout<<"Enter the total number of elements";
cin>>n;
cout<<"Enter the elements into the array";
for(i=0;i<=n-1;i++)
{
cin>>A[i];
}
cout<<"Enter the search element";
cin>>ELE;
freq=0;
for(i=0;i<=n-1;i++)
{
if(A[i]==ELE)
{
freq++;
}
}
if(freq>0)
cout<<"The frequency of “<<ELE<<is = “<<freq;
else
cout<<ELE<<” does not exist”;
getch();
}
Output:

Program to find the frequency of presence of an element in an array


Enter total number of elements 5
Enter the elements into the array 2 4 2 5 2
Enter the search element 2
The frequency of 2 is =3

/* Prgm No: 2
Program to insert an element into an array at a given position*/
#include<iostream.h>
#include<conio.h>
void main()
{
int A[10],ELE,n,i,p;
clrscr();
cout<<"Program to insert an element into the array at a given position"<<"\n";
cout<<"Enter the number of elements";
cin>>n;
cout<<"Enter the elements into the array";
for(i=0;i<=n-1;i++)
{
cin>>A[i];
}
cout<<"Enter the element to be inserted : ";
cin>>ELE;
cout<<"Enter the position of the element ( 0 to “<<n<<"):”;
cin>>p;

if(p<=n-1)
{
for(i=n-1;i>=p;i--)
{
A[i+1]=A[i];
}
A[p]=ELE;
n=n+1;
cout<<"Elements after insertion = ";
for(i=0;i<=n-1;i++)
{
cout<<A[i]<<"\t";
}
}
else
{
cout<<p<<” is an invalid position”;
}
getch();
}

Output:
Program to insert an element into the array at a given position
Enter the number of elements 5
Enter the elements into the array 1 3 4 5 6
Enter the insertion element 2
Enter the position ( 0 to 5) : 1
Elements after insertion = 1 2 3 4 5 6

/* Prgm No: 3
Program to delete an element from an array from a given position*/

#include<iostream.h>
#include<conio.h>
void main()
{
int A[10],n,i,ELE,p;
clrscr();
cout<<"Program to delete an element from an array"<<"\n";
cout<<"Enter the number of elements";
cin>>n;
cout<<"Enter the elements into the array";
for(i=0;i<=n-1;i++)
{
cin>>A[i];
}
cout<<"Enter the position ( 0 to "<<n-1<<”) :”;
cin>>p;

if(p<=n-1)
{
ELE=A[p];
for(i=p;i<=n-1;i++)
{
A[i]=A[i+1];
}
n=n-1;
cout<<ele<<” is successfully removed\n”;
cout<<"Elements after deletion : ";
for(i=0;i<=n-1;i++)
{
cout<<A[i];
}
}
else
{
cout<<p<< “ is an invalid position”;
}

getch();
}
Output:
Program to delete an element from an array
Enter the number of elements : 5
Enter the elements into the array: 2 3 2 5 6 9
Enter the position ( 0 to 4 ) : 3
5 is successfully removed
Elements after deletion : 2 3 2 6 9
/* Prgm No: 4
Program to sort the elements of an array in ascending order using insertion
sort*/

#include<iostream.h>
#include<conio.h>

void main()
{
int A[10],i,j,TEMP,n;
clrscr();
cout<<"Program to sort the array using insertion sort"<<"\n";
cout<<"Enter the number of elements";
cin>>n;
cout<<"Enter the elements into the array";
for(i=0;i<=n-1;i++)
{
cin>>A[i];
}
for(i=1;i<=n-1;i++)
{
j=i;
while(j>=1)
{
if(A[j]<A[j-1])
{
TEMP=A[j];
A[j]=A[j-1];
A[j-1]=TEMP;
}
j=j-1;
}
}
cout<<"The sorted elements are ";
for(i=0;i<=n-1;i++)
{
cout<<A[i]<<"\t";
}
getch();
}
Output:

Program to sort a element of an array in ascending order using insertion sort


Enter the number of elements 5
Enter the elements into the array
22 77 44 11 88
The sorted elements are
11 22 44 77 88

/* Prgm No: 5
Program to search a given element in an array using binary search method*/

#include<iostream.h>
#include<conio.h>
void main()
{
int B,E,M,A[20],i,ele,loc,n;
clrscr();
cout<<"Program to search a given element in an array using binary search
method\n";
cout<<"Enter the value for n";
cin>>n;
cout<<"Enter the element in to the array (sorted array) ";
for(i=0;i<=n-1;i++)
{
cin>>A[i];
}
cout<<"Enter the search element=";
cin>>ele;
loc=-1;
B=0;
E=n-1;
while(B<=E)
{
M=int(B+E)/2;
if(A[M]==ele)
{
loc=M;
break;
}
if(ele<A[M])

{
E=M-1;
}
else
{
B=M+1;
}
}
if(loc>=0)
{
cout<<"Location is found="<<loc+1;
}
else
{
cout<<"Location is not found";
}
getch();
}

Output:
Program to search a given element in an array using binary search method
Enter the value for n 4
Enter the element in to the array ( sorted array) 1 4 5 6
Enter the search element=4
Location is found=2
/*Prgm no.:13
Program to perform push the items into the stack.*/

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 3
class stack
{
private:
int arr[MAX];
int top;
public:
stack()
{
top=-1;
}
void push(int item)
{
if(top==MAX-1)
{
cout<<"\n"<<"Stack is full"<<"\n";
return;
}
top++;
arr[top]=item;
cout<<item<<"is pushed"<<"\n";
}
void print()
{
if(top!=-1)
{
cout<<"Stack contains";
for(int i=0;i<=top;i++)
cout<<arr[i]<<"\t";
cout<<"\n";
}
else
cout<<"Stack is empty"<<"\n";
}
};

void main()
{
stack s;
char choice;
int itm;
clrscr();
cout<<” Program to perform push the items into the stack\n”;
s.print();
cout<<"Do you want to push an item(Y/N)?";
cin>>choice;
while(toupper(choice)=='Y')
{
cout<<"Enter the item";
cin>>itm;
s.push(itm);
s.print();
cout<<"Do you want to push another item(Y/N)";
cin>>choice;
}
getch();
}

Output:

Program to perform push the items into the stack


Stack is empty
Do you want to push an item(Y/N)? Y
Enter the item 100
100 is pushed
Stack contains 100
Do you want to push another item(Y/N) Y
Enter the item 200
200 is pushed
Stack contains 100 200
Do you want to push another item(Y/N) Y
Enter the item 300
300 is pushed
Stack contains 100 200 300
Do you want to push another item(Y/N) Y
Enter the item 400
Stack is full
Stack contains 100 200 300
Do you want to push another item(Y/N) N

/*Prgm no.:14
Program to pop the elements from the stack.*/
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define Max 10
class stack
{
private:
int arr[Max];
int top;
public:
stack()
{
top=-1;
}
void push(int item)
{
if(top==Max-1)
{
cout<<"\n"<<"Stack is full"<<"\n";
return;
}
top++;
arr[top]=item;
cout<<item<<" is pushed"<<"\n";
}
void print()
{
if(top!=-1)
{
cout<<"Stack contains"<<"\t";
for(int i=0;i<=top;i++)
cout<<arr[i]<<"\t";
cout<<"\n";
}
else
cout<<"Stack is empty"<<"\n";
}
void pop()
{
if(top==-1)
{
cout<<"Stack is empty"<<"\n";
return;
}
else
{
int data=arr[top];
top--;
cout<<data<<" is poped"<<"\n";
}}
};
void main()
{
stack s;
clrscr();
cout<<” Program to pop the elements from the stack\n”;
s.print();
s.push(100);
s.print();
s.push(200);
s.print();
s.push(300);
s.print();
s.pop();
s.print();
s.pop();
s.print();
s.pop();
s.print();
getch();
}

Output:
Program to pop the elements from the stack
Stack is empty
100 is pushed
Stack contains 100
200 is pushed
Stack contains 100 200
300 is pushed
Stack contains 100 200 300
300 is poped
Stack contains 100 200
200 is poped
Stack contains 100
100 is poped
Stack is empty
/*Prgm no.:15
Program to perform enqueue & dequeue.*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#define MAX 5
class queue
{
private:
int arr[MAX];
int first,last;
int count;
public:
queue();
void enqueue(int x);
int dequeue();
void getsize();
};
queue::queue()
{
first=0;
last=MAX-1;
count=0;
}
void queue::enqueue(int x)
{
last=(last+1)%MAX;
arr[last]=x;
cout<<x<<" is inserted"<<endl;
count++;
}
int queue::dequeue()
{
int x=arr[first];
first=(first+1)%MAX;
count--;
cout<<x<<" is deleted"<<endl;
return x;
}
void queue::getsize()
{
if(count>0)
{
cout<<"Queue contains"<<"\t";
for(int i=first;i<=last;i++)
cout<<arr[i]<<"\t";
cout<<"\n";
}
else
{
cout<<"Queue is empty"<<"\n";
}
}
void main()
{
queue q;
clrscr();
cout<<” Program to perform enqueue & Dequeue\n”;
q.enqueue(10);
q.getsize();
q.enqueue(20);
q.getsize();
q.enqueue(30);
q.getsize();
q.dequeue();
q.getsize();
q.dequeue();
q.getsize();
q.dequeue();
q.getsize();
getch();
}
Output:
Program to perform enqueue & Dequeue
10 is inserted
Queue contains 10
20 is inserted
Queue contains 10 20
30 is inserted
Queue contains 10 20 30
10 is deleted
Queue contains 20 30
20 is deleted
Queue contains 30
30 is deleted
Queue is empty
/*Prg no:16
Program to create a linked list and appending nodes.*/
#include<iostream.h>
#include<conio.h>
class linklist
{
private:
struct node
{
int data;
node *link;
}*START;
public:
linklist();
void print();
void append(int num);
void count();
};
linklist::linklist()
{
START=NULL;
}
void linklist::print()
{
if(START==NULL)
{
cout<<"Linked list is empty"<<endl;
return;
}
cout<<"Linked list contains";
node *tmp=START;
while(tmp!=NULL)
{
cout<<tmp->data<<"\t";
tmp=tmp->link;
}
}
void linklist::append(int num)
{
node *newnode;
newnode=new node;
newnode->data=num;
newnode->link=NULL;
if(START==NULL)
{
START=newnode;
cout<<num<<"is inserted as the first node"<<endl;
}
else
{
node *tmp=START;
while(tmp->link!=NULL)
tmp=tmp->link;
tmp->link=newnode;
cout<<num<<"is inserted"<<endl;
}
}
void linklist::count()
{
node *tmp;
int c=0;
for(tmp=START;tmp!=NULL;tmp=tmp->link)
c++;
cout<<"\n Number of nodes in the linked list="<<c<<endl;
}

void main()
{
linklist *obj=new linklist();
clrscr();
cout<<” Program to create a linked list and appending nodes\n”;
obj->print();
obj->append(100);
obj->print();
obj->count();
obj->append(200);
obj->print();
obj->count();
obj->append(300);
obj->print();
obj->count();
getch();
}

Output:
Program to create a linked list and appending nodes
Linked list is empty
100 is inserted as the first node
Linked list contains 100
Number of nodes in the linked list=1
200 is inserted
Linked list contains 100 200
Number of nodes in the linked list=2
300 is inserted
Linked list contains 100 200 300
Number of nodes in the linked list=3

You might also like