Batch - B Practical Test Question

Batch - B Practical Test Question
Batch - B Practical Test Question

Advance Operation on Linked List

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

struct Node
{
int data;
struct Node *next;
};
struct Node *first=NULL,*second=NULL,*head=NULL,*fhead=NULL,*shead=NULL,*temp=NULL;

//Function Declaration
struct Node * createLL(int);
void printLL(struct Node *);
struct Node * copyLL(struct Node *);
void concateLL(struct Node *,struct Node * );
void reverseLL(struct Node *);
struct Node * splitLL(struct Node *,int);
int countNodeInLL(struct Node *);

//main Function
int main()
{
int choice,noOfNode;
do
{
system("cls"); // To clear the screen
printMenu();
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n How many nodes you want in First LL : ");
scanf("%d",&noOfNode);
first=createLL(noOfNode);

printf("\n How many nodes you want in Second LL : ");
scanf("%d",&noOfNode);
second=createLL(noOfNode);
break;

case 2:
fhead=first;
second=copyLL(fhead);
printf("\Nodes in Second LL : \n");
shead=second;
printLL(shead);
break;

case 3:
fhead=first;
shead=second;
concateLL(fhead,shead);
break;

case 4:
head=NULL;
fhead=first;
reverseLL(fhead);

fhead=first;
printLL(fhead);
break;

case 5:
printf("\n Enter Postion from which you want to split LL : ");
scanf("%d",&noOfNode);
fhead=first;
second = splitLL(fhead,noOfNode);
break;

case 6:
fhead=first;
noOfNode=countNodeInLL(fhead);
printf("\n No of Node in First LL : %d",noOfNode);

shead=second;
noOfNode=countNodeInLL(shead);
printf("\n No of Node in Second LL : %d",noOfNode);
break;

case 7:
printf("\n Nodes in First LL : \n");
fhead=first;
printLL(fhead);
printf("\n Nodes in Second LL : \n");
shead=second;
printLL(shead);
break;

default:
printf("Thanks for Using LL Program");
}
getch();
}while(choice>0 && choice<8);

return 0;
}

void printMenu()
{
printf("\n==============================");
printf("\n     LL OPERATION MENU");
printf("\n==============================");
printf("\n     1 - Create LL");
printf("\n     2 - Copy");
printf("\n     3 - Concate");
printf("\n     4 - Reverse");
printf("\n     5 - Split");
printf("\n     6 - Count");
printf("\n     7 - Print LL");
printf("\n     8 - Exit");
printf("\n==============================");
}

//This function will create LL and return its Pointer
struct Node * createLL(int noOfNode)
{
head=NULL;
while(noOfNode>0)
{
temp=(struct Node *) malloc (sizeof(struct Node) * 1);
printf("\n Insert Value for Node : ");
scanf("%d",&temp->data);
temp->next=NULL;

if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
noOfNode--;
}
return head;
}

//This function will perform copy operation on LL
struct Node * copyLL(struct Node *fhead)
{
second=NULL;
while(fhead!=NULL)
{
temp=(struct Node *) malloc (sizeof(struct Node) * 1);
temp->data=fhead->data;
temp->next=NULL;

if(second==NULL)
{
shead=temp;
second=shead;
}
else
{
shead=second;
while(shead->next!=NULL)
{
shead=shead->next;
}
shead->next=temp;
}
fhead=fhead->next;
}
return second;
}

//This function will perform concatenation of two LL
void concateLL(struct Node *fhead,struct Node *shead)
{
while(fhead->next!=NULL)
{
fhead=fhead->next;
}
while(shead!=NULL)
{

temp=(struct Node *) malloc (sizeof(struct Node) * 1);
temp->data=shead->data;
temp->next=NULL;

fhead->next=temp;
fhead=fhead->next;

shead=shead->next;
}
}

//This function will reverse the LL
void reverseLL(struct Node *fhead)
{
if(fhead==NULL)
{
return ;
}
reverseLL(fhead->next);
//to get LL in reverse
if(head==NULL)
{
head=fhead;
first=head;
head->next=NULL;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=fhead;
fhead->next=NULL;
}
}

//This function will perform split Opeartion on LinkedList and return pointer
struct Node * splitLL(struct Node *fhead,int pos)
{
shead=NULL;
while(pos>1)
{
fhead=fhead->next;
pos--;
}
if(fhead==NULL)
{
return NULL;
}
else
{
shead=fhead->next;
fhead->next=NULL;
return shead;
}
}

//This function will count the No of Nodes in  LL
int countNodeInLL(struct Node *head)
{
int pos=0;
while(head!=NULL)
{
head=head->next;
pos++;
}
return pos;
}

//This function will print Nodes of LL
void printLL(struct Node *head)
{
if(head==NULL)
{
return;
}
printf("%d -> ",head->data);
printLL(head->next);
}

Comments