Batch - B Practical Test Question

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

Linked List Demo - B

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

//Strcture definition
struct Node
{
int value;
struct Node *next;
};
struct Node *first=NULL,*head=NULL,*temp=NULL;

//Function Declaration
void printMenu();
void createLL(int);
void insertNode();
void printLL();
void searchNode(int);
void updateNode(int,int);
void deleteNode(int);
void deleteLL();

//Main Function
int main()
{
int choice,noOfNode,searchValue,oldValue,newValue;
do
{
printMenu();
printf("\n Enter Your Choice for LL Operation : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter how many node you want to add in LL : ");
scanf("%d",&noOfNode);
createLL(noOfNode);
break;
case 2:
insertNode();
break;
case 3:
printf("\n Enter value to search in LL : ");
scanf("%d",&searchValue);
searchNode(searchValue);
break;
case 4:
printf("\n Enter value to update in LL : ");
scanf("%d",&oldValue);
printf("\n Enter new value to replace in LL : ");
scanf("%d",&newValue);
updateNode(oldValue,newValue);
break;
case 5:
printf("\n Enter value to delete in LL : ");
scanf("%d",&oldValue);
deleteNode(oldValue);
break;
case 6:
deleteLL();
break;
case 7:
printLL();
break;
default:
printf("\n Thanks for using LL Program");
}
}while(choice>0 && choice<8);
return 0;
}

//Function Implementation
void printMenu()
{
printf("\n############################");
printf("\n    LL Operation MENU");
printf("\n############################");
printf("\n    1 - Create LL");
printf("\n    2 - Insert Node");
printf("\n    3 - Search Node");
printf("\n    4 - Update Node");
printf("\n    5 - Delete Node");
printf("\n    6 - Delete LL");
printf("\n    7 - Print LL");
printf("\n############################");
}
void createLL(int noOfNode)
{
while(noOfNode>0)
{
insertNode();
noOfNode--;
}
}
void insertNode()
{
temp=(struct Node *)malloc(sizeof(struct Node)*1);
printf("\n Enter value for Node : ");
scanf("%d",&temp->value);
temp->next=NULL;

if(first==NULL)
{
first=temp;
}
else
{
temp->next=first;
first=temp;
}
}

void searchNode(int searchValue)
{
int pos=1;
head=first;
while(head!=NULL)
{
if(head->value==searchValue)
{
printf("%d ",pos);
}
head=head->next;
pos++;
}
}

void updateNode(int oldValue,int newvalue)
{
int pos=1;
head=first;
while(head!=NULL)
{
if(head->value==oldValue)
{
head->value=newvalue;
printf("%d ",pos);
}
head=head->next;
pos++;
}
}

void deleteNode(int oldValue)
{
int pos=1;
head=first;
if(head->value==oldValue)
{
first=head->next;
temp=head;
free(temp);
//pos++;
}
else
{
while(head!=NULL)
{
if(head->next->value==oldValue)
{
printf("IN");
temp=head->next;
head->next=temp->next;
free(temp);
break;
//printf("%d ",pos);
}
head=head->next;
//pos++;
}

}
}

void deleteLL()
{
head=first;
while(head->next!=NULL)
{
temp=head->next;
head->next=temp->next;
free(temp);
}
free(first);
first=NULL;
head=NULL;

}
void printLL()
{
head=first;
while(head!=NULL)
{
printf("%d -> ",head->value);
head=head->next;
}
}

Comments