Saturday, 13 August 2011

Singly Linked List

#include
#include
#include

typedef struct student node;
void Create();
void Create1();
void Create2(int);

void Display(node *,int);
void Concate();
void Merge();
void Union();
void Intersection();
void FreeList(node *);


struct student
{
int rollno;
struct student *next;
}*first,*first1,*first2,*newrec,*newrec1,*newrec2,*last,*last1,*last2;


void main()
{
int choice=0,i,n;
clrscr();
while(choice!=8)
{
printf("\n===============MENU=================");
printf("\n| 1. Create 1st Singly Linked List |");
printf("\n| 2. Create 2nd Singly Linked List |");
printf("\n| 3. Display |");
printf("\n| 4. Concate |");
printf("\n| 5. Merge |");
printf("\n| 6. Union |");
printf("\n| 7. Intersection |");
printf("\n| 8. Exit |");
printf("\n====================================");
printf("\n Enter the Choice :->");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\n ENTER NUMBER OF NODES IN SINGLY LINKED LIST 1 :-");
scanf("%d",&n);
for(i=0;irollno);
newrec->next=NULL;
if(first==NULL)
first=newrec;
else
last->next=newrec;

last=newrec;

}
void Create1()
{
node *ptr;
newrec1=(node *)malloc(sizeof(node));
printf("\n Enter the Roll no : ");
scanf("%d",&newrec1->rollno);
newrec1->next=NULL;
if(first1==NULL)
first1=newrec1;
else
last1->next=newrec1;

last1=newrec1;

}


void Create2(int data)
{
node *ptr;
newrec2=(node *)malloc(sizeof(node));
newrec2->rollno=data;
newrec2->next=NULL;
if(first2==NULL)
first2=newrec2;
else
last2->next=newrec2;

last2=newrec2;

}

void Display(node *first,int n)
{
node *ptr;
int srno=0;
ptr=first;
if(ptr==NULL)
{
printf("\n Linked List no. %d is empty ",n);
return;
}
printf("\n------------------------");
printf("\nSr. No. Record Value ");
while(ptr!=NULL)
{
srno++;
printf("\n %d %d ",srno,ptr->rollno);
ptr=ptr->next;
}
printf("\n------------------------");

}

void Concate()
{
node *ptr;
ptr=first;

FreeList(first2);

while(ptr!=NULL)
{
Create2(ptr->rollno);
ptr=ptr->next;
}
ptr=first1;
while(ptr!=NULL)
{
Create2(ptr->rollno);
ptr=ptr->next;
}

}
void Merge()
{
node *ptr,*ptr1;
ptr=first;
ptr1=first1;

FreeList(first2);

while(ptr!=NULL && ptr1!=NULL)
{
Create2(ptr->rollno);
Create2(ptr1->rollno);
ptr=ptr->next;
ptr1=ptr1->next;
}

}

void Union()
{
int value,flag;
node *ptr,*ptr1;

FreeList(first2);

ptr=first;
ptr1=first1;

while(ptr!=NULL)
{
Create2(ptr->rollno);
ptr=ptr->next;
}
ptr=first;
while(ptr1!=NULL)
{
ptr=first;
while(ptr!=NULL)
{
if(ptr->rollno!=ptr1->rollno)
{
flag=1;
value=ptr1->rollno;
}
else
{
flag=0;
break;
}
ptr=ptr->next;
}
if(flag==1)
Create2(value);
ptr1=ptr1->next;
}
}

void Intersection()
{
node *ptr,*ptr1;
ptr=first;
ptr1=first1;
FreeList(first2);

while(ptr!=NULL)
{
ptr1=first1;
while(ptr1!=NULL)
{
if(ptr->rollno==ptr1->rollno)
Create2(ptr->rollno);
ptr1=ptr1->next;
}
ptr=ptr->next;
}
}

//It will take first node of any link list

void FreeList(node *first)
{
node *ptr,*temp;
ptr=first;
while(ptr!=NULL)
{
temp=ptr;
ptr=ptr->next;
free(temp);
}
first=NULL;
}

No comments:

Post a Comment