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
User is asked to Insert into.. Delete from or Display the entire queue..
- #include<stdio.h>
- #include<conio.h>
- #include<process.h>
- int queue[5];
- long front,rear;
- void initqueue();
- void display();
- void main()
- {
- int choice,info;
- clrscr();
- while(1)
- {
- clrscr();
- printf(" MENU \n");
- printf("1.Insert an element in queue\n");
- printf("2.Delete an element from queue\n");
- printf("3.Display the queue\n");
- printf("4.Exit!\n");
- printf("Your choice: ");
- scanf("%i",&choice);
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- switch(choice)
- {
- case 1:if(rear<4)
- {
- printf("enter the number: ");
- scanf("%d",&info);
- if (front==-1)
- {
- front=0;
- rear=0;
- }
- else
- rear=rear+1;
- queue[rear]=info;
- }
- else
- printf("queue is full");
- getch();
- break;
- case 2: int info;
- if(front!=-1)
- {
- info=queue[front];
- if(front==rear)
- {
- front=-1;
- rear=-1;
- }
- else
- front=front+1;
- printf("no deleted is = %d",info);
- }
- else
- printf("queue is empty");
- getch();
- break;
- case 3: display();
- getch();
- break;
- case 4: exit(1);
- break;
- default:printf("You entered wrong choice!");
- getch();
- break;
- }
- }
- }
- void initqueue()
- {
- front=rear=-1;
- }
- void display()
- {
- int i;
- for(i=front;i<=rear;i++)
- printf("%i\n",queue[i]);
- }
Posted by
snehil
User is asked to PUSH, POP , or TRAVERSE.. PUSH is to add items to the Stack, POP is to remove item from the Stack, and TRAVERSE is to traverse the whole Stack
- #include<stdio.h>
- #include<conio.h>
- #include<process.h>
- #define MAXSIZE 10
- void push();
- int pop();
- void traverse();
- int stack[MAXSIZE];
- int Top=-1;
- void main()
- {
- int choice;
- char ch;
- do
- {
- clrscr();
- printf("\n1. PUSH ");
- printf("\n2. POP ");
- printf("\n3. TRAVERSE ");
- printf("\nEnter your choice ");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1: push();
- break;
- case 2: printf("\nThe deleted element is %d ",pop());
- break;
- case 3: traverse();
- break;
- default: printf("\nYou Entered Wrong Choice");
- }
- printf("\nDo You Wish To Continue (Y/N)");
- fflush(stdin);
- scanf("%c",&ch);
- }
- while(ch=='Y' || ch=='y');
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void push()
- {
- int item;
- if(Top == MAXSIZE - 1)
- {
- printf("\nThe Stack Is Full");
- getch();
- exit(0);
- }
- else
- {
- printf("Enter the element to be inserted ");
- scanf("%d",&item);
- Top= Top+1;
- stack[Top] = item;
- }
- }
-
- int pop()
- {
- int item;
- if(Top == -1)
- {
- printf("The stack is Empty");
- getch();
- exit(0);
- }
- else
- {
- item = stack[Top];
- Top = Top-1;
- }
- return(item);
- }
-
- void traverse()
- {
- int i;
- if(Top == -1)
- {
- printf("The Stack is Empty");
- getch();
- exit(0);
- }
- else
- {
- for(i=Top;i>=0;i--)
- {
- printf("Traverse the element ");
- printf("%d\n",stack[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>
- #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
Friday, September 18, 2009
Posted by
snehil
Friday, September 4, 2009
The difference between a++ and ++a, is that when we use a++ in a program and then the effect would be on the next statement while the ++a, effect is that on the current line not on the next statement.
Output:
Example code:
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int a,b;
- a=1;
- cout<<a<<endl;
- cout<<a++<<endl;
- cout<<a<<endl<<endl;
- b=5;
- cout<<b<<endl;
- cout<<++b<<endl;
- cout<<b<<endl;
- getch();
- }
Posted by
snehil
Thursday, July 16, 2009
Name age and address of students are stored in a string..
- #include<iostream.h>
- #include<conio.h>
- #include<stdio.h>
- void main()
- {
- clrscr();
- char name[100],age[100],address[100];
- cout<<"Please enter a name: ";
- gets(name);
- cout<<"Please enter age: ";
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- gets(age);
- cout<<"Please eneter address: ";
- gets(address);
- cout<<endl<<endl<<"Name is: "<<name<<endl<<"Age is: "<<age<<endl<<"Address is:
- "<<address;
- getch();
- }
Posted by
snehil
A line is taken from user and number of words is calculated..
- #include<iostream.h>
- #include<conio.h>
- #include<stdio.h>
- void main()
- {
- clrscr();
- char str[100];
- int w=1;
- cout<<"Please enter a String: ";
- gets(str);
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- for (int i=0;str[i]!='\0';i++)
- {
- if(str[i]==' ')
- w++;
- }
- cout<<"There are "<<w<<" words.";
- getch();
- }