Thursday, 27 March 2014

FINDING COMMON ELEMENTS IN GIVEN LINK LIST

QUES=>  Printing the common elements in two given linked lists.

CODE

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct nodetype
{  int info;
      struct nodetype *next;
}node;

void common (node *head1, node *head2)
{  node *p;
   p=head2;
   printf("\n common elements are :");
   while(head2!=NULL || head1!=NULL)
   {
   head2=p;
   while(head2!=NULL)
   {
     if(head1->info==head2->info)
     printf(" %d ",head1->info);
     head2=head2->next;
   }
     head1=head1->next;
   }
}


void main()
{  node *head1,*head2,*ptr1,*ptr2,*p,*q,*h1,*h2;
int i,j;
   clrscr();
   ptr1=(node*)malloc(sizeof(node));
   ptr2=(node*)malloc(sizeof(node));
   head1=ptr1;
   head2=ptr2;
   h1=ptr1;
   h2=ptr2;
   ptr1->info=1;
   ptr2->info=7;

   for(i=2,j=2; i<=6; i++,j=j+2)
   { p=(node*)malloc(sizeof(node));
     q=(node*)malloc(sizeof(node));
     p->info=i;
     q->info=j;

     ptr1->next=p;
 ptr1=ptr1->next ;

     ptr2->next=q;
 ptr2=ptr2->next ;


   }
   ptr1->next=NULL;
   ptr2->next=NULL;
printf("The link list is : ");
   while(head1!=NULL)
   {   printf(" %d ",head1->info);
       head1=head1->next;
   }
   printf("\nThe link list is : ");
   while(head2!=NULL)
   {   printf(" %d ",head2->info);
       head2=head2->next;
   }
   common(h1,h2);
   getch();

   }


OUTPUT


MERGING OF TWO LINKED LIST

QUES=>Two linked lists are given as
       A: 1, 2, 3, 4, 5, 6
 B:  7, 8, 9, 10, 11, 12
Make a third linked list so that it is in the following order.

 C: 1,7,2,8,…..,6,12


                                           CODE

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct nodetype
{  int info;
      struct nodetype *next;
}node;

void merg (node *head1, node *head2)
{
node *curr,*temp;
   curr=head1;
   temp=head1;
   while(head2!=NULL || head1!=NULL)
   {   head1=head1->next;
    curr->next=head2;
     head2=head2->next;
    curr->next->next=head1;


    curr=head1;
   }
   curr=NULL;
   puts("\nThe merged link list is :");
   while(temp!=NULL)
   {   printf(" %d ",temp->info);
       temp=temp->next;
   }

}


void main()
{  node *head1,*head2,*ptr1,*ptr2,*p,*q,*h1,*h2;
int i,j;
   clrscr();
   ptr1=(node*)malloc(sizeof(node));
   ptr2=(node*)malloc(sizeof(node));
   head1=ptr1;
   head2=ptr2;
   h1=ptr1;
   h2=ptr2;
   ptr1->info=1;
   ptr2->info=7;

   for(i=2,j=8; i<=6,j<=12; i++,j++)
   { p=(node*)malloc(sizeof(node));
     q=(node*)malloc(sizeof(node));
     p->info=i;
     q->info=j;

     ptr1->next=p;
 ptr1=ptr1->next ;

     ptr2->next=q;
 ptr2=ptr2->next ;


   }
   ptr1->next=NULL;
   ptr2->next=NULL;
printf("The link list is : ");
   while(head1!=NULL)
   {   printf(" %d ",head1->info);
       head1=head1->next;
   }
   printf("\nThe link list is : ");
   while(head2!=NULL)
   {   printf(" %d ",head2->info);
       head2=head2->next;
   }
   merg(h1,h2);
   getch();
   }


                                                   OUTPUT



Wednesday, 19 March 2014

PROGRAM TO CREATE,ADD,DELETE,DISPLAY,REVERSE & SEARCH AN ELEMENT IN SINGLY LINKED LIST


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

//Structure declaration for the node

struct node
{
int info;
struct node *link;
}*start;

//This function will create a new linked list

void Create_List(int data)
{
struct node *q,*tmp;

//Dynamic memory is been allocated for a node

tmp= (struct node*)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=NULL;

if(start==NULL) /*If list is empty*/
start=tmp;
else
{ /*Element inserted at the end*/
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}

}/*End of create_list()*/

//This function will add new element at the beginning of the linked list

void AddAtBeg(int data)
{
struct node *tmp;
tmp=(struct node*)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
}/*End of addatbeg()*/

//This function will add new element at any position

void AddAfter(int data,int pos)
{
struct node *tmp,*q;
int i;
q=start;
//Finding the position to add new element to the linked list
for(i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
printf ("\n\n There are less than %d elements",pos);
getch();
return;
}
}/*End of for*/
tmp=(struct node*)malloc(sizeof (struct node));
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
}/*End of addafter()*/

//Delete any element from the linked list
void Del(int data)
{
struct node *tmp,*q;
if (start->info == data)
{

tmp=start;
start=start->link; /*First element deleted*/
free(tmp);
return;
}
q=start;
while(q->link->link != NULL)
{
if(q->link->info == data) /*Element deleted in between*/
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
return;
}
q=q->link;
}/*End of while */
if(q->link->info==data) /*Last element deleted*/
{
tmp=q->link;
free(tmp);
q->link=NULL;
return;
}
printf ("\n\nElement %d not found",data);
getch();
}/*End of del()*/

//This function will display all the element(s) in the linked list
void Display()
{
struct node *q;
if(start == NULL)
{
printf ("\n\nList is empty");
return;
}
q=start;
printf("\n\nList is : ");
while(q!=NULL)
{
printf ("%d ", q->info);
q=q->link;

}
printf ("\n");
getch();
}/*End of display() */
//Function to count the number of nodes in the linked list
void Count()
{
struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->link;
cnt++;
}
printf ("Number of elements are %d\n",cnt);
getch();
}/*End of count()*/
//This function will reverse the linked list
void Rev()
{
struct node *p1,*p2,*p3;
if(start->link==NULL) /*only one element*/
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}/*End of rev()*/

//Function to search an element from the linked list

void Search(int data)
{

struct node *ptr = start;
int pos = 1;
//searching for an element in the linked list
while(ptr!=NULL)
{
if (ptr->info==data)
{
printf ("\n\nItem %d found at position %d", data, pos);
getch();
return;
}
ptr = ptr->link;
pos++;
}
if (ptr == NULL)
printf ("\n\nItem %d not found in list",data);
getch();
}

void main()
{
int choice,n,m,position,i;
start=NULL;
while(1)
{
clrscr();
printf ("1.Create List\n");
printf ("2.Add at beginning\n");
printf ("3.Add after \n");
printf ("4.Delete\n");
printf ("5.Display\n");
printf ("6.Count\n");
printf ("7.Reverse\n");
printf ("8.Search\n");
printf ("9.Quit\n");
printf ("\nEnter your choice:");
scanf ("%d",&choice);
switch (choice)
{
case 1:
printf ("\n\nHow many nodes you want:");
scanf ("%d",&n);

for(i = 0;i<n;i++)
{
printf ("\nEnter the element:");
scanf ("%d",&m);
Create_List(m);
}
break;
case 2:
printf ("\n\nEnter the element : ");
scanf ("%d",&m);
AddAtBeg(m);
break;
case 3:
printf ("\n\nEnter the element:");
scanf ("%d",&m);
printf ("\nEnter the position after which this element is inserted:");
scanf ("%d",&position);
AddAfter(m,position);
break;
case 4:
if (start == NULL)
{
printf("\n\nList is empty");
continue;
}
printf ("\n\nEnter the element for deletion:");
scanf ("%d",&m);
Del(m);
break;
case 5:
Display();
break;
case 6:
Count();
break;
case 7:
Rev();
break;
case 8:
printf("\n\nEnter the element to be searched:");
scanf ("%d",&m);

Search(m);
break;
case 9:
exit(0);
default:
printf ("\n\nWrong choice");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/ 

WATER FLOATING BOT FROM RAW MATERIALS

What you need?
1.bottle
2.scale
3.2 x CPU Fans
4.2 DPDT
5.M-seal
6.Soap Tray