Thursday, March 29, 2012

LinkList - Polynomial Addition


Polynomial
Polynomial,  expression consisting of any number of terms, each of which contains a constant, called a coefficient, and one or more variables raised to an integral (whole number) power, as Exponent.
ex. 2x2 + 3x + 8.



#include <stdio.h>
#include <conio.h>

  struct poly
      {           
              int coff;
              int exp;
              struct poly *next;
      };
           typedef struct poly pol;
           pol* Insert(pol *start,float co, int ex)
              {
                   pol *temp,*pr;
                   int fl=0;
                   for(pr=start;pr!=NULL;pr=pr->next)
                       {
                            if(ex==pr->exp)
                                {
                                     pr->coff=co+pr->coff;
                                     return start;
                                }
                       }
                   temp=(pol *)malloc(sizeof(pol)); ;
                   temp->coff=co;
                   temp->exp=ex;
                   if(start==NULL)
                       {
                            start=temp;
                            temp->next=NULL;
                       }
                   else
                       {
                            for(pr=start;pr->next!=NULL;pr=pr->next);
                            pr->next=temp;
                            temp->next=NULL;
                       }
              return start;
              }

      pol* polyadd(pol *start,pol *start1)
              {
                   pol *pr,*ptr;
                   pol *PD=NULL;
                   int cf,ex,cff=0,f;
                   for(pr=start;pr!=NULL;pr=pr->next)
                       {
                            cf=pr->coff;
                            ex=pr->exp;
                            f=0;
                            for(ptr=start1;ptr!=NULL;ptr=ptr->next)
                                {
                                     if(pr->exp==ptr->exp)
                                          {
                                               cff=cf+ptr->coff;
                                               f=1;
                                          }

                                }
                        if(f==1)
                            {
                                PD=Insert(PD,cff,pr->exp);
                            }
                            else
                                PD=Insert(PD,cf,pr->exp);
                       }

                       for(ptr=start1;ptr!=NULL;ptr=ptr->next)
                       {  f=0;
                            for(pr=start;pr!=NULL;pr=pr->next)
                                {
                                     if(ptr->exp==pr->exp)
                                          {
                                               f=1;
                                          }
                                }
                            if(f==0)
                                {
                                     PD=Insert(PD,ptr->coff,ptr->exp);
                                }
                       }
                     return PD;
              }

           void display(pol *start)
              {
                   pol *pr;
                   for(pr=start;pr!=NULL;pr=pr->next)
                       {
                            printf("%dx^%d", pr->coff,pr->exp);
                            if(pr->next!=NULL)
                                {
                                     printf("+");
                                }
                       }
              }
  void main()
      {
           int choice,ch,co,ex;
           pol *head,*head1,*head2;
           head=head1=head2=NULL;
           clrscr();
           while(choice!=4)
              {
                   printf("\n\n\t1. Insert");
                   printf("\n\t2. Display");
                   printf("\n\t3. Add");
                   printf("\n\t4. Exit");
                   printf("\n\tEnter a Choice : ");
                   scanf("%d",&choice);
                   switch(choice)
                       {
                            case 1:
                                          printf("\n\tPoly 1 or 2?\t");
                                          scanf("%d",&ch);
                                          printf("\n\tEnter Co-eff : ");
                                          scanf("%d",&co);
                                          printf("\n\tEnter Exp. : ");
                                          scanf("%d",&ex);
                                          switch(ch)
                                               {
                                                    case 1:
                                                                   head=Insert(head,co,ex);
                                                                   break;
                                                    case 2:
                                                                   head1=Insert(head1,co,ex);
                                                                   break;
                                                    default:
                                                                   printf("\n\t*Invalid Input*");
                                               }
                                          break;
                            case 2:
                                          printf("\n\t1. Poly-1");
                                          printf("\n\t2. Poly-2");
                                          printf("\n\t3. Final Poly?\t");
                                          scanf("%d",&ch);
                                          switch(ch)
                                               {
                                                    case 1:
                                                                   printf("\n\tFirst Poly : ");
                                                                   display(head);break;
                                                    case 2:
                                                                   printf("\n\tSecond Poly : ");
                                                                   display(head1);break;
                                                    case 3:
                                                                   printf("\n\tResulatant Poly : ");
                                                                   display(head2);break;
                                               }
                                          break;
                            case 3:
                                          head2=polyadd(head,head1);break;
                            case 4:
                                          printf("\n\tQuit....");
                                          getch();
                                          free(head);
                                          free(head1);
                                          free(head2);
                                          exit(0);
                       }
              }
      }

Thursday, March 15, 2012

Circular LinkList & Circular Queue




Circular Link List - A less common convention is to make it point to the first node of the list; in that case the list is said to be circular or circularly linked; otherwise it is said to be open or linear.



#include <stdio.h>
#include <conio.h>

//Circular LinkList

  struct Queue
      {
           int n;
           struct Queue *next;
      }*front, *rear,*cur,*prt;
  void push()
      {
           prt=(struct Queue *)malloc(sizeof(struct Queue));
           printf("\n\tEnter a no. : ");
           scanf("%d",&prt->n);
           prt->next=NULL;
           if(front==NULL)
              {
                   front=rear=prt;
              }
           else
              {
                   rear->next=prt;
                   rear=prt;
              }
      }

  void pop()
      {
           if(front==NULL)
              {
                   printf("\n\tQueue is Empty!");
                   getch();
                   return;
              }
           else
              {
                   prt=front;
                   front=front->next;
                   printf("\n\tNo. Poped - %d ",prt->n);
                   free(prt);
              }
           getch();
      }

  void display()
      {
           if(front==NULL)
              {
                   printf("\n\tQueue is Empty!");
                   getch();
                   return;
              }
           else
              {
                   printf("\n\tValues in Queue : ");
                   for(cur=front;cur;cur=cur->next)
                        {
                            printf(" %d ",cur->n);
                        }
                   getch();
              }
      }
  void main()
      {
           int c=0;
           char a;
           clrscr();
           front=rear=cur=prt=NULL;
           while(c!=4)
              {
                   clrscr();
                   printf("\n\t1. Push ");
                   printf("\n\t2. Pop");
                   printf("\n\t3. Display ");
                   printf("\n\t4. Exit ");
                   printf("\n\tEnter a Choice : ");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1: push();
                                                break;
                            case 2: pop(front);
                                                break;
                            case 3: display();
                                                break;
                            case 4: printf("\n\t\tQuit...");
                                                getch();
                                                exit(0);
                        }
              }
           getch();
      }



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//Circular Queue using LinkList

  typedef struct CList
      {
           int n;
           struct CList *next;
      }*Cnode;
  Cnode NewNode,temp,last;
  typedef Cnode Cqueue;
  Cqueue CreateQ()
      {
           Cqueue cq;
           cq=(Cqueue)malloc(sizeof(struct CList));
           if(cq==NULL)
              {
                   printf("\n\tNull Value \n");
                   return NULL;
              }
           cq->next=cq;
           return cq;
      }

  void disp(Cqueue cq)
      {
           if(isempty(cq))
              {
                   printf("\n\tList is Empty!\n");
              }
          else
              {
                   printf("\n\tValues in the List : ");
                   temp=cq->next;
                   while(temp!=cq)
                        {
                            printf("%d\t",temp->n);
                            temp=temp->next;
                        }
                   printf("\n");
              }
      }

  int isempty(Cqueue cq)
      {
           return(cq->next==cq);
      }

  Cqueue Push(Cqueue cq,int num)
      {
           Cnode NewNode;
           NewNode=(Cnode)malloc(sizeof(struct CList));
           NewNode->n=num;
           if(isempty(cq))
              {
                   NewNode->next=cq;
                   cq->next=NewNode;
                   last=NewNode;
              }
           else
              {
                   last->next=NewNode;
                   NewNode->next=cq;
                   last=NewNode;
              }
           return cq;
      }

  int Pop(Cqueue cq)
      {
           int num;
           if(isempty(cq))
              {
                   printf("\n\tUnderflow Condition!\n");
                   return -1;
              }
           else
              {
                   num=(cq->next)->n;
                   temp=cq->next;
                   cq->next=(cq->next)->next;
                   free(temp);
                   return num;
              }
      }

  void main()
      {
           int c,num;
           Cqueue cq;
           cq=CreateQ();
           clrscr();
           while(1)
              {
                   printf("\n\t1. Push");
                   printf("\n\t2. Display");
                   printf("\n\t3. Pop");
                   printf("\n\t4. Exit");
                   printf("\n\tEnter your choice: ");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1: printf("\n\tEnter the Values: ");
                                                scanf("%d",&num);
                                                Push(cq,num);
                                                break;
                            case 2:
                                                disp(cq);
                                                break;
                            case 3:  num=Pop(cq);
                                                if(num!=-1)
                                                printf("\n\tValue Popped -  %d\n",num);
                                                break;
                            case 4:
                                                printf("\n\tQuit..,.");
                                                getch();
                                                exit(0);

                            default:
                                                printf("\nInvalid choice");
                   }
              }
           getch();
      }