-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list2.c
191 lines (167 loc) · 4.38 KB
/
linked_list2.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node
{
int data;
struct Node *next;
};
// Function prototypes
void insertAtBeginning(struct Node **head, int data);
void insertAtEnd(struct Node **head, int data);
void insertBefore(struct Node **head, int target, int data);
void insertAfter(struct Node *head, int target, int data);
void printList(struct Node *head);
int main()
{
struct Node *head = NULL;
int choice, data, target;
while (1)
{
printf("\nChoose an option:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert Before a Node\n");
printf("4. Insert After a Node\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data to insert at the end: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter the target node value before which to insert: ");
scanf("%d", &target);
printf("Enter data to insert before %d: ", target);
scanf("%d", &data);
insertBefore(&head, target, data);
break;
case 4:
printf("Enter the target node value after which to insert: ");
scanf("%d", &target);
printf("Enter data to insert after %d: ", target);
scanf("%d", &data);
insertAfter(head, target, data);
break;
case 5:
printf("Linked List: ");
printList(head);
break;
case 6:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
// Function to insert a node at the beginning
void insertAtBeginning(struct Node **head, int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
// Function to insert a node at the end
void insertAtEnd(struct Node **head, int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp = *head;
newNode->data = data;
newNode->next = NULL;
if (*head == NULL)
{
*head = newNode;
return;
}
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
// Function to insert a node before a given node
void insertBefore(struct Node **head, int target, int data)
{
if (*head == NULL)
{
printf("The list is empty.\n");
return;
}
// If the target node is the head node
if ((*head)->data == target)
{
insertAtBeginning(head, data);
return;
}
struct Node *temp = *head;
while (temp->next != NULL && temp->next->data != target)
{
temp = temp->next;
}
// If target is found, insert before it
if (temp->next != NULL)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = temp->next;
temp->next = newNode;
}
else
{
printf("Target node not found.\n");
}
}
// Function to insert a node after a given node
void insertAfter(struct Node *head, int target, int data)
{
if (head == NULL)
{
printf("The list is empty.\n");
return;
}
struct Node *temp = head;
while (temp != NULL && temp->data != target)
{
temp = temp->next;
}
// If target is found, insert after it
if (temp != NULL)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = temp->next;
temp->next = newNode;
}
else
{
printf("Target node not found.\n");
}
}
// Function to print the list
void printList(struct Node *head)
{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct Node *temp = head;
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}