0% found this document useful (0 votes)
15 views3 pages

Data Structure (CLG)

The document contains code for three data structure programs - bubble sort, adding two numbers, and a stack program. The bubble sort program sorts an array using the bubble sort algorithm. The adding numbers program sums the even indexed elements of an array. The stack program implements push and pop operations on a stack.

Uploaded by

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

Data Structure (CLG)

The document contains code for three data structure programs - bubble sort, adding two numbers, and a stack program. The bubble sort program sorts an array using the bubble sort algorithm. The adding numbers program sums the even indexed elements of an array. The stack program implements push and pop operations on a stack.

Uploaded by

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

Data Structure file Programs

#Bubble Sort:-
// Online C++ compiler to run C++ program online
#include <iostream>
#include<stdio.h>
using namespace std;
int main() {
int n, c, d, temp;
cout<<"Enter the no of element you like to enter in the array : ";
cin>>n;
int ar[n + 1];
for(int i = 0;i < n;i++)
{
cout<<"Enter the element of index "<<i<<": ";
cin>>ar[i];

}
for(c = 0; c < n; c++)
{
for(d = 0;d < n - c-1;d++)
{
if(ar[d] > ar[d+1])
{
temp = ar[d];
ar[d] = ar[d+1];
ar[d+1] = temp;

}
}
}
for(int i = 0;i < n;i++)
{
cout<<ar[i]<<endl;

return 0;
}
--------------------------------------------------------------
PROGRAM TO ADD 2 NO:
#include<iostream>
using namespace std;

int sum(int [],int);


int main()
{ int a[20], n;
cout<<"Enter the range:";
cin>>n;
cout<<"Enter the element:"<<endl;
for(int i = 0;i < n;i++)
{
cin>>a[i];
}
cout<<"So the required sum will be "<<sum(a,n);
return 0;
}
int sum(int a[], int n)
{ int s = 0, i;
for(i = 0;i < n;i++)
{
if((i % 2) == 0) {
s = s + a[i];
}
}
return s;
}
----------------------------------------------------------------------------------
PROGRAM: STACK PROGRAM
//IMPLEMENT STACK PUSH AND POP
#include<stdio.h>
#include<conio.h>
#define Max 5

void PUSH();
void POP();
void DISPLAY();

int stack[5];
int Top=-1;

int main ()
{
int ch;
do
{
printf("\n1. Push \n2. POP \n3. DISPLAY \n4. EXIT");
printf("\nENTER UR CHOICE\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1: PUSH();
break;
case 2: POP();
break;
case 3: DISPLAY();
break;
}
}while(ch!=4);
return 0;
}
void PUSH()
{
int item;
if(Top==Max-1)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nENTER THE VALUE U WANT TO PUSH\n");
scanf("%d",&item);
printf("\n");
Top=Top+1;
stack[Top]=item;
}
}
void POP()
{
int item;
if (Top==-1)
{
printf("\nUNDERFLOW\n");
}
else
{
item=stack[Top];
Top=Top-1;
printf("\nTHE DELETED ITEM FROM THE STACK IS %d\n\n",item);
}
}
void DISPLAY()
{
int i;
if(Top==-1)
{
printf("\nUNDERFLOW\n");
}
else
{
printf("\nStack ELEMENT ARE--->\n");
for(i=Top;i>=0;i--)
{
printf("\n %d\n",stack[i]);
}
printf("\n");

}
}
-------------------------------------------------------------------------------

You might also like