Monday 2 April 2012

Program : Addition of two polynomial equation


Program : Addition of two polynomial equation

// Add two  polynomial equation using linked list

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

struct link
{
          int coef;
          int exp;
          struct link *next;
};


struct link *node,*start,*new1,*pre;
int i;

void create_poly(struct link *node);
void display(struct link *node);
void add_poly(struct link *node);

void main()
{
          clrscr();
          printf("\n create polynomial equation");
          node=(struct link *)malloc(sizeof(struct link));
          create_poly(node);
          printf("\n Polynomial equation is as follow\n");
          display(node);
          add_poly(node);
          printf("\n after addition polynomial equation :");
          display(node);
          getch();
}

void create_poly(struct link *node)
{
          char ans;
          i=0;
          start->next=NULL;
          node=start;
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter Coeficient value : ");
                   scanf("%d",&node->coef);
                   printf("\n Enter exponent value   : ");
                   scanf("%d",&node->exp);

                   node->next=NULL;

                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
}

void display(struct link *node)
{
          node=start->next;
          while(node)
          {
                   printf("%dX ^ %d  ",node->coef,node->exp);
                   node=node->next;
                   if(node!=NULL)
                     printf(" + ");
          }

}

void add_poly(struct link *node)
 {
    int t_coef,t_exp;
    char ans;
    struct link *temp,*tp;
    node=start->next;
    pre=start;
    printf("\n Enter 'n' for break:-");
    ans=getchar();
          while(ans!='n')
          {
                   printf("\n Enter Coeficient value : ");
                   scanf("%d",&t_coef);
                   printf("\n Enter exponent value   : ");
                   scanf("%d",&t_exp);

                   pre=start;
                   node=start->next;

                   while(node)
                    {
                      if(t_exp > node->exp)
                       {
                         new1=(struct link *)malloc(sizeof(struct link ));
                         new1->next=node;
                         pre-> next=new1;
                         new1->coef=t_coef;
                         new1->exp=t_exp;
                         break;
                       }
                    else
                    {
                      if(t_exp == node->exp)
                             {
                                node->coef=node->coef+t_coef;
                                node->exp=t_exp;
                                break;
                             }
                    }

                    pre=pre->next;
                    node=node->next;
                 }
             fflush(stdin);
             printf("\n Enter 'n' for break:-");
             ans=getchar();
     }
  }

Posted By :Ruchita Pandya

Program : Create Polynomial equation using Linked List


Program : Create Polynomial equation  using Linked List

Polynomial equation  
5x^3 +4x^2+6x^1

// Create polynomial equation using linked list

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

struct link
{
          int coef;
          int exp;
          struct link *next;
};

struct link *node,*start;
int i;

void create_poly(struct link *node);
void display(struct link *node);

void main()
{
          clrscr();
          printf("\n create polynomial equation");
          node=(struct link *)malloc(sizeof(struct link));
          create_poly(node);
          printf("\n Output\n");
          display(node);
          getch();
}

void create_poly(struct link *node)
{
          char ans;
          i=0;
          start->next=NULL;
          node=start;
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter Coeficient value : ");
                   scanf("%d",&node->coef);
                   printf("\n Enter exponent value   : ");
                   scanf("%d",&node->exp);

                   node->next=NULL;

                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
}

void display(struct link *node)
{
          node=start->next;
          while(node)
          {
                   printf("%dX ^ %d",node->coef,node->exp);
                   node=node->next;
                   if(node!=NULL)
                     printf(" + ");
          }

}

Posted By  : Ruchita Pandya

Program : Circular Header Linked List – Create – Insertion


Program  : Circular Header Linked List – Create – Insertion

// Insertion in Cicular Header Linked List

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

struct link
{
          int no;
          struct link *next;
};

struct link *node,*start,*pre,*new1;
int i=0;

void create_link(struct link *node);
void display(struct link *node);
void insert(struct link *node);

void main()
{
          clrscr();

          node=(struct link *)malloc(sizeof(struct link));
          create_link(node);

          printf("\n Output\n");

          display(node);

          insert(node);
          printf("\n list after insertion ");
          display(node);

                   getch();
}

void create_link(struct link *node)
{
          char ans;
          node=start;
          node->next= (struct link *)malloc(sizeof(struct link));
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter data for node:-");
                   scanf("%d",&node->no);
                   node->next=start;
                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
          printf("\n Total node in circular header linked list : %d",i);
          node=start;
          node->no=i;// save total no. of node in header node


}

void display(struct link *node)
{
          int count;
          node=start;
          count=node->no;
          node=node->next;

          while(count>0)
          {
                   printf("\n  %d address = %u ",node->no,node);
                   node=node->next;
                   count--;
          }
}

void insert(struct link *node)
{
          int node_no=1,insert_no,flag=0,count;

          node=start;
          count=node->no;

          node=node->next;
          pre=start;

          printf("\n Enter position where you want to insert new node:-");
          scanf("%d",&insert_no);

          while(count>0)
          {
                   if(node_no==insert_no)
                   {
                             new1=(struct link *)malloc(sizeof(struct link));
                             printf("\n Insert data for new node : ");
                             scanf("%d",&new1->no);
                             pre->next=new1;
                             new1->next=node;
                             flag=1;

                             break;
                   }
                   else
                   {
                             pre=pre->next;
                             node=node->next;
                   }
                   node_no++;
                   count--;
          }

          if(flag==0)
          {
                   printf("\n Position not found");
          }
          else
          {
          node=start;
          node->no=node->no+1;
      }

}

Posted By  :Ruchita Pandya

Program : Grounded Header Linked List – Create – Insertion


Program : Grounded Header Linked List – Create – Insertion

// Insertion in  grounded Header Linked List

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

struct link
{
          int no;
          struct link *next;
};

struct link *node,*start,*pre,*new1;
int i=0;

void create_link(struct link *node);
void display(struct link *node);
void insert(struct link *node);

void main()
{
          clrscr();

          node=(struct link *)malloc(sizeof(struct link));
          create_link(node);

          printf("\n Output\n");

          display(node);

          insert(node);
          printf("\n list after insertion ");
          display(node);

          getch();
}

void create_link(struct link *node)
{
          char ans;

          start->next=NULL;
          node=start;
          node->next= (struct link *)malloc(sizeof(struct link));

          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter data for node:-");
                   scanf("%d",&node->no);
                   node->next=NULL;
                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
          printf("\n Total node in header linked list : %d",i);
          node=start;
          node->no=i;// save total no. of node in header node
}

void display(struct link *node)
{
          int count;
          node=start;
          count=node->no;
          node=node->next;
          while(count>0)
          {
                   printf("\n  %d",node->no);
                   node=node->next;
                   count--;
          }
}

void insert(struct link *node)
{
          int node_no=1,insert_no,flag=0,count;

          node=start;
          count=node->no;

          node=node->next;
          pre=start;

          printf("\n Enter position where you want to insert new node:- ");
          scanf("%d",&insert_no);

          while(count>0)
          {
                   if(node_no==insert_no)
                   {
                             new1=(struct link *)malloc(sizeof(struct link));
                             printf("\n Insert data for new node : ");
                             scanf("%d",&new1->no);
                             pre->next=new1;
                             new1->next=node;
                             flag=1;

                             break;
                   }
                   else
                   {
                             pre=pre->next;
                             node=node->next;
                   }
                   node_no++;
                   count--;
          }

          if(flag==0)
          {
                   printf("\n Position not found");
          }
          else
          {
          node=start;
          node->no=node->no+1;
      }
}


Posted By : Ruchita Pandya

Program – Circular Linked List – Create - insertion – Deletion


Program – Circular Linked List – Create - insertion – Deletion

// Create and traverse Circular linked list
// Insert first, last and node at desire position
// Delete first, last and node at desire position

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

struct link
{
          int no;
          struct link *next;
};
int i=0;
struct link *node,*start,*pre,*new1;

void create_link(struct link *node);
void display(struct link *node);
void insert_first(struct link *node);
void insert_last(struct link *node);
void insert_desireposition(struct link *node);
void delete_first(struct link *node);
void delete_last(struct link *node);
void delete_desireposition(struct link *node);

void main()
{
          int ch;
          clrscr();
          while(1)
          {
          printf("\n Circular Linked List");
          printf("\n---------------------------");
          printf("\n 1. Create Circular Linked List");
          printf("\n 2. Traverse Link List");
          printf("\n 3. Insert First node");
          printf("\n 4. Insert Last node");
          printf("\n 5. Insert node at Desire Position");
          printf("\n 6. Delete First node");
          printf("\n 7. Delete Last node");
          printf("\n 8. Delete node at Desire Position");
          printf("\n 9. Exit");
          printf("\n-----------------------------");

          printf("\n Enter your choice : ");
          scanf("%d",&ch);

                   switch(ch)
                   {
                             case 1:
                                      node=(struct link *)malloc(sizeof(struct link));
                                      create_link(node);
                                      break;
                             case 2:
                                      printf("\n Output\n");
                                      display(node);
                                       break;
                             case 3:
                                      insert_first(node);
                                      printf("\n After Inserting First Node");
                                      display(node);
                                      break;
                             case 4:
                                      insert_last(node);
                                      printf("\n After Inserting Last Node");
                                      display(node);
                                      break;
                             case 5:
                                      insert_desireposition(node);
                                      printf("\n After Inserting At Desire Position");
                                      display(node);
                                      break;
                             case 6:
                                      delete_first(node);
                                      printf("\n After Deleting First Node");
                                      display(node);
                                      break;
                             case 7:
                                      delete_last(node);
                                      printf("\n After Deleting Last Node");
                                      display(node);
                                      break;
                             case 8:
                                      delete_desireposition(node);
                                      printf("\n After Deleting At Desire Position");
                                      display(node);
                                      break;

                             case 9:
                                      exit(0);
                                      break;
                             default:
                                      printf("\n Wrong Choice");
                   }
          }
          getch();
}

void create_link(struct link *node)
{
          char ans;
          start->next=NULL;
          node=start;
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;

                   printf("\n Enter data for node:-");
                   scanf("%d",&node->no);
                   node->next=start;
                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
}

void display(struct link *node)
{
          int count;
          node=start->next;
          count=i;

          while(count)
          {
                   printf("\n  %d",node->no);
                   node=node->next;
                   count--;
          }
}

void insert_first(struct link *node)
{
          node=start->next;
          pre=start;
          new1=(struct link *)malloc(sizeof(struct link));
          printf("\n Insert data for first node:-");
          scanf("%d",&new1->no);
          pre->next=new1;
          new1->next=node;
          i++;
}

void insert_last(struct link *node)
{
          int count;
          node=start->next;
          pre=start;
          count=i;
          while(count)
          {
                   pre=pre->next;
                   node=node->next;
                   count--;
          }
          new1=(struct link *)malloc(sizeof(struct link));
          printf("\n Insert data for last node:-");
          scanf("%d",&new1->no);
          pre->next=new1;
          new1->next=node;
          i++;
}

void insert_desireposition(struct link *node)
{
          int node_no=1,insert_no,flag=0,count;
          node=start->next;
          pre=start;
          count=i;
          printf("\n Enter position where you want to insert new node:-");
          scanf("%d",&insert_no);

          while(count)
          {
                   if(node_no==insert_no)
                   {
                             new1=(struct link *)malloc(sizeof(struct link));
                             printf("\n Insert data for new node:-");
                             scanf("%d",&new1->no);
                             pre->next=new1;
                             new1->next=node;
                             flag=1;
                             break;
                   }
                   else
                   {
                             pre=pre->next;
                             node=node->next;
                   }
                   node_no++;
                   count--;
          }
          if(flag==0)
          {
                   printf("\n Position not found");
          }
          else
          {
                   i++;
          }
}

void delete_first(struct link *node)
{
          node=start->next;
          pre=start;
          if(i==0)
          {
                   printf("\n List is empty");
                   exit(0);
          }
          pre->next=node->next;
          free(node);
          i--;
}

void delete_last(struct link *node)
{
          int node_no=0,count;
          node=start->next;
          pre=start;
          count=i;
          if(i==0)
          {
                   printf("\n List is empty");
                   exit(0);
          }
          while(count)
          {
                   node_no++;
                   pre=pre->next;
                   node=node->next;
                   count--;
          }
          node=start->next;
          pre=start;
          while(node_no!=1)
          {
                   node_no--;
                   pre=pre->next;
                   node=node->next;
          }
          if(node_no==1)
          {
                   pre->next=node->next;
                   free(node);
                   i--;
          }
}

void delete_desireposition(struct link *node)
{
          int node_no=1,delete_no,flag=0,count;
          node=start->next;
          pre=start;
          count=i;
          printf("\n Enter position where you want to delete node:-");
          scanf("%d",&delete_no);

          while(count)
          {
                   if(node_no==delete_no)
                   {
                             pre->next=node->next;
                             free(node);
                             flag=1;
                             break;
                   }
                   else
                   {
                             pre=pre->next;
                             node=node->next;
                   }
                   node_no++;
                   count--;
          }
          if(flag==0)
          {
                   printf("\n Position not found");
          }
          else
          {
                   i--;
          }
}

Posted By : Ruchita Pandya