#include<stdio.h>
#include<memory.h>
#include<stdlib.h>
//LinkedList
struct Node
{
int no;
struct Node *next;
};
struct Node *first=NULL,*head=NULL,*temp=NULL;
//Function Declaration
void printMenu();
void createLL(int);
void insertNode();
void searchNode(int);
void updateNode(int,int);
void deleteNode(int);
void deleteLL();
void printLL();
int main()
{
int choice,noOfNode,valueOfNode,searchValue,newValue;
do
{
system("cls"); // To clear the screen
printMenu();
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n How Many Node(s) you want to Add in LL : ");
scanf("%d",&noOfNode);
createLL(noOfNode);
break;
case 2:
insertNode();
break;
case 3:
if(first==NULL)
{
printf("\n LinkedList is Empty so Searching is not Possible");
}
else
{
printf("\n Enter Node value to Search it in LL : ");
scanf("%d",&valueOfNode);
searchNode(valueOfNode);
}
break;
case 4:
if(first==NULL)
{
printf("\n LinkedList is Empty so Updation is not Possible");
}
else
{
printf("\n Enter Node value to Update it in LL : ");
scanf("%d",&searchValue);
printf("\n Enter new Node value to Update it in LL : ");
scanf("%d",&newValue);
updateNode(searchValue,newValue);
}
break;
case 5:
if(first==NULL)
{
printf("\n LinkedList is Empty so Deletion is not Possible");
}
else
{
printf("\n Enter Node value to Delete from LL : ");
scanf("%d",&valueOfNode);
deleteNode(valueOfNode);
}
break;
case 6:
if(first==NULL)
{
printf("\n LinkedList is already Empty");
}
else
{
deleteLL();
}
break;
case 7:
if(first==NULL)
{
printf("\n LinkedList is Empty so Printing is not Possible");
}
else
{
printLL();
}
break;
default:
printf("Thanks for Using LL Program");
}
getch(); //To stop output on Screen
}while(choice>0 && choice<8);
return 0;
}
//Implementation of Function
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 8 - Exit");
printf("\n==============================");
}
//Function Will create LL with = noOfNode
void createLL(int noOfNode)
{
while(noOfNode>0)
{
//Create new Node
insertNode();
noOfNode--;
}
}
//This Function Will Insert Node at Last Postion in LL
void insertNode()
{
temp=(struct Node *) malloc (sizeof(struct Node) * 1);
printf("\n Enter Value for New Node : ");
scanf("%d",&temp->no);
temp->next=NULL;
//To Attach Node in LL
if(first==NULL)
{
first=temp;
}
else
{
head=first;
while(head->next!=NULL)
{
head=head->next;
}
head->next=temp;
}
}
//This Function Will Search Node based on Value and Display its positions
void searchNode(int valueOfNode)
{
int position=0,flag=0;
printf("\n Node %d : ",valueOfNode);
head=first;
while(head!=NULL)
{
if(head->no==valueOfNode)
{
printf("%d ",position+1);
flag=1;
}
head=head->next;
position++;
}
if(flag==0)
{
printf(" is not found in LL");
}
else
{
printf(" Found at mentioned postion(s)");
}
}
//This function will update Node value in LL if found
void updateNode(int searchValue,int newValue)
{
int flag=0,position=0;
printf("\n Node %d : ",searchValue);
head=first;
while(head!=NULL)
{
if(head->no==searchValue)
{
head->no=newValue;
printf("%d ",position+1);
flag=1;
}
head=head->next;
position++;
}
if(flag==0)
{
printf(" is not found in LL");
}
else
{
printf(" Updated at mentioned postion(s)");
}
}
//This function will delete all node from LL having value = valueOfNode
void deleteNode(int valueOfNode)
{
head=first;
while(head->next!=NULL)
{
if(head->no==valueOfNode)
{
temp=head;
head=head->next;
first=first->next;
free(temp);
}
else if(head->next->no==valueOfNode)
{
temp=head->next;
head->next=temp->next;
free(temp);
}
else
{
head=head->next;
}
}
//code will run if only one node is remains in LL with specific value
if(first->no==valueOfNode)
{
free(first);
first=NULL;
head=NULL;
}
}
//This code will delete all nodes from LL and make it NULL
void deleteLL()
{
while(first->next!=NULL)
{
temp=first->next;
printf("\n%d - deleted",temp->no);
first->next=temp->next;
free(temp);
}
printf("\n%d - deleted",first->no);
free(first);
first=NULL;
}
//This code will print LL in sequence.
void printLL()
{
head=first;
while(head!=NULL)
{
printf("%d -> ",head->no);
head=head->next;
}
}
#include<memory.h>
#include<stdlib.h>
//LinkedList
struct Node
{
int no;
struct Node *next;
};
struct Node *first=NULL,*head=NULL,*temp=NULL;
//Function Declaration
void printMenu();
void createLL(int);
void insertNode();
void searchNode(int);
void updateNode(int,int);
void deleteNode(int);
void deleteLL();
void printLL();
int main()
{
int choice,noOfNode,valueOfNode,searchValue,newValue;
do
{
system("cls"); // To clear the screen
printMenu();
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n How Many Node(s) you want to Add in LL : ");
scanf("%d",&noOfNode);
createLL(noOfNode);
break;
case 2:
insertNode();
break;
case 3:
if(first==NULL)
{
printf("\n LinkedList is Empty so Searching is not Possible");
}
else
{
printf("\n Enter Node value to Search it in LL : ");
scanf("%d",&valueOfNode);
searchNode(valueOfNode);
}
break;
case 4:
if(first==NULL)
{
printf("\n LinkedList is Empty so Updation is not Possible");
}
else
{
printf("\n Enter Node value to Update it in LL : ");
scanf("%d",&searchValue);
printf("\n Enter new Node value to Update it in LL : ");
scanf("%d",&newValue);
updateNode(searchValue,newValue);
}
break;
case 5:
if(first==NULL)
{
printf("\n LinkedList is Empty so Deletion is not Possible");
}
else
{
printf("\n Enter Node value to Delete from LL : ");
scanf("%d",&valueOfNode);
deleteNode(valueOfNode);
}
break;
case 6:
if(first==NULL)
{
printf("\n LinkedList is already Empty");
}
else
{
deleteLL();
}
break;
case 7:
if(first==NULL)
{
printf("\n LinkedList is Empty so Printing is not Possible");
}
else
{
printLL();
}
break;
default:
printf("Thanks for Using LL Program");
}
getch(); //To stop output on Screen
}while(choice>0 && choice<8);
return 0;
}
//Implementation of Function
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 8 - Exit");
printf("\n==============================");
}
//Function Will create LL with = noOfNode
void createLL(int noOfNode)
{
while(noOfNode>0)
{
//Create new Node
insertNode();
noOfNode--;
}
}
//This Function Will Insert Node at Last Postion in LL
void insertNode()
{
temp=(struct Node *) malloc (sizeof(struct Node) * 1);
printf("\n Enter Value for New Node : ");
scanf("%d",&temp->no);
temp->next=NULL;
//To Attach Node in LL
if(first==NULL)
{
first=temp;
}
else
{
head=first;
while(head->next!=NULL)
{
head=head->next;
}
head->next=temp;
}
}
//This Function Will Search Node based on Value and Display its positions
void searchNode(int valueOfNode)
{
int position=0,flag=0;
printf("\n Node %d : ",valueOfNode);
head=first;
while(head!=NULL)
{
if(head->no==valueOfNode)
{
printf("%d ",position+1);
flag=1;
}
head=head->next;
position++;
}
if(flag==0)
{
printf(" is not found in LL");
}
else
{
printf(" Found at mentioned postion(s)");
}
}
//This function will update Node value in LL if found
void updateNode(int searchValue,int newValue)
{
int flag=0,position=0;
printf("\n Node %d : ",searchValue);
head=first;
while(head!=NULL)
{
if(head->no==searchValue)
{
head->no=newValue;
printf("%d ",position+1);
flag=1;
}
head=head->next;
position++;
}
if(flag==0)
{
printf(" is not found in LL");
}
else
{
printf(" Updated at mentioned postion(s)");
}
}
//This function will delete all node from LL having value = valueOfNode
void deleteNode(int valueOfNode)
{
head=first;
while(head->next!=NULL)
{
if(head->no==valueOfNode)
{
temp=head;
head=head->next;
first=first->next;
free(temp);
}
else if(head->next->no==valueOfNode)
{
temp=head->next;
head->next=temp->next;
free(temp);
}
else
{
head=head->next;
}
}
//code will run if only one node is remains in LL with specific value
if(first->no==valueOfNode)
{
free(first);
first=NULL;
head=NULL;
}
}
//This code will delete all nodes from LL and make it NULL
void deleteLL()
{
while(first->next!=NULL)
{
temp=first->next;
printf("\n%d - deleted",temp->no);
first->next=temp->next;
free(temp);
}
printf("\n%d - deleted",first->no);
free(first);
first=NULL;
}
//This code will print LL in sequence.
void printLL()
{
head=first;
while(head!=NULL)
{
printf("%d -> ",head->no);
head=head->next;
}
}
Comments
Post a Comment