#include<stdio.h> #include<string.h> #include<stdlib.h> struct node { int data; struct node *link; //to creating self reference pointer }; int display(struct node *,int); void insertion(struct node *,int); void deletion(struct node *,int); struct node *head; int count=0; main() { int i,choice,inst=0,del,chk1=1,chk=1,num; do { printf("\nMENU:\n\n 1.Insertion\n 2.Deletion \n 3.Display\n 4.Exit\n\nenter your choice : "); scanf("%d",&choice); switch(choice) { case 1: if(count==0) { printf("\nThis is First Node\n"); head=(struct node *)malloc(sizeof(struct node)); insertion(head,inst); } else { printf("Where do you want to insert data\n 1. In starting \n2. End\n"); scanf("%d",&inst); insertion(head,inst); } break; case 2: printf("from where do you want to delete\n 1. In starting 2. End\n"); scanf("%d",&del); deletion(head,del); break; case 3: chk1=display(head,count); if(chk1==1) printf("NULL\n"); break; case 4: chk=0; break; default : printf("invalid option selected\ntry again\n\n"); } } while(choice!=4); } void insertion(struct node *list,int num) { struct node *new1; int num1; printf("Enter the number\n"); scanf("%d",&num1); if(count==0&&num==0) { head->data=num1; head->link=NULL; } else if(num==1&&count>0) { new1=(struct node *)malloc(sizeof(struct node)); new1->data=num1; new1->link=head; head=new1; } else if(num==2&&count>0) { for( ; ;) { if(list->link!=NULL) list=list->link; else { new1=(struct node *)malloc(sizeof(struct node)); new1->data=num1; list->link=new1; new1->link=NULL; break; } } } count++; } void deletion(struct node *list,int num) { struct node *temp; if(num==1&&count>0) { head=list->link; free(list); } else if(num==2&&count>1) { for( ; ;) { if(list->link->link!=NULL) list=list->link; else { free(list->link); list->link=NULL; break; } } } else if(count==1) free(head); else if(count<=0) { printf("List is empty\n\n"); count--; } count--; } int display(struct node *list,int num) { int chk=1; if(num>0) { printf("%d->",list->data); num--; display(list->link,num); } else if(count<=0) { chk=0; printf("list is empty\n\n"); } return(chk); }
New Android apps UniqueKey
Saturday, 21 March 2015
Simple Linked list Program
Inline Function
Inline function: whenever we use inline function in our program, and whenever compiler executes inline function, each time it create a copy of function code and execute the functions.
for use inline function you just have to write "inline" in front of function name.
for use inline function you just have to write "inline" in front of function name.
#include<iostream> using namespace std; inline float area(float radius) { return 3.14*radius*radius; } inline float peri(float radius) { return 2*3.14*radius; } inline float simple(float amount,float interest,int year) { return amount*(interest/100)*year; } main() { int year; float amount,radius,interest; cout << "Enter radius: "; cin >> radius; cout << "Area : " << area(radius)<<endl; cout << "Perimeter : " << peri(radius)<<endl; cout << "Simple Interest Calculator" << endl; cout << "Amount: "<<endl; cin >> amount; cout << "Interest (in %): "; cin >> interest; cout << "Year: "; cin >> year; cout << "Simple interest: "<< simple(amount,interest,year)<<endl; }
Swapping using Function Overloading
#include<iostream> #include<string.h> using namespace std; class swapp{ public: int itemp; float ftemp; char ctemp[100]; void swap_it(int num1,int num2) { cout<<"\nBEFORE SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2; itemp=num1; num1=num2; num2=itemp; cout<<"\nAFTER SWAPPING :"<<"\nNUM1: "<<num1<<"\nNUM2 :"<<num2; } void swap_it(float num1,float num2) { cout<<"\nBEFORE SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2; ftemp=num1; num1=num2; num2=ftemp; cout<<"\nAFTER SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2; } void swap_it(char c1[100],char c2[100]) { cout<<"\nBEFORE SWAPPING :"<<"\n"<<"CHAR1 :"<<c1<<"\nCHAR2 :"<<c2; strcpy(ctemp,c1); strcpy(c1,c2); strcpy(c2,ctemp); cout<<"\nAFTER SWAPPING :"<<"\n"<<"CHAR1 :"<<c1<<"\nCHAR2 :"<<c2; } }; int main() { swapp swapp; int i1,i2; float f1,f2; char c1[100],c2[100]; cout<<"\nENTER THE INTEGER VARIABLES"; cin>>i1>>i2; swapp.swap_it(i1,i2); cout<<"\nENTER THE FLOAT VARIABLES"; cin>>f1>>f2; swapp.swap_it(f1,f2); cout<<"\nENTER THE CHARACTER VARIABLES"; cin>>c1>>c2; swapp.swap_it(c1,c2); return 0; }
Stack operations
#include<iostream> using namespace std; int size; class stack{ public: int stck[100]; int top; int i; int element; void push(void) { cout<<"\nenter the element to push into th stack\n"; cin>>element; stck[++top]=element; } void display(void) { for(i=top;i>=0;i--) { if(i==top) { cout<<"TOP OF STACK-->"<<stck[i]; } else { cout<<"\t\n"<<stck[i]; } } } int pop(void) { return(stck[top--]); } int isfull() { if(top==size-1) { return 1; } else { return 0; } } int isempty() { if(top==-1) { return 1; } else { return 0; } } stack() { stck[100]={0}; top=-1; } }; int main() { int choice; stack stack; cout<<"\nenter the size of stack\n"; cin>>size; do { cout<<"\n..MENU..\n1.push\n2.pop\n3.display.\n4.EXIT"; cin>>choice; switch(choice) { case 1: if(!(stack.isfull())) { stack.push(); } else { cout<<"\nSTACK IS FULL\n"; } break; case 2: if(!(stack.isempty())) { cout<<"\nTHE POPPED OUT ELEMENT IS :"<<stack.pop(); } else { cout<<"\nSTACK IS EMPTY\n"; } break; case 3: stack.display(); break; case 4: cout<<"\nYOU HAVE EXITED\n"; break; } }while(choice!=4); return 0; }
Saturday, 7 March 2015
Armstrong number
/*check number is Armstrong number or not*/ #include<stdio.h> #include<math.h> main() { int num,i,j,temp,sum=0,dig,n; printf("enter the number to check\n"); scanf("%d",&num); for(temp=num;temp>0;) { dig=temp%10; sum=sum+(dig*dig*dig); temp=temp/10; } if(num==sum) { printf("number is a armstrong number\n"); } else printf("number is not a armstrong number\n"); }
Print all the characters using ASCII value.
/*print alphabet*/ #include<stdio.h> main() { int n[122],i,j,num; for(i=0;i<=121;i++) { n[i]='0'+i; printf("%d %c\t",n[i],n[i]); } }
Merge 2 arrays and then sort this array.
/*a c program to sort the merg of array*/ #include<stdio.h> main() { float num1[100],num2[100],num[300]; //array declaration int n1,n2,i,j,t; printf("\nenter the number of elements in 1st array\n"); scanf("%d",&n1); //get the number of 1st array element printf("\nenter first array in ascending order\n"); for(i=0;i<n1;i++) { scanf("%f",&num1[i]); //to read the element of array } printf("\nenter the number of elements in 2nd array\n"); scanf("%d",&n2); //get the number of 2nd array element printf("\nenter second array in descending order\n"); for(j=0;j<n2;j++) { scanf("%f",&num2[j]); //to read the element of array } i=0;j=0; while(i<n1) { num[i]=num1[i]; i++; } while(j<n2) { num[i]=num2[j]; j++; i++; } for(i=0;i<n1+n2-1;i++) { for(j=0;j<n1+n2-i-1;j++) { if(num[j]==num[j+1]) j++; if(num[j]<num[j+1]) { t=num[j]; //use bubble sort to arrage array element num[j]=num[j+1]; num[j+1]=t; } } } printf("\nthe sorted and merged array in decending order is :\n"); for(i=0;i<n1+n2;i++) if(num[i]!=num[i+1]) printf("%6.0f\n",num[i]); //to print sorted and merg array in decending order }
Computing equation using recursive function.
/*a c program to compute equation using a recursive function*/ #include<stdio.h> #include<math.h> long int power(int,int); //declaration of function main() { int num,x; long int sum; printf("---------------------------------\n"); printf("your equation is::\n"); printf("4*x^5+3*x^4+2*x^3+x^2+x\n"); //to show the equation printf("--------------------------------\n"); printf("enter the value of x\n"); //to enter the value of x scanf("%d",&x); sum=4*power(x,5)+3*power(x,4)+2*power(x,3)+power(x,2)+x; // using the function printf("-------------------------------\n"); printf("result of given equation is :: %ld\n",sum); printf("--------------------------------\n"); } long int power(int num1,int num2) //definition of the function { if(num2!=1) return(num1*power(num1,num2-1)); }
Program to create number pyramid.
/*to make number pyramid*/ #include<stdio.h> #include<math.h> main() { int num,t=0,s=1,j=0; int choice,num1,dig,sum=0; int n,i,pro; printf("1. FOR MAKE PYRAMID\n"); printf("2. FOR FIND DIGITS SUM\n"); printf("3. FOR MAKE MATHEMATICAL TABLE OF GIVEN NUMBER\n4. EXIT\n\n"); do{ printf("\nENTER YOUR CHOICE\n\n"); scanf("%d",&choice); switch(choice) { case 1 : printf("enter the terms of pyramid\n"); scanf("%d",&num); for(t=0;t<=num;t++) { for(j=0;j<=num-t;j++) { printf(" "); } for(s=1;s<=t;s++) { printf(" %d",s); } printf(" \n"); } printf(" "); break; case 2 : printf("enter the number\n"); scanf("%d",&num1); while(num1!=0) { dig=num1%10; sum=sum+dig; num1=num1/10; } printf("sum of indivisual digits=%d\n",sum); break; case 3 : printf("enter the number to make mathematical table\n"); scanf("%d",&n); printf("table of number is \n\n"); for(i=1;i<=10;i++) { pro=n*i; printf("%d\n",pro); } break; case 4 : break; default : printf("invalid option\ntry again\n"); break; } }while(choice!=4); }
Find enter number pellendrom or not and find number is prime or not
/*program to find enter number pellendrom or not and find number is prime or not*/ #include<stdio.h> #include<math.h> main() { int num,i,j,k,rev=0,n,c=0; printf("enter the number\n"); scanf("%d",&num); n=num; while(num>0) { i=num%10; rev=rev*10+i; num=num/10; } if(rev==n) { printf("%d is pellendrom number\n\n",n); } else { printf("%d is not a pellendrom number\n",n); } for(j=2;j<=n/2;j++) { if(n%j==0) { c=1; break; } } if((c==1)||(n==1)) printf("%d is not a prime number\n\n",n); else printf("%d is a prime number\n\n",n); }
Check whether year is a leap year or not
/*program to check the year leap or not*/ #include<stdio.h> #include<math.h> main() { int year; printf("enter the year which you want to check\n"); scanf("%d",&year); if(year%400==0) { printf("%d is leap year\n",year); } else { if(year%4==0&&year%100!=0) { printf("%d is leap year\n",year); } else { printf("%d is not leap year\n",year); } } }
Subscribe to:
Posts (Atom)