#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
void insert_node(struct node **Head)
{
struct node *iterate;
int data;
printf("Enter node data:");
scanf("%d", &data);
if(*Head == NULL) {
*Head = (struct node*)malloc(sizeof(struct node*));
(*Head)->data = data;
(*Head)->next = NULL;
} else {
iterate = *Head;
while(iterate->next != NULL)
iterate = iterate->next;
iterate->next = (struct node*)malloc(sizeof(struct node*));
iterate->next->data = data;
iterate->next->next = NULL;
}
}
void show_linked_list(struct node *Head)
{
struct node *iterate = Head;
if(!iterate) {
printf("Linked List is empty\n");
return;
}
printf("Linked List elements are\n");
while (iterate != NULL) {
printf("%d\n", iterate->data);
iterate = iterate->next;
}
}
void isLL_ContainLoop(struct node *Head)
{
struct node *start, *end = NULL;
start = Head;
do {
if (start != NULL) {
if(start->next)
start = start->next->next;
else
start = start->next;
}
if (end == NULL)
end = Head;
else
end = end->next;
}while(start != end && start!= NULL);
if (start == end && start != NULL)
printf("+++Linked List has LOOP+++\n");
else
printf("+++Linked List has NO LOOP+++\n");
}
void cleanup(struct node **Head)
{
struct node *iterate = *Head;
while(*Head)
{
iterate = (*Head);
*Head = (*Head)->next;
iterate->next = NULL;
free(iterate);
}
}
struct node* search_node(struct node *Head, int data)
{
while (Head) {
if (Head->data == data)
return Head;
Head = Head->next;
}
return NULL;
}
void delete_node(struct node **Head)
{
struct node *del_node = NULL;
struct node *iterate = *Head;
int data, find;
if (*Head == NULL)
printf("Linked List is empty\n");
show_linked_list(*Head);
printf("Select data to delete from linked list\n");
scanf("%d", &data);
del_node = search_node(*Head, data);
if (del_node) {
printf("Data is present\n");
if (del_node == *Head) {
*Head = (*Head)->next;
iterate->next = NULL;
free(iterate);
} else {
while (iterate->next != del_node)
iterate = iterate->next;
iterate->next = iterate->next->next;
del_node->next = NULL;
free(del_node);
}
} else {
printf("Data is not present\n");
}
}
void main()
{
struct node *Head = NULL;
int choice, data;
while(1) {
printf("****Menu*****\n");
printf("select 1: Insert new node\n");
printf("select 2: show linked list elements\n");
printf("select 3: is Linked list contain loop\n");
printf("select 4: Delete node\n");
printf("select 5: Search node\n");
printf("select 6: Exit\n");
printf("Enter your input 1-6 ?\n");
scanf("%d", &choice);
switch(choice){
case 1: insert_node(&Head); break;
case 2: show_linked_list(Head); break;
case 3: isLL_ContainLoop(Head); break;
case 4: delete_node(&Head); break;
case 5: printf("Enter Data to search in Linked List\n");
scanf("%d", &data);
if (search_node(Head, data)) printf("Data is present\n");
else printf("Data is not present\n");
break;
case 6:
default: cleanup(&Head); exit(1);
}
printf("=========================\n");
}
}
Output:
-----------
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
1
Enter node data:11
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
1
Enter node data:22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
2
Linked List elements are
11
22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
3
+++Linked List has NO LOOP+++
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
5
Enter Data to search in Linked List
22
Data is present
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
4
Linked List elements are
11
22
Select data to delete from linked list
11
Data is present
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
2
Linked List elements are
22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
void insert_node(struct node **Head)
{
struct node *iterate;
int data;
printf("Enter node data:");
scanf("%d", &data);
if(*Head == NULL) {
*Head = (struct node*)malloc(sizeof(struct node*));
(*Head)->data = data;
(*Head)->next = NULL;
} else {
iterate = *Head;
while(iterate->next != NULL)
iterate = iterate->next;
iterate->next = (struct node*)malloc(sizeof(struct node*));
iterate->next->data = data;
iterate->next->next = NULL;
}
}
void show_linked_list(struct node *Head)
{
struct node *iterate = Head;
if(!iterate) {
printf("Linked List is empty\n");
return;
}
printf("Linked List elements are\n");
while (iterate != NULL) {
printf("%d\n", iterate->data);
iterate = iterate->next;
}
}
void isLL_ContainLoop(struct node *Head)
{
struct node *start, *end = NULL;
start = Head;
do {
if (start != NULL) {
if(start->next)
start = start->next->next;
else
start = start->next;
}
if (end == NULL)
end = Head;
else
end = end->next;
}while(start != end && start!= NULL);
if (start == end && start != NULL)
printf("+++Linked List has LOOP+++\n");
else
printf("+++Linked List has NO LOOP+++\n");
}
void cleanup(struct node **Head)
{
struct node *iterate = *Head;
while(*Head)
{
iterate = (*Head);
*Head = (*Head)->next;
iterate->next = NULL;
free(iterate);
}
}
struct node* search_node(struct node *Head, int data)
{
while (Head) {
if (Head->data == data)
return Head;
Head = Head->next;
}
return NULL;
}
void delete_node(struct node **Head)
{
struct node *del_node = NULL;
struct node *iterate = *Head;
int data, find;
if (*Head == NULL)
printf("Linked List is empty\n");
show_linked_list(*Head);
printf("Select data to delete from linked list\n");
scanf("%d", &data);
del_node = search_node(*Head, data);
if (del_node) {
printf("Data is present\n");
if (del_node == *Head) {
*Head = (*Head)->next;
iterate->next = NULL;
free(iterate);
} else {
while (iterate->next != del_node)
iterate = iterate->next;
iterate->next = iterate->next->next;
del_node->next = NULL;
free(del_node);
}
} else {
printf("Data is not present\n");
}
}
void main()
{
struct node *Head = NULL;
int choice, data;
while(1) {
printf("****Menu*****\n");
printf("select 1: Insert new node\n");
printf("select 2: show linked list elements\n");
printf("select 3: is Linked list contain loop\n");
printf("select 4: Delete node\n");
printf("select 5: Search node\n");
printf("select 6: Exit\n");
printf("Enter your input 1-6 ?\n");
scanf("%d", &choice);
switch(choice){
case 1: insert_node(&Head); break;
case 2: show_linked_list(Head); break;
case 3: isLL_ContainLoop(Head); break;
case 4: delete_node(&Head); break;
case 5: printf("Enter Data to search in Linked List\n");
scanf("%d", &data);
if (search_node(Head, data)) printf("Data is present\n");
else printf("Data is not present\n");
break;
case 6:
default: cleanup(&Head); exit(1);
}
printf("=========================\n");
}
}
Output:
-----------
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
1
Enter node data:11
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
1
Enter node data:22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
2
Linked List elements are
11
22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
3
+++Linked List has NO LOOP+++
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
5
Enter Data to search in Linked List
22
Data is present
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
4
Linked List elements are
11
22
Select data to delete from linked list
11
Data is present
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
2
Linked List elements are
22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
6
No comments:
Post a Comment