C ++ Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

C++ Programming

Use vs code to run these programs


C++ programs and data structure
Basis programs
Selection statement program
Iterative statement program
Patterns printing programs
Array’s programs & DS
String programs
Function & recursion
Linked list
Stack
Queue

➢ Basics
// C++ Program to print HELLO WORLD
#include<iostream>
using namespace std;
int main()
{
cout<<" HEllO WORLD !!!!!";
}

// C++ program to add two numbers


#include<iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"ENTER THE TWO NUMBERS : ";

SURAJ MEHTA 1
cin>>a>>b;
sum=a+b;
cout<<"sum= "<<sum;
}

// C++ program to multiply two floating numbers


#include<iostream>
using namespace std;
int main()
{
float a,b,product;
cout<<"ENTER THE TWO FLOAT NUMBERS : ";
cin>>a>>b;
product=a*b;
cout<<"product = "<<product;

// C++ program to perform all arithmetic operations


#include<iostream>
using namespace std;
int main()
{
int a,b,sum,diff,mult,rem;
float div;
cout<<"ENTER THE TWO NUMBERS : ";
cin>>a>>b;
sum=a+b;
diff=a-b;
mult=a*b;
div=a/b;
rem=a%b;
cout<<"sum=
"<<sum<<endl<<"subtractions="<<diff<<endl<<"multiplication
="<<mult<<endl<<"division="<<div<<endl<<"remainder="<<rem;
}

SURAJ MEHTA 2
// C++ program to convert upper case character to lower
case
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"ENTER the upper case character : ";
cin>>ch;
cout<<ch;
ch=ch+32;
cout<<"= "<<ch;
}

// C++ program to convert lower case character to upper


case
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"ENTER the lower case character : ";
cin>>ch;
cout<<ch;
ch=ch-32;
cout<<"= "<<ch;
}

// C++ program to print ASCII value of any character


#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"ENTER the any character : ";
cin>>ch;
cout<<int(ch);
}

SURAJ MEHTA 3
// C++ program to swap two numbers
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"ENTER the two value a & b : ";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"a= "<<a<<endl<<"b= "<<b;
}

// using temporary variable


#include<iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"ENTER the two value a & b : ";
cin>>a>>b;
temp=b;
b=a;
a=temp;
cout<<"a= "<<a<<endl<<"b= "<<b;
}

➢ Conditional statement
// C++ program to find the PROFIT and LOSS
#include<iostream>
using namespace std;
int main()
{

SURAJ MEHTA 4
int sp,cp,p,l;
cout<<"ENTER the cost price of the product : ";
cin>>cp;
cout<<"ENTER the selling price of the product : ";
cin>>sp;
if(sp>cp)
cout<<"PROFIT of $ "<<(sp-cp);
else
cout<<"LOSS of $ "<<(cp-sp);
}

// C++ program to largest of 2 numbers


#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"ENTER THE TWO NUMBERS : ";
cin>>a>>b;
if(a>b)
cout<<"largest no. is "<<a;
else
cout<<"largest no. is "<<b;
}

// C++ program to largest of 3 numbers


#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"ENTER THE 3 NUMBERS : ";
cin>>a>>b>>c;
if(a>b && a>c)
cout<<"largest no. is "<<a;
else
{
if(b>c)
cout<<"largest no. is "<<b;
else

SURAJ MEHTA 5
cout<<" largest no. is "<<c;
}
}

// C++ program to largest of 4 numbers


#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
cout<<"ENTER THE 4 NUMBERS : ";
cin>>a>>b>>c>>d;
if(a>=b && a>=c&&a>=d)
cout<<"largest no. is "<<a;
else
{
if(b>=c&&b>=d)
cout<<"largest no. is "<<b;
else
{
if(c>=d)
cout<<" largest no. is "<<c;
else
cout<<" largest no. is "<<d;

➢ Loops (Iteration statement)


// C++ program to find the sum of N natural numbers
#include<iostream>
using namespace std;
int main()
{
int n,i,sum=0;
cout<<"ENTER THE nth NUMBERS : ";
cin>>n;

SURAJ MEHTA 6
for(i=1;i<=n;i++)
{
sum=sum+i;
}
cout<<"sum ="<<sum;

// C++ program to find the even and odd sum of N


natural numbers
#include<iostream>
using namespace std;
int main()
{
int n,i,even_sum=0,odd_sum=0;
cout<<"ENTER THE nth NUMBERS : ";
cin>>n;
for(i=1;i<=n;i++)
{
if(i%2==0)
even_sum=even_sum+i;
else
odd_sum+=i;
}
cout<<"even sum ="<<even_sum<<endl;
cout<<"odd sum ="<<odd_sum;

// C++ program to print multiplication table of


any number
#include<iostream>
using namespace std;
int main()
{
int n,i;
cout<<"ENTER THE any NUMBERS : ";
cin>>n;
for(i=1;i<=10;i++)
{

SURAJ MEHTA 7
cout<<n<<"x"<<i<<"="<<n*i<<endl;
}

// C++ program to print multiplication table of any number


in given range
#include<iostream>
using namespace std;
int main()
{
int n,i,j,m;
cout<<"ENTER THE range N to M : ";
cin>>n>>m;
for(j=n;j<=m;j++)
{
cout<<endl<<"multiplication table of "<<j<<"\n";
for(i=1;i<=10;i++)
{
cout<<j<<"x"<<i<<"="<<j*i<<endl;
}
}

// C++ program to find the sum of digits of any number


#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
cout<<"enter the any number : ";
cin>>n;
while(n!=0)
{
sum=sum+n%10;
n=n/10;

SURAJ MEHTA 8
}
cout<<"sum of digits = "<<sum;
}

// c++ program to print the reverse of any number


#include<iostream>
using namespace std;
int main()
{
int n,rev=0;
cout<<"enter the any number : ";
cin>>n;
while(n!=0)
{
rev=(rev*10)+n%10;
n=n/10;

}
cout<<"reverse no. = "<<rev;
}

// Another way to print the reverse of any number


#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"enter the any number : ";
cin>>n;
while(n!=0)
{
cout<<n%10;
n=n/10;
}
}

// C++ program to check whether given no. is palindrome


no. or not
#include<iostream>
using namespace std;
int main()
{

SURAJ MEHTA 9
int n,rev=0,m;
cout<<"enter the any number : ";
cin>>n;
m=n;
while(n!=0)
{

rev=(rev*10)+n%10;
n=n/10;

}
if(rev==m)
cout<<"given no. is palindrome number";
else
cout<<"given no. is NOT palindrome number";
}

// C++ program to check whether given no. is Armstrong


no. or not
#include<iostream>
using namespace std;
int main()
{
int n,r,a=0,m;
cout<<"enter the any number : ";
cin>>n;
m=n;
while(n!=0)
{

r=n%10;
a=a+r*r*r;
n=n/10;

}
if(a==m)
cout<<"given no. is Armstrong number";
else
cout<<"given no. is NOT Armstrong number";
}

SURAJ MEHTA 10
// C++ program to find the factorial of a numbers
#include<iostream>
using namespace std;
int main()
{
int n,i,fact=1;
cout<<"enter the any number : ";
cin>>n;
if(n>=0)
{
for(i=n;i>=1;i--)
{
fact=fact*i;
}
cout<<"factorial="<<fact;
}
else
cout<<"factorial is 0";
}

// C++ program to find the LCM OF 3 numbers


#include<iostream>
using namespace std;
int main()
{
int a,b,c,g,p,i;
cout<<"enter the 3 number : ";
cin>>a>>b>>c;
g=(a>b&&a>c?a:b>c?b:c);
p=a*b*c;
for(i=g;i<=p;i++)
{
if(i%a==0&&i%b==0&&i%c==0)
{
cout<<"LCM ="<<i;
break;
}
}
}

// C++ program to find the HCF OF 3 numbers


#include<iostream>

SURAJ MEHTA 11
using namespace std;
int main()
{
int a,b,c,min,hcf,i;
cout<<"enter the 3 number : ";
cin>>a>>b>>c;
min=(a<b&&a<c?a:b<c?b:c);
for(i=1;i<=min;i++)
{
if(a%i==0&&b%i==0&&c%i==0)
{
hcf=i;
}
}
cout<<"HCF ="<<hcf;
}

➢ Patterns printing program


/*C++ program to print pattern
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
solid rectangle
*/
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"* ";
}

SURAJ MEHTA 12
cout<<endl;
}
}

/*C++ program to print pattern


* * * * *
* *
* *
* *
* * * * *
hollow rectangle
*/
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1||j==1||i==n||j==n)
cout<<"* ";
else
cout<<" ";
}

cout<<endl;
}
}

/*C++ program to print pattern


*
* *
* *
* *
* * * * *

hollow triangle
*/

SURAJ MEHTA 13
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i==1||j==1||i==j||i==n)
cout<<"* ";
else
cout<<" ";
}

cout<<endl;
}
}

/*C++ program to print pattern


*
* *
* * *
* * * *
* * * * *

triangle */

#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"* ";

SURAJ MEHTA 14
}

cout<<endl;
}
}

/*C++ program to print pattern


* * * * *
* * * *
* * *
* *
*

Inverted triangle */

#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<"* ";
}

cout<<endl;
}
}

/*C++ program to print pattern


* * * * *
* * * *
* * *
* *
*

Inverted triangle */

SURAJ MEHTA 15
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i<=j)
cout<<"* ";
else
cout<<" ";
}

cout<<endl;
}
}

/*C++ program to print pattern


*
* *
* * *
* * * *
* * * * *
triangle */

#include<iostream>
using namespace std;
int main()
{
int i,j,n,k;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(k=1;k<=2*n-2*i;k++)
cout<<" ";
for(j=1;j<=i;j++)

SURAJ MEHTA 16
{
cout<<"* ";
}

cout<<endl;
}
}

/*C++ program to print pattern


*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
pyramid */
#include<iostream>
using namespace std;
int main()
{
int i,j,n,k,c=1;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
cout<<" ";
for(j=1;j<=c;j++)
{
cout<<"* ";
}
c+=2;
cout<<endl;
}
}
/*C++ program to print pattern
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
inverted pyramid */

SURAJ MEHTA 17
#include<iostream>
using namespace std;
int main()
{
int i,j,n,k,c=1;
cout<<"enter the number of row : ";
cin>>n;
for(i=0;i<n;i++)
{
for(k=1;k<=i;k++)
cout<<" ";
for(j=1;j<=2*(n-i)-1;j++)
{
cout<<"* ";
}
c+=2;
cout<<endl;
}
}
/*C++ program to print pattern
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

pyramid pattern

*/

#include<iostream>
using namespace std;
int main()
{
int i,j,n,k,l;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(k=1;k<=2*n-2*i;k++)
cout<<" ";
for(j=1;j<=i;j++)

SURAJ MEHTA 18
{
cout<<j<<" ";
}
for(l=i-1;l>=1;l--)
cout<<l<<" ";

cout<<endl;
}
}

/*C++ program to print pattern


1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5

pyramid pattern

*/

#include<iostream>
using namespace std;
int main()
{
int i,j,n,k,l;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(k=1;k<=2*n-2*i;k++)
cout<<" ";
for(j=i;j>=1;j--)
{
cout<<j<<" ";
}
for(l=2;l<=i;l++)
cout<<l<<" ";

cout<<endl;
}

SURAJ MEHTA 19
}

➢Array
// c++ program to find the greatest and smallest between
multiple numbers
#include<iostream>
using namespace std;
int main()
{
int ar[100],i,n,g,s;
cout<<"enter the size of an array : ";
cin>>n;
cout<<"enter the data \n";
for(i=0;i<n;i++)
cin>>ar[i];
g=s=ar[0];
for(i=0;i<n;i++)
{
if(g<ar[i])
g=ar[i];
else
{
if(s>ar[i])
s=ar[i];
}
}
cout<<"greatest no. is : "<<g<<endl;
cout<<"smallest no. is :"<<s;

// c++ program to search a number in array


#include<iostream>
using namespace std;
int main()
{
int ar[100],i,n,s;
cout<<"enter the size of an array : ";
cin>>n;

SURAJ MEHTA 20
cout<<"enter the data \n";
for(i=0;i<n;i++)
cin>>ar[i];
cout<<"Enter the which you want be search : ";
cin>>s;
for(i=0;i<n;i++)
{
if(s==ar[i])
{
cout<<"Searching successful";
break;
}

}
if(i==n)
cout<<s<<" is not found in array list ";

// c++ program to seach a number in array


#include<iostream>
using namespace std;
int main()
{
int ar[100],i,n,temp,j;
cout<<"enter the size of an array : ";
cin>>n;
cout<<"enter the data \n";
for(i=0;i<n;i++)
cin>>ar[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(ar[i]>ar[j])
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}

SURAJ MEHTA 21
cout<<" "<<ar[i];

➢ String

//c++ program to find the length of a string


#include<iostream>
using namespace std;
int main()
{
char str[100];
int i=0;
cout<<"enter the string : ";
cin.get(str,100);
while(str[i]!='\0')
{
i++;
}
cout<<"string length is "<<i;
}

//c++ program to find the rverse of a string


#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[100];
int i,l;
cout<<"enter the string : ";

SURAJ MEHTA 22
cin.get(str,100);
l=strlen(str);
i=l;
while(i>=0)
{
cout<<str[i];
i--;
}

//c++ program to copy a string


#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[100],str2[100];
int i=0,l;
cout<<"enter the string : ";
cin.get(str1,100);
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++;
}
cout<<"copied string is "<<str2;

//c++ program to upper case string into lower case


string
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[100];
int i=0;
cout<<"enter the string : ";
cin.get(str1,100);
while(str1[i]!='\0')

SURAJ MEHTA 23
{
if(str1[i]>='A'&&str1[i]<='Z')
str1[i]+=32;
i++;
}
cout<<"lower case string is "<<str1;

//c++ program to lower case string into upper case


string
#include<iostream>
using namespace std;
int main()
{
char str1[100];
int i=0;
cout<<"enter the string : ";
cin.get(str1,100);
while(str1[i]!='\0')
{
if(str1[i]>='a'&&str1[i]<='z')
str1[i]-=32;
i++;
}
cout<<"upper case string is "<<str1;

//c++ program to concatenate two strings


#include<iostream>
using namespace std;
int main()
{
char str1[100],str2[100],str3[200];
int i=0,j;
cout<<"enter the 1st string : ";
cin>>str1;
cout<<"enter the 2nd string : ";
cin>>str2;
while(str1[i]!='\0')
{

SURAJ MEHTA 24
str3[i]=str1[i];
i++;
}
j=0;
while(str2[j]!='\0')
{
str3[i]=str2[j];
i++;
j++;
}
str3[i]='\0';
cout<<"concatenated string is "<<str3;

➢ Function & Recursion


// c++ program to find the factorial of any number using
recursion
#include<iostream>
using namespace std;
unsigned long long int fact=1;
void factorial(int x)
{
if(x==1||x==0)
cout<<"factorial = "<<fact;
else
{
fact=fact*x;
factorial(x-1);
}
}
int main()
{
int n,sum;
cout<<"enter the any number : ";
cin>>n;
if(n>=0)
factorial(n);
else
cout<<"undefined or negative value ";

SURAJ MEHTA 25
}

// c++ program to print the fibonacci series using


recursion
#include<iostream>
using namespace std;
int a=0,b=1,c;
void fibo(int x)
{

if(x==0)
cout<<"......fibonacci series is printed";
else
{
cout<<a<<" ";
c=a+b;
a=b;
b=c;
fibo(x-1);
}
}
int main()
{
int n,sum;
cout<<"enter the no. of terms : ";
cin>>n;
fibo(n);
}

➢Array
// implementation of Array in c++
#include<iostream>
using namespace std;
int x,ar[100],n,i;

SURAJ MEHTA 26
void insert()
{
int p;
cout<<"enter the data : ";
cin>>x;
cout<<"enter the positon no. where you want to be
insert ";
cin>>p;
n++;
for(i=n-1;i>=p-1;i--)
{
ar[i+1]=ar[i];
}
ar[p-1]=x;

}
void print()
{
for(i=0;i<n;i++)
{
cout<<" "<<ar[i];
}

}
void del()
{
int p;
cout<<"enter the positon no. which you want to be
delete ";
cin>>p;
for(i=p-1;i<n;i++)
{
ar[i]=ar[i+1];
}
n--;
}
int main()
{
int ch,c=1;
cout<<"enter the size of an array : ";
cin>>n;
cout<<"enter the data ";

SURAJ MEHTA 27
for(i=0;i<n;i++)
cin>>ar[i];

while(c)
{
cout<<"\n 1.insert\n 2.delete\n 3.print\n 4.exit\n
enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:print();
break;
case 4:c=0;
}

}
}

➢Stack

// implementation of stack in c++


#include<iostream>
using namespace std;
int top=-1,s[5];
void push()
{
int x;
if(top==4)
cout<<"-----X stack is overflow X-----";
else
{
top++;
cout<<"enter the data which you want to push on
stack : ";
cin>>x;
s[top]=x;
}

SURAJ MEHTA 28
}
void print()
{
int i;
if(top==-1)
cout<<"stack is empty ";
else
{
for(i=top;i>=0;i--)
{
cout<<"____"<<endl<<" "<<s[i]<<endl;

}
}
}
void pop()
{
int x;
if(top==-1)
cout<<"-----X--> stack is underflow <--X-----";
else
{
top--;
}
}
int main()
{
int ch,c=1;

while(c)
{
cout<<"\n 1.push\n 2.pop\n 3.print\n 4.exit\n
enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:print();
break;

SURAJ MEHTA 29
case 4:c=0;
}

}
}

➢Queue
// implementation of Queue in c++
#include<iostream>
using namespace std;
int f=0,r=-1,q[5];
void insert()
{
int x;
if(r==4)
cout<<"-----X Queue is overflow X-----";
else
{
r++;
cout<<"enter the data which you want to insert in
queue : ";
cin>>x;
q[r]=x;
}
}
void print()
{
int i;
if(r<f)
cout<<"queue is empty ";
else
{
for(i=f;i<=r;i++)
{
cout<<"|"<<" "<<q[i]<<" "<<"|";

}
}
}
void del()
{

SURAJ MEHTA 30
int x;
if(r<f)
{
r=-1;
f=0;
cout<<"-----X--> Queue is underflow <--X-----";
}
else
{
f++;
}
}
int main()
{
int ch,c=1;

while(c)
{
cout<<"\n 1.insert\n 2.delete\n 3.print\n 4.exit\n
enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:print();
break;
case 4:c=0;
}

}
}

➢Single Linked list


// implementation of single linked list in c++
#include<iostream>
#include<stdlib.h>
using namespace std;
int x,i;

SURAJ MEHTA 31
typedef struct node
{
int data;
struct node *next;

}Node;
Node *create(Node *p)
{
if(p==NULL)
{
p=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
p->next=NULL;
p->data=x;
}
else
p->next=create(p->next);
return p;
}
Node *insatbeg(Node *p)
{
Node *q;
q=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
q->next=p;
q->data=x;
return q;

}
void print(Node *p)
{
while(p!=NULL)
{
cout<<" "<<p->data;
p=p->next;
}
}
void insatany(Node *p)
{
int k;

SURAJ MEHTA 32
Node *q;
q=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
cout<<" enter the position where you want be insert
:";
cin>>k;
for(i=1;i<k-1;i++)
{
p=p->next;
}
q->next=p->next;
p->next=q;
q->data=x;

}
void insatend(Node *p)
{
Node *q;
q=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
while(p->next!=NULL)
{
p=p->next;
}
q->next=NULL;
p->next=q;
q->data=x;

}
Node *delnode(Node *p)
{
int k;
Node *q=NULL,*r;
r=p;

cout<<" enter the position no.where you want be delete


:";
cin>>k;
for(i=1;i<k;i++)
{

SURAJ MEHTA 33
q=r;
r=r->next;
}
if(q==NULL)
{
p=p->next;
free(r);
}
else
{
q->next=r->next;
free(r);
}
return(p);

int main()
{
Node *head=NULL;
int ch,c=1;

while(c)
{
cout<<"\n 1.create\n 2.insert at beginning\n
3.insert at any positon\n 4.insert at end\n 5.dlete a
node\n 6.print\n 7.exit\n enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:head=create(head);
break;
case 2:head=insatbeg(head);
break;
case 3:insatany(head);
break;
case 4:insatend(head);
break;
case 5: head=delnode(head);
break;
case 6:print(head);

SURAJ MEHTA 34
break;
case 7:c=0;
}

}
}

➢Double linked list


// implementation of Double linked list in c++
#include<iostream>
#include<stdlib.h>
using namespace std;
int x,i;
typedef struct node
{
int data;
struct node *next,*prev;

}Node;
Node *create(Node *p)
{
if(p==NULL)
{
p=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
p->next=p->prev=NULL;
p->data=x;
}
else
{
p->next=create(p->next);
p->next->prev=p;
}
return p;
}
Node *insatbeg(Node *p)
{
Node *q;
q=(Node*)malloc(sizeof(Node));

SURAJ MEHTA 35
cout<<"enter the data : ";
cin>>x;
q->prev=NULL;
q->next=p;
p->prev=q;
q->data=x;
return q;

}
void print(Node *p)
{
while(p!=NULL)
{
cout<<" "<<p->data;
p=p->next;
}
}
void insatany(Node *p)
{
int k;
Node *q;
q=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
cout<<" enter the position where you want be insert
:";
cin>>k;
for(i=1;i<k-1;i++)
{
p=p->next;
}
p->next->prev=q;
q->next=p->next;
q->prev=p;
p->next=q;
q->data=x;

}
void insatend(Node *p)
{
Node *q;
q=(Node*)malloc(sizeof(Node));

SURAJ MEHTA 36
cout<<"enter the data : ";
cin>>x;
while(p->next!=NULL)
{
p=p->next;
}
q->next=NULL;
q->prev=p;
p->next=q;
q->data=x;

}
Node *delnode(Node *p)
{
int k;
Node *q=NULL,*r;
r=p;

cout<<" enter the position no.where you want be delete


:";
cin>>k;
for(i=1;i<k;i++)
{
q=r;
r=r->next;
}
if(q==NULL)
{
p=p->next;
p->prev=NULL;
free(r);
}
else if(r->next==NULL)
{
q->next=NULL;
free(r);
}
else
{
r->next->prev=q;
q->next=r->next;
free(r);

SURAJ MEHTA 37
}
return(p);

int main()
{
Node *head=NULL;
int ch,c=1;

while(c)
{
cout<<"\n 1.create\n 2.insert at beginning\n
3.insert at any positon\n 4.insert at end\n 5.dlete a
node\n 6.print\n 7.exit\n enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:head=create(head);
break;
case 2:head=insatbeg(head);
break;
case 3:insatany(head);
break;
case 4:insatend(head);
break;
case 5: head=delnode(head);
break;
case 6:print(head);
break;
case 7:c=0;
}

}
}

SURAJ MEHTA 38

You might also like