0% found this document useful (0 votes)
7 views8 pages

Extra Coding of Linked List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Extra Coding of Linked List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/*Reversing a Linked List */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node *next;
};
struct Node *start=NULL;
struct Node *insert(struct Node*,int);
void traverse(struct Node*);
struct Node *reverse(struct Node*);
void main()
{
start=insert(start,10);
start=insert(start,40);
start=insert(start,60);
start=insert(start,80);
start=insert(start,90);
printf("\n Original List\n");
traverse(start);
start=reverse(start);
printf("\nList after reversing\n");
traverse(start);
}
void traverse(struct Node *ptr)
{
if(ptr==NULL)
printf("\nList is Empty");
else
{
while(ptr!=NULL)
{
printf("|%d|->",ptr->info);
ptr=ptr->next;
}
}
}
struct Node *insert(struct Node *start,int
item)
{
struct Node *New;
New=(struct Node*)malloc(sizeof(struct
Node));
New->info=item;
New->next=NULL;
if(start==NULL)
start=New;
else
{
New->next=start;
start=New;
}
}
struct Node *reverse(struct Node *start)
{
struct Node *curr,*nex,*prev=NULL;
curr=start;
while(curr!=NULL)
{
nex=curr->next;
curr->next=prev;
prev=curr;
curr=nex;
}
return prev;
}
/*Copying a list into another list*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node *next;
};
struct Node *start1=NULL;
struct Node *start2=NULL;
struct Node *insert(struct Node*,int);
void traverse(struct Node*);
struct Node *copy(struct Node*,struct Node*);
void main()
{
start1=insert(start1,10);
start1=insert(start1,70);
start1=insert(start1,90);
start1=insert(start1,100);
start1=insert(start1,90);
printf("\n List 1 is :\n");
traverse(start1);
start2=copy(start2,start1);
printf("\nList2 copied from list 1 is
:\n");
traverse(start2);
}
void traverse(struct Node *ptr)
{
if(ptr==NULL)
printf("\nList is Empty");
else
{
while(ptr!=NULL)
{
printf("|%d|->",ptr->info);
ptr=ptr->next;
}
}
}
struct Node *insert(struct Node *start,int
item)
{
struct Node *New;
New=(struct Node*)malloc(sizeof(struct
Node));
New->info=item;
New->next=NULL;
if(start==NULL)
start=New;
else
{
New->next=start;
start=New;
}
}
struct Node *copy(struct Node *start2,struct
Node *start1)
{
if(start1==NULL)
printf("\nList is Empty, Can't Copy");
else
{
struct Node *ptr;
ptr=start1;
while(ptr!=NULL)
{
struct Node *New;
New=(struct
Node*)malloc(sizeof(struct Node));
New->info=ptr->info;
if(start2==NULL)
{
start2=New;
start2->next=NULL;
}
else
{
struct Node *temp;
temp=start2;
while(temp->next!=NULL)
temp=temp->next;
temp->next=New;
New->next=NULL;
}
ptr=ptr->next;
}
}
return start2;
}
/*Concatenation of two linked list*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node *next;
};
struct Node *start1=NULL;
struct Node *start2=NULL;
struct Node *insert(struct Node*,int);
void traverse(struct Node*);
struct Node *concat(struct Node*,struct
Node*);
void main()
{
start1=insert(start1,10);
start1=insert(start1,40);
start1=insert(start1,60);
start1=insert(start1,80);
start1=insert(start1,90);
start2=insert(start2,30);
start2=insert(start2,50);
start2=insert(start2,70);
printf("\nList 1 is : ");
traverse(start1);
printf("\nList 2 is : ");
traverse(start2);
start1=concat(start1,start2);
printf("\nFinal List is ");
traverse(start1);
}
void traverse(struct Node *ptr)
{
if(ptr==NULL)
printf("\nList is Empty");
else
{
while(ptr!=NULL)
{
printf("|%d|->",ptr->info);
ptr=ptr->next;
}
}
}
struct Node *insert(struct Node *start,int
item)
{
struct Node *New;
New=(struct Node*)malloc(sizeof(struct
Node));
New->info=item;
New->next=NULL;
if(start==NULL)
start=New;
else
{
New->next=start;
start=New;
}
}
struct Node *concat(struct Node
*start1,struct Node *start2)
{
if((start1==NULL)&&(start2==NULL))
printf("\nLists are Empty");
else if(start2==NULL)
return start1;
else if(start1==NULL)
return start2;
else
{
struct Node *temp;
temp=start1;
while(temp->next!=NULL)
temp=temp->next;
temp->next=start2;
}
return start1;
}

You might also like