-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathLinkedList.c
122 lines (108 loc) · 3.21 KB
/
LinkedList.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
#include <stdio.h>
#include <stdlib.h>
// Structure for a linked list node
struct Node {
int data;
struct Node* next;
};
// Function to create a new linked list node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// Function to insert a value at the beginning of the linked list
struct Node* insertAtBeginning(struct Node* head, int value) {
struct Node* newNode = createNode(value);
newNode->next = head;
return newNode;
}
// Function to search for a value in the linked list
int search(struct Node* head, int value) {
struct Node* current = head;
while (current != NULL) {
if (current->data == value) {
return 1; // Value found
}
current = current->next;
}
return 0; // Value not found
}
// Function to delete a value from the linked list
struct Node* deleteNode(struct Node* head, int value) {
struct Node* current = head;
struct Node* prev = NULL;
// If the value is at the head of the list
if (current != NULL && current->data == value) {
head = current->next;
free(current);
return head;
}
// Search for the value to be deleted
while (current != NULL && current->data != value) {
prev = current;
current = current->next;
}
// If the value was not found
if (current == NULL) {
return head;
}
// Unlink the node from the list
prev->next = current->next;
free(current);
return head;
}
// Function to display the linked list
void display(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
int choice, value;
while (1) {
printf("\nLinked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at the beginning: ");
scanf("%d", &value);
head = insertAtBeginning(head, value);
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &value);
if (search(head, value)) {
printf("%d is present in the linked list.\n", value);
} else {
printf("%d is not found in the linked list.\n", value);
}
break;
case 3:
printf("Enter value to delete: ");
scanf("%d", &value);
head = deleteNode(head, value);
break;
case 4:
printf("Linked List: ");
display(head);
break;
case 5:
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}