-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlellmid.c
160 lines (135 loc) · 3.13 KB
/
singlellmid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a linked list node
typedef struct Node
{
int data;
struct Node *next;
} Node;
// Function to insert a new node at the end of the linked list
void insertNode(Node **head)
{
int data;
printf("Enter data for new node: ");
scanf("%d", &data);
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL)
{
*head = newNode;
}
else
{
Node *lastNode = *head;
while (lastNode->next != NULL)
{
lastNode = lastNode->next;
}
lastNode->next = newNode;
}
}
// Function to insert a new node at the middle of the linked list
void insertAtMid(Node **head, int data)
{
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL)
{
*head = newNode;
}
else
{
Node *slowPtr = *head;
Node *fastPtr = *head;
while (fastPtr->next != NULL && fastPtr->next->next != NULL)
{
fastPtr = fastPtr->next->next;
slowPtr = slowPtr->next;
}
newNode->next = slowPtr->next;
slowPtr->next = newNode;
}
}
// Function to delete a node from the linked list
void deleteNode(Node **head, int data)
{
if (*head == NULL)
{
printf("List is empty\n");
return;
}
if ((*head)->data == data)
{
Node *temp = *head;
*head = (*head)->next;
free(temp);
return;
}
Node *prevNode = *head;
Node *currentNode = (*head)->next;
while (currentNode != NULL)
{
if (currentNode->data == data)
{
prevNode->next = currentNode->next;
free(currentNode);
return;
}
prevNode = currentNode;
currentNode = currentNode->next;
}
printf("Node not found\n");
}
// Function to find the middle value of the linked list
int findMiddle(Node *head)
{
Node *slowPtr = head;
Node *fastPtr = head;
if (head != NULL)
{
while (fastPtr != NULL && fastPtr->next != NULL)
{
fastPtr = fastPtr->next->next;
slowPtr = slowPtr->next;
}
}
return slowPtr->data;
}
// Function to print the linked list
void printList(Node *head)
{
while (head != NULL)
{
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main()
{
Node *head = NULL;
char choice;
do
{
insertNode(&head);
printf("Do you want to insert another node? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y');
printf("Linked List: ");
printList(head);
printf("Middle Value: %d\n", findMiddle(head));
int data;
printf("Enter data to insert at mid: ");
scanf("%d", &data);
insertAtMid(&head, data);
printf("Linked List after inserting at mid: ");
printList(head);
printf("Enter data to delete: ");
scanf("%d", &data);
deleteNode(&head, data);
printf("Linked List after deleting: ");
printList(head);
return 0;
}