Read in your tongue here..

Friday, June 28, 2013

Doubly Linked List

#include<alloc.h>
struct list{
     int data;
     struct list *prev,*next;
}*head=NULL,*temp,*node;
void infirst(void), inpos(void), inlast(void), delfront(void), delpos(void), dellast(void), display(void), count(void);
int num,pos,empty(void);
char con;
int empty(void)
{
     if(head==NULL)
     {
           clrscr();
           printf("The list is empty!!");
           return 1;
     }
     return 0;
}

void main()
{
     printf("\nMain Menu\n1.Insert at first\n2.Insert at specified position\n3.Insert at last\n4.Delete first\n5.Delete specified position\n6.Delete from last\n7.Display\n8.Count number of nodes\n9.Exit\nEnter choice:");
     scanf("%d",&num);
     switch(num)
     {
           case 1:
                infirst();
                break;
           case 2:
                inpos();
                break;
           case 3:
                inlast();
                break;
          case 4:
                delfront();
                break;
           case 5:
                delpos();
                break;
           case 6:
                dellast();
                break;
           case 7:
                display();
                break;
           case 8:
                count();
                break;
           case 9:
                clrscr();
                exit();
           default:
                clrscr();
                puts("\nWRONG INPUT!!");
     }
     main();
}
void infirst(void)
{
     do
     {
           printf("\nEnter number:");
           scanf("%d",&num);
           temp=(struct list*)malloc(sizeof(struct list));
           temp->data=num;
           temp->next=head;
           temp->prev=NULL;
           head=temp;
           printf("\nContinue [y/n]?:");
           con=getche();
     }while(con=='y'||con=='Y');
}
void inpos(void)
{
     do
     {
           num=0;
           temp=head;
           printf("\nEnter position to insert at:");
           scanf("%d",&pos);
           while(temp->next!=NULL)
           {
                num++;
                if(num==pos-1)
                     break;
                temp=temp->next;
           }
           printf("\nEnter number:");
           scanf("%d",&num);
           node=(struct list*)malloc(sizeof(struct list));
           node->data=num;
           node->next=temp->next;
           node->prev=temp;
           temp->next=node;
           printf("\nContinue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}
void inlast(void)
{
     do
     {
           printf("\nEnter data:");
           scanf("%d",&num);
           temp=head;
           while(temp->next!=NULL)
                temp=temp->next;
           node=(struct list *)malloc(sizeof(struct list));
           node->data=num;
           node->next=NULL;
           node->prev=temp;
           temp->next=node;
           printf("\nContinue [y/n]?:");
           con=getche();
     } while(con=='y'||con=='Y');
}

void delfront(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                temp=head;
                head=temp->next;
                head->prev=NULL;
                free(temp);
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void delpos(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                num=0;
                temp=head;
                printf("\nEnter position:");
                scanf("%d",&pos);
                while(temp->next!=NULL)
                {
                num++;
                if(num==pos-1)
                     break;
                temp=temp->next;
                }
                node=temp->next->next;
                free(temp->next);
                temp->next=node;
                node->prev=temp;
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void dellast(void)
{
     do
     {
           if(empty()==1)
                break;

           else
           {
                temp=head;
                while(temp->next!=NULL)
                {
                     node=temp;
                     temp=temp->next;
                }
                node->next=NULL;
                free(temp);
                printf("\nContinue [y/n]?:");
                con=getche();
           }
     } while(con=='y'||con=='Y');
}
void display(void)
{
     if(empty()==1)
return;
     else
     {
           puts("The list is:");
           for(temp=head;temp!=NULL;temp=temp->next)
                printf("%d ",temp->data);
     }
}
void count(void)
{
     num=0;
     temp=head;
     while(temp!=NULL)
     {
           num++;
           temp=temp->next;
     }
     clrscr();
     printf("\nTotal number of nodes=%d",num);
}



Digits in words(till 100)

void main()
{
     char n1[10][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eigth","Nine"};
     char n2[10][10]={"Ten","Eleven","Tweleve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
     char n3[10][10]={"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninty","Hundred"};
     int n;
     printf("Enter digit:");
     scanf("%d",&n);
     clrscr();
     printf("%d in words: ",n);
     if(n>=0&&n<=100)
     {
           if(n<=9)
                printf("%s",n1[n]);
           else if(n<=19)
                printf("%s",n2[n%10]);
           else if(n%10==0)
                printf("%s",n3[n/10-2]);
           else
           {
                printf("%s ",n3[n/10-2]);
                printf("%s",n1[n%10]);
           }
     }
     else
     {
           clrscr();
           printf("WRONG INPUT!!");
           getch();
           exit(0);
     }
     getch();
     clrscr();
}



Circular Queue

#define size 5
void in(void),del(void),dis(void);
int que[size],num,n=0,front=0,rear=-1;
char con;
void main()
{
      printf("\n\n\tMain Menu\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter choice:");
      scanf("%d",&num);
      clrscr();
      switch(num)
      {
            case 1:
                  in();
                  break;
            case 2:
                  del();
                  break;
            case 3:
                  dis();
                  break;
            case 4:
                  exit();
            default:
                  puts("\nWRONG INPUT!!!");
      }
      main();
}
void in(void)
{
       do
       {
            if(n==size)
            {
                  clrscr();
                  puts("\nQueue is full!!!");
                  break;
            }
            printf("\nEnter number:");
            scanf("%d",&num);
            rear=(rear+1)%size;
            que[rear]=num;
            n++;
            printf("Continue? [y/n]:");
            con=getche();
       }while(con=='y'||con=='Y');
}

void del(void)
{
      do
      {
            if(n==0)
            {
                  clrscr();
                  puts("Queue is empty!!!");
                  break;
            }
            front=(front+1)%size;
            n--;
            printf("Continue? [y/n]:");
            con=getche();
      }while(con=='y'||con=='Y');
}
void dis(void)
{
      int i;
      clrscr();
      if(n==0)
      {
            puts("Queue is empty!!!");
            return;
      }
      printf("The queue is: ");
      for(i=front;i!=rear;i=(i+1)%size)
            printf("%d ",que[i]);
      printf("%d",que[i]);

}