Posted by
snehil
Saturday, January 29, 2011
The user is provided with the following options..
To insert an item at begining
To insert an item at end
To insert an item at position
To traverse the list
To delete first item
To delete last item
To delete item from position
To count no. of items in list
- #include<conio.h>
- #include<stdio.h>
- #include<alloc.h>
- #include<process.h>
- struct node
- { int info;
- struct node *next;
- };
- typedef struct node sn;
- void insertatbeg(sn **,int);
- void insertatend(sn **,int);
- void insertatpos(sn **,int,int);
- void traverse(sn *);
- int delfirst(sn *);
- int dellast(sn *);
- int delpos(sn *,int);
- int count(sn *);
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void main()
- { sn *p;
- int n,i,l;
- char ch;
- clrscr();
- p=NULL;
- do
- { printf("\n-----select an operation-----");
- printf("\n1->To insert an item at begining<-");
- printf("\n2->To insert an item at end<-");
- printf("\n3->To insert an item at position<-");
- printf("\n4->To traverse the list<-");
- printf("\n5->To delete first item<-");
- printf("\n6->To delete last item<-");
- printf("\n7->To delete item from position<-");
- printf("\n8->To count no. of items in list<-");
- printf("\n9->Exit<-");
- printf("\n->Enter your choice:");
- scanf("%d",&n);
- switch(n)
- { case 1:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- insertatbeg(&p,i);
- break;
- case 2:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- insertatend(&p,i);
- break;
- case 3:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- printf("\n->Enter the position");
- scanf("%d",&l);
- insertatpos(&p,i,l);
- break;
- case 4:traverse(p);
- break;
- case 5:i=delfirst(p);
- printf("\nItem deleted is %d.",i);
- break;
- case 6:i=dellast(p);
- printf("\nItem deleted is %d.",i);
- break;
- case 7:printf("\n->Enter the position");
- scanf("%d",&l);
- i=delpos(p,l);
- printf("\nItem deleted is %d.",i);
- break;
- case 8:i=count(p);
- printf("\nNumber of nodes in list are %d.",i);
- break;
- case 9:exit(0);
- break;
- default:printf("\n->You have entered wrong choice");
- }
- printf("\n->Do you want to continue/exit(y/n)");
- fflush(stdin);
- scanf("%c",&ch);
- } while((ch=='y')||(ch=='Y'));
- getch();
- }
- int count(sn *p)
- { sn *temp;
- int c=1;
- temp=p;
- while((temp->next)!=NULL)
- { c++;
- temp=temp->next;
- }
- return c;
- }
- void insertatbeg(sn **p,int item)
- { sn *ptr;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- if(*p==NULL)
- ptr->next=NULL;
- else
- ptr->next=*p;
- *p=ptr;
- }
- void insertatend(sn **p,int item)
- { sn *ptr,*temp;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- ptr->next=NULL;
- if(*p==NULL)
- *p=ptr;
- else
- { temp=*p;
- while((temp->next)!=NULL)
- temp=temp->next;
- temp->next=ptr;
- }
- }
- void insertatpos(sn **p,int item,int loc)
- { sn *ptr,*temp,*s;
- int i,c;
- temp=*p;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- c=count(*p);
- if(loc>c)
- printf("\nList is short,Item can't inserted");
- else
- {
- for(i=0;i<loc-1;i++)
- { s=temp;
- temp=temp->next;
- }
- ptr->next=s->next;
- s->next=ptr;
- }
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void traverse(sn *p)
- { sn *temp;
- if(p==NULL)
- printf("\nList is empty.");
- else
- {
- temp=p;
- while((temp->next)!=NULL)
- { printf("\nno.=%d",temp->info);
- temp=temp->next;
- }
- printf("\nno.=%d",temp->info);
- }
- }
- int delfirst(sn *p)
- { int item;
- sn *temp;
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { temp=p;
- p=p->next;
- item=temp->info;
- free(temp);
- return item;
- }
- }
- int dellast(sn *p)
- { int item;
- sn *temp,*s;
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { temp=p;
- while((temp->next)!=NULL)
- { s=temp;
- temp=temp->next;
- }
- item=temp->info;
- s->next=NULL;
- free(temp);
- return item;
- }
- }
- int delpos(sn *p,int loc)
- { int item,i,c;
- sn *temp,*s;
- c=count(p);
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { if(loc>c)
- { printf("\nItem is not in list.");
- return 0;
- }
- else
- { temp=p;
- for(i=0;i<loc-1;i++)
- { s=temp;
- temp=temp->next;
- }
- item=temp->info;
- s->next=temp->next;
- free(temp);
- return item;
- }
- }
- }
Posted by
snehil
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int a[100],n,i,j,temp,loc,min;
- clrscr();
- printf("\How many elements:\n");
- scanf("%d",&n);
- printf("Enter the element of array\n");
- for(i=0;i<=n-1;i++)
- {
- scanf("%d",&a[i]);
- }
- min=a[0];
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(i=0;i<=n-1;i++)
- {
- min=a[i];
- loc=i;
- for(j=i+1;j<=n-1;j++)
- {
- if(a[j]<min)
- {
- min=a[j];
- loc=j;
- }
- }
- if(loc!=1)
- {
- temp=a[i];
- a[i]=a[loc];
- a[loc]=temp;
- }
-
- }
- printf("The number after selection sorting are:\n");
- for(i=0;i<=n-1;i++)
- {
- printf("%d\n",a[i]);
- }
- getch();
- }
Posted by
snehil
Friday, January 28, 2011
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- #include<conio.h>
- #define max 100
- int a[max],n,i,l,h;
- void main()
- {
- void input(void);
- input();
- getch();
- }
-
- void input(void)
- {
- void output(int a[],int n);
- void quick_sort(int a[],int l,int h);
- printf("How many elements in the array : ");
- scanf("%d",&n);
- printf("\n");
- printf("Enter the elemennts : \n");
- for(i=0;i<=n-1;i++)
- {
- scanf("%d",&a[i]);
- }
- l=0;
- h=n-1;
- quick_sort(a,l,h);
- printf("Sorted Array :\n ");
- output(a,n);
- }
-
- void quick_sort(int a[],int l, int h)
- {
- int temp,key,low,high;
- low=l;
- high=h;
- key=a[(low+high)/2];
- do
- {
- while(key>a[low])
- {
- low++;
- }
- while(key<a[high])
- {
- high--;
- }
- if(low<=high)
- {
- temp=a[low];
- a[low++]=a[high];
- a[high--]=temp;
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- } while(low<=high);
- if(l<high)
- quick_sort(a,l,high);
- if(low<h)
- quick_sort(a,low,h);
- }
- void output(int a[],int n)
- {
- for(i=0;i<=n-1;i++)
- {
- printf("%d\n",a[i]);
- }
- }
Posted by
snehil
Size of array is taken from user.. Elements are entered by user in any order... Sorting is performed..
- #include<stdio.h>
- main()
- {
- int a[100],n,k,i,j,temp;
- printf("How many elements: \n");
- scanf("%d",&n);
- printf("Enter the element of array: \n");
- for(i=0;i<=n-1;i++)
- scanf("%d",&a[i]);
- for(k=1;k<=n-1;k++)
- {
- temp=a[k];
- j=k-1;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- while((temp<a[j])&&(j>=0))
- {
- a[j+1]=a[j];
- j=j-1;
- }
- a[j+1]=temp;
- }
- printf("Elements of array after sorting: \n");
- for(i=0;i<=n-1;i++)
- printf("%d\n",a[i]);
- getch();
- }
Posted by
snehil
Wednesday, July 8, 2009
A number is taken from user and sum of all even numbers upto that number is calculated.
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int n,sum=0;
- cout<<"Please enter a number: ";
- cin>>n;
- for (int i=1;i<=n;i++)
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- {
- if(i%2==0)
- sum=sum+i;
- else
- continue;
- }
- cout<<sum;
- getch();
- }
Posted by
snehil
A number n is taken from user.
Sum of all natural numbers upto n is displayed.
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int n,sum=0;
- cout<<"Please enter a number: ";
- cin>>n;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for (int i=1;i<=n;i++)
- sum=sum+i;
- cout<<sum;
-
- getch();
- }
Posted by
snehil
Prime number: A natural number which has exactly two distinct natural number divisors: 1 and itself.
A number is taken from user and is checked whether prime number or not.
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int n,m;
- cout<<"Please enter a number: ";
- cin>>n;
- for (int i=2;i<=(n-1);i++)
- {
- if(n%i==0)
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- {
- cout<<"Not a prime";
- break;
- }
- else
- {
- cout<<"Prime";
- break;
- }
- }
- getch();
- }
Posted by
snehil
numbers from 10 to 1 are displayed...
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- for(int i=10;i>0;i--)
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- { cout<<i;
- cout<<"\n";
- }
- getch();
- }
Posted by
snehil
Even numbers upto 50 are displayed.
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- for(int i=1;i<=50;i++)
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- {if(i%2==0)
- cout<<i;
- cout<<"\n";
- }
- getch();
- }
Posted by
snehil
A number n is taken from the user and then numbers from 1 to n is displayed.
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int lim;
- cout<<"Please enter Limit: ";
- cin>>lim;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for(int i=1;i<=lim;i++)
- { cout<<i;
- cout<<"\n";
- }
- getch();
- }
Posted by
snehil
Sunday, July 5, 2009
Three numbers are taken(Input) from the user and avg of these three nos. is calculated(Output).
- #include <iostream.h>
- #include <conio.h>
- void main()
- {
- clrscr();
- int a,b,c,d,avg;
- cout<<"enter a no. ";
- cin>>a;
- cout<<"enter another no. ";
- cin>>b;
- cout<<"enter another nos ";
- cin>>c;
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- d=a+b;
- avg=d/3;
- cout<<"The average is "<<s;
- getch();
- }