-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_list
167 lines (150 loc) · 3.47 KB
/
Linked_list
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
#include <stdio.h>
#include <stdlib.h>
// Define the Node structure
typedef struct Node
{
int data;
struct Node *next;
} Node;
// Function to insert a node at the beginning of the linked list
Node *insert_at_begin(struct Node *head, int data)
{
Node *ptr = (Node *)malloc(sizeof(Node));
ptr->data = data;
ptr->next = head;
head = ptr;
return head;
}
// Function to delete the first node of the linked list
Node *delete_first(struct Node *head)
{
if (head == NULL)
printf("List is already empty\n");
else
{
Node *temp = head;
head = head->next;
free(temp);
temp = NULL;
}
return head;
}
// Function to traverse and count nodes in the linked list
void traverse_count_nodes(struct Node *head)
{
int count = 0;
if (head == NULL)
printf("Linked list is empty\n");
Node *ptr = head;
while (ptr != NULL)
{
count++;
ptr = ptr->next;
}
printf("Number of nodes: %d\n", count);
}
// Function to print data of the linked list
void print_data_of_list(struct Node *head)
{
if (head == NULL)
printf("Linked list is empty\n");
Node *ptr = head;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
// Function to insert a node at the end of the linked list
// Function to insert a node at the end of the linked list
void insert_at_end(struct Node *head, int data)
{
if (head == NULL)
{
printf("Error: List is empty\n");
return;
}
Node *ptr = head;
Node *temp = (Node *)malloc(sizeof(Node));
temp->data = data;
temp->next = NULL;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
// Function to delete the last node using 2 pointer method
Node* del_last(struct Node *head)
{
if (head == NULL)
{
printf("List is already empty!\n");
}
else if (head->next == NULL)
{
free(head);
head = NULL;
}
else
{
Node *temp = head;
Node *temp2 = head;
while (temp->next != NULL)
{
temp2 = temp;
temp = temp->next;
}
temp2->next = NULL;
free(temp);
temp = NULL;
}
return head;
}
int main()
{
Node *head = NULL;
int choice = 0, data;
do
{
printf("Menu Program\n");
printf("1. Insertion at head\n");
printf("2. Insertion at tail\n");
printf("3. Deletion at head\n");
printf("4. Traversal through Linked list\n");
printf("5. Print data of the list\n");
printf("6. Exit\n");
printf("Enter the choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
head = insert_at_begin(head, data);
break;
case 2:
printf("Enter data to insert at the end: ");
scanf("%d", &data);
insert_at_end(head, data);
break;
case 3:
head = delete_first(head);
break;
case 4:
traverse_count_nodes(head);
break;
case 5:
print_data_of_list(head);
break;
case 6:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice\n");
break;
}
} while (choice != 6);
return 0;
}