Sunday, February 27, 2011

Different operations on SINGLY LINK LIST

SINGLY LINK LIST

//download from http://gtu-mca.blogspot.com
#include
#include
#include
#include

struct list1
{
int info1;
struct list1 *nxt1;
}* head1=NULL,*node1;

struct list2
{
int info2;
struct list2 *nxt2;
}* head2=NULL,*node2;

void push();
void pop();
void display();
void concate();
void marge();
void insert();
void sort();
void list_union();
void list_intersection();
void list_sum();
void list_count();
void list_rev();

void main()
{
int i=0;
clrscr();
do
{ clrscr();
printf("\n=================MENU===============\n\n");
printf(" 1.) INSERT ELEMENT..................\n");
printf(" 2.) DELETE ELEMENT..................\n");
printf(" 3.) DISPLAY ELEMENT.................\n");
printf(" 4.) INSERT AT PARTICULAR POSITION...\n");
printf(" 5.) CONCATINATE TWO LISTS...........\n");
printf(" 6.) MARGE TWO LISTS.................\n");
printf(" 7.) SORT TWO LISTS..................\n");
printf(" 8.) UNION OF TWO LISTS..............\n");
printf(" 9.) INTERSECTION OF TWO LISTS.......\n");
printf("10.) SUM OF TWO LIST.................\n");
printf("11.) COUNT THE ELEMENTS OF THE LIST..\n");
printf("12.) REVERSR BOTH OF THE LISTS.......\n");
printf("13.) EXIT............................\n");
printf("\nENTER YOUR CHOICE..................");
scanf("%d",&i);
switch(i)
{
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
case 4: insert(); break;
case 5: concate(); break;
case 6: marge(); break;
case 7: sort(); break;
case 8: list_union(); break;
case 9: list_intersection(); break;
case 10:list_sum(); break;
case 11:list_count(); break;
case 12:list_rev(); break;
case 13:exit(1);
default :
printf("\n\nENTER NUMBER BETWEEN 1 TO 10 ONLY..");
getch();
}
}while(1);
}
void push()
{
int ch;
printf("\n\nIN WHICH LIST YOU WANT TO INSERT....?");
printf("\n\nLIST - 1 OR LIST - 2..............? ");
scanf("%d",&ch);
if(ch==1)
{
struct list1 *templ1;
templ1 = (struct list1 *) (malloc(sizeof(struct list1)));
printf("\n\nENTER ELEMENT FOR LIST - 1 : ");
scanf("%d",&templ1->info1);
templ1->nxt1 = NULL;
if(head1==NULL)
{ head1 = templ1;
node1 = head1; }
else
{ node1->nxt1 = templ1;
node1 = templ1; }
// free(templ1);
}
else if(ch==2)
{
struct list2 *templ2;
templ2 = (struct list2 *)(malloc(sizeof(struct list2)));
printf("\n\nENTER ELEMENT FOR LIST - 2 : ");
scanf("%d",&templ2->info2);
templ2->nxt2 = NULL;
if(head2==NULL)
{ head2 = templ2;
node2 = head2; }
else
{ node2->nxt2 = templ2;
node2 = templ2; }
// free(templ2);
}
else
{ clrscr();
printf("\n\nENTER ONLY 1 OR 2.....");
getch(); }
}
void pop()
{
struct list1 *deltemp1,*delfind1,*delprev1;
struct list2 *deltemp2,*delfind2,*delprev2;
int ch,pos;
printf("\n\nENTER FROM WHICH LIST YOU WANT TO DELETE....");
printf("\n\nLIST - 1 OR LIST - 2...: ");
scanf("%d",&ch);
if(ch<1||ch>2)
{ clrscr();
printf("\n\nENTER ONLY 1 OR 2.....");
getch(); return; }
display();
printf("\n\nENTER IN WHICH ELEMENT YOU WANT TO DELETE....");
scanf("%d",&pos);
if(ch==1)
{
delfind1 = head1;
if(delfind1==NULL)
{ printf("\n\nLIST - 1 IS EMPTY....");
getch(); }
else if(delfind1->info1==pos)
head1 = delfind1->nxt1;
else
{ delprev1 = head1;
delfind1 = delfind1->nxt1;
while(delfind1->nxt1!=NULL)
{ if(delfind1->info1==pos)
{ delprev1->nxt1 = delfind1->nxt1;
break; }
else
{ delfind1 = delfind1->nxt1;
delprev1 = delprev1->nxt1; }
}
if(delfind1->info1==pos && delfind1->nxt1==NULL)
{ node1 = delprev1;
delprev1->nxt1 = NULL; }
}
}
else
{
delfind2 = delprev2 = head2;
if(delfind2==NULL)
{ printf("\n\nLIST - 2 IS EMPTY....");
getch(); }
else if(delfind2->info2==pos)
head2 = delfind2->nxt2;
else
{ delprev2 = head2;
delfind2 = delfind2->nxt2;
while(delfind2->nxt2!=NULL)
{ if(delfind2->info2==pos)
{ delprev2->nxt2 = delfind2->nxt2;
break; }
else
{ delfind2 = delfind2->nxt2;
delprev2 = delprev2->nxt2; }
}
if(delfind2->info2==pos && delfind2->nxt2==NULL)
{ node2 = delprev2;
delprev2->nxt2 = NULL; }
}
}
}
void display()
{
struct list1 *temp1;
struct list2 *temp2;
printf("\n\nELEMENTS OF LIST-1==");
temp1 = head1;
if(temp1==NULL)
printf(" LIST-1 IS EMPTY....");
else
while(temp1!=NULL)
{ printf("%4d,",temp1->info1);
temp1 = temp1->nxt1; }
printf("\n\nELEMENTS OF LIST-2==");
temp2 = head2;
if(temp2==NULL)
printf(" LIST-2 IS EMPTY....");
else
while(temp2!=NULL)
{ printf("%4d,",temp2->info2);
temp2 = temp2->nxt2; }
getch();
}
void insert()
{
int ch,pos;
printf("\n\nENTER IN WHICH LIST YOU WANT TO INSERT....");
printf("\n\nLIST - 1 OR LIST - 2...: ");
scanf("%d",&ch);
if(ch<1||ch>2)
{ clrscr();
printf("\n\nENTER ONLY 1 OR 2.....");
getch(); return; }
display();
if(ch==1)
{
struct list1 *templ1,*findpos1;
templ1 = (struct list1 *) (malloc(sizeof(struct list1)));
findpos1 = head1;
if(findpos1==NULL)
{ printf("\n\nLIST - 1 IS EMPTY.....");
getch(); }
else
{ printf("\n\nAFTER WHICH ELEMENT YOU WANT TO INSERT..: ");
scanf("%d",&pos);
while(findpos1->info1!=pos&&findpos1!=NULL)
findpos1 = findpos1->nxt1;
if(findpos1->info1==pos)
{ printf("\n\nENTER ELEMENT FOR LIST - 1 : ");
scanf("%d",&templ1->info1);
templ1->nxt1 = findpos1->nxt1;
findpos1->nxt1 = templ1;
}
else
{ printf("\n\nELEMENT NOT FOUND...");
getch(); }
}
}
else
{
struct list2 *templ2,*findpos2;
templ2 = (struct list2 *)(malloc(sizeof(struct list2)));
findpos2 = head2;
if(findpos2==NULL)
{ printf("\n\nLIST - 2 IS EMPTY.....");
getch(); }
else
{ printf("\n\nAFTER WHICH ELEMENT YOU WANT TO INSERT..: ");
scanf("%d",&pos);
while(findpos2->info2!=pos)
findpos2 = findpos2->nxt2;
if(findpos2->info2==pos&&findpos2->info2!=NULL)
{ printf("\n\nENTER ELEMENT FOR LIST - 2 : ");
scanf("%d",&templ2->info2);
templ2->nxt2 = findpos2->nxt2;
findpos2->nxt2 = templ2;
}
else
{ printf("\n\nELEMENT NOT FOUND...");
getch(); }
}
}
}
void concate()
{
struct list1 *temphead1;
struct list2 *temphead2;
temphead1 = head1;
temphead2 = head2;
if(temphead1==NULL&&temphead2==NULL)
{ printf("\n\nBOTH LISTS ARE EMPTY....");
return; }
display();
printf("\n\n=====THE CONCATINATION OF LIST-1 and LIST-2 IS =====\n\n");
if(temphead1==NULL)
while(temphead2!=NULL)
{ printf("%4d,",temphead2->info2);
temphead2 = temphead2->nxt2; }
else if(temphead2==NULL)
while(temphead1!=NULL)
{ printf("%4d,",temphead1->info1);
temphead1 = temphead1->nxt1; }
else
{ while(temphead1!=NULL)
{ printf("%4d,",temphead1->info1);
temphead1 = temphead1->nxt1; }
while(temphead2!=NULL)
{ printf("%4d,",temphead2->info2);
temphead2 = temphead2->nxt2; }
}getch();
}
void marge()
{ struct list1 *temphead1;
struct list2 *temphead2;int comp;
temphead1 = head1;
temphead2 = head2;
sort();
if(temphead1==NULL && temphead2==NULL)
{ printf("\n\nBoth the Lists are EMPTY...");
return; }
printf("\n\n====MARGE of two lists List-1 and List-2====\n\n");
if(temphead1==NULL)
{ while(temphead1!=NULL)
{ printf("%4d ,",temphead1->info1);
temphead1 = temphead1->nxt1; }
}
else if(temphead2==NULL)
{ while(temphead2!=NULL)
{ printf("%4d ,",temphead2->info2);
temphead2 = temphead2->nxt2; }
}
else
{ while(temphead1!=NULL)
{ comp = temphead1->info1;
if(temphead2!=NULL)
{
while(temphead2!=NULL)
{ if(temphead2->info2 <= comp)
{ printf("%4d ,",temphead2->info2);
temphead2 = temphead2->nxt2; }
else if(comp <>info2)
{ printf("%4d ,",comp);
temphead1 = temphead1->nxt1;
break; }
}
}
else
{ printf("%4d ,",comp);
temphead1 = temphead1->nxt1; }
}
if(temphead2!=NULL)
{ while(temphead2!=NULL)
{ printf("%4d ,",temphead2->info2);
temphead2 = temphead2->nxt2;
}
}
if(temphead1!=NULL)
{ while(temphead1!=NULL)
{ printf("%4d ,",temphead1->info1);
temphead1 = temphead1->nxt1;
}
}
}getch();
}
void sort()
{
int count=0,tempswap;
struct list1 *i1,*j1,*sorttemp1;
sorttemp1 = head1;
if(sorttemp1==NULL)
printf("\n\nLIST-1 IS EMPTY.....");
else if(sorttemp1->nxt1==NULL)
printf("\n\nONLY ONE ELEMENT IS THERE IN LIST-1..");
else
{ i1 = sorttemp1; j1 = i1->nxt1;
do
{ do
{ if(i1->info1 > j1->info1)
{ tempswap = i1->info1;
i1->info1 = j1->info1;
j1->info1 = tempswap; }
j1 = j1->nxt1;
}while(j1->info1!=NULL);
if(count==0) head1 = i1;
count++; i1 = i1->nxt1; j1 = i1->nxt1;
}while((i1->nxt1)!=NULL);
}count=0;
//=================================================
struct list2 *i2,*j2,*sorttemp2;
sorttemp2 = head2;
if(sorttemp2==NULL)
printf("\n\nLIST-2 IS EMPTY.....");
else if(sorttemp2->nxt2==NULL)
printf("\n\nONLY ONE ELEMENT IS THERE IN LIST-2..");
else
{ i2 = sorttemp2; j2 = i2->nxt2;
do
{ do
{ if(i2->info2 > j2->info2)
{ tempswap = i2->info2;
i2->info2 = j2->info2;
j2->info2 = tempswap; }
j2 = j2->nxt2;
}while(j2->info2!=NULL);
if(count==0) head2 = i2;
count++; i2 = i2->nxt2; j2 = i2->nxt2;
}while((i2->nxt2)!=NULL);
}
printf("\n\n=========SORTED LISTS==========\n");
display();
}
void list_union()
{ int flag=0;
struct list1 *temphead1;
struct list2 *temphead2;
temphead1 = head1;
temphead2 = head2;
if(temphead1==NULL&&temphead2==NULL)
{ printf("\n\nBOTH LISTS ARE EMPTY....");
return; }
display();
printf("\n\n=====THE UNION OF LIST-1 and LIST-2 IS =====\n\n");
if(temphead1==NULL)
while(temphead2!=NULL)
{ printf("%4d,",temphead2->info2);
temphead2 = temphead2->nxt2; }
else if(temphead2==NULL)
while(temphead1!=NULL)
{ printf("%4d,",temphead1->info1);
temphead1 = temphead1->nxt1; }
else
{ while(temphead1!=NULL)
{ printf("%4d,",temphead1->info1);
temphead1 = temphead1->nxt1; }
while(temphead2!=NULL)
{ temphead1 = head1;flag=0;
while(temphead1!=NULL)
if(temphead1->info1==temphead2->info2)
{ flag=1; break; }
else temphead1=temphead1->nxt1;
if(flag==0)
printf("%4d,",temphead2->info2);
temphead2 = temphead2->nxt2;
}
}getch();
}
void list_intersection()
{
struct list1 *temphead1;
struct list2 *temphead2;
temphead1 = head1;
display();
printf("\n\n======INTERSECTION OF LIST-1 and LIST-2 IS========\n\n");
int intcount=0;
while(temphead1!=NULL)
{ temphead2 = head2;
while(temphead2!=NULL)
{ if(temphead1->info1 == temphead2->info2)
{ printf("%4d,",temphead1->info1);
intcount++; break; }
temphead2 = temphead2->nxt2;
}
temphead1 = temphead1->nxt1;
}
if(intcount==0) printf("\n\nNO ANY COMMON ELEMENTS ARE THERE IN LIST-1 and LIST-2..\n\n");
getch();
}
void list_sum()
{
struct list1 *temphead1;
struct list2 *temphead2;
int suml1=0,suml2=0;
temphead1 = head1;
temphead2 = head2;
display();
printf("\n\n========SUM OF THE TWO LIST========\n\n");
if(temphead1==NULL)
printf("List-1 is EMPTY.....\n");
else
{ while(temphead1!=NULL)
{ suml1+=temphead1->info1;
temphead1=temphead1->nxt1; }
printf("SUM OF THE ELEMENTS OF LIST-1 IS : %d",suml1);
}
if(temphead2==NULL)
printf("List-2 is EMPTY.....\n");
else
{ while(temphead2!=NULL)
{ suml2+=temphead2->info2;
temphead2=temphead2->nxt2; }
printf("\n\nSUM OF THE ELEMENTS OF LIST-2 IS : %d",suml2);
}
getch();
}
void list_count()
{
struct list1 *temphead1;
struct list2 *temphead2;
int countl1=0,countl2=0;
temphead1 = head1;
temphead2 = head2;
display();
printf("\n\n========COUNT OF THE TWO LIST========\n\n");
if(temphead1==NULL)
printf("List-1 is EMPTY.....\n");
else
{ while(temphead1!=NULL)
{ countl1++;
temphead1=temphead1->nxt1; }
printf("TOTAL NUMBER OF ELEMENTS IN LIST-1 IS : %d",countl1);
}
if(temphead2==NULL)
printf("List-2 is EMPTY.....\n");
else
{ while(temphead2!=NULL)
{ countl2++;
temphead2=temphead2->nxt2; }
printf("\n\nTOTAL NUMBER OF ELEMENTS IN LIST-2 IS : %d",countl2);
}
getch();
}
void list_rev()
{
struct list1 *temphead1=head1;
struct list1 *mid1,*tmid1;
struct list2 *temphead2=head2;
struct list2 *mid2,*tmid2;
int cnt1=0,cnt2=0;
int i,j,st1,st2;
while(temphead1!=NULL)
{ cnt1++;
temphead1=temphead1->nxt1; }
while(temphead2!=NULL)
{ cnt2++;
temphead2=temphead2->nxt2; }
temphead1 = head1; temphead2 = head2;
for(i=1;i<=(cnt1/2);i++)
temphead1 = temphead1->nxt1;
for(i=1;i<=(cnt2/2);i++)
temphead2 = temphead2->nxt2;
mid1 = temphead1;
mid2 = temphead2;
temphead1 = head1; temphead2 = head2;

if(cnt1>1&&cnt1%2==0)
{ int tcnt1 = cnt1;
for(i=1;i<=(cnt1/2);i++)
{ tmid1 = mid1;
for(j=((cnt1/2)+1);j tmid1 = tmid1->nxt1;
tcnt1--;
st1 = temphead1->info1;
temphead1->info1 = tmid1->info1;
tmid1->info1 = st1;
temphead1 = temphead1->nxt1;
}
}
if(cnt2>1&&cnt2%2==0)
{ int tcnt2 = cnt2;
for(i=1;i<=(cnt2/2);i++)
{ tmid2 = mid2;
for(j=((cnt2/2)+1);j tmid2 = tmid2->nxt2;
tcnt2--;
st2 = temphead2->info2;
temphead2->info2 = tmid2->info2;
tmid2->info2 = st2;
temphead2 = temphead2->nxt2;
}
}
getch();
}

0 comments:

Post a Comment