-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList14.c
153 lines (141 loc) · 3.75 KB
/
LinkedList14.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
// Retrieve the Nth node from the list or from the end
#include <stdio.h>
#include <stdlib.h>
// Definition for singly-linked list.
struct ListNode
{
int val;
struct ListNode *next;
};
// Function to create a linked list
struct ListNode *createList()
{
struct ListNode *head = NULL;
struct ListNode *tail = NULL;
int val;
while (1)
{
printf("Enter value (-1 to stop): ");
scanf("%d", &val);
if (val == -1)
break;
struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
if (head == NULL)
head = newNode;
else
tail->next = newNode;
tail = newNode;
}
return head;
}
// Function to retrive N th node from front
struct ListNode *getNthNodeFromStart(struct ListNode *head, int n)
{
if (head == NULL || n <= 0)
return NULL;
for (int i = 1; i < n; i++)
{
if (head == NULL)
return NULL;
head = head->next;
}
return head;
}
// Function to retrive N th node, from end
struct ListNode *getNthNodeFromEnd(struct ListNode *head, int n)
{
if (head == NULL || n <= 0)
return NULL;
struct ListNode *fast = head;
struct ListNode *slow = head;
for (int i = 0; i < n; i++)
{
if (fast == NULL)
return NULL;
fast = fast->next;
}
while (fast != NULL)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
// To Display List
void displayList(struct ListNode *head)
{
while (head != NULL)
{
printf("%d -> ", head->val);
head = head->next;
}
printf("NULL\n");
}
// To Delete the Linked list Using dynamic memory allocation
void freeList(struct ListNode *head)
{
while (head != NULL)
{
struct ListNode *temp = head;
head = head->next;
free(temp);
}
}
int main()
{
struct ListNode *head = NULL;
int choice, n;
struct ListNode *nthNode;
do
{
printf("\nMenu:\n");
printf("1. Create linked list\n");
printf("2. Retrieve Nth node from start\n");
printf("3. Retrieve Nth node from end\n");
printf("4. Display linked list\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
if (head != NULL)
freeList(head);
head = createList();
printf("Linked list created successfully.\n");
break;
case 2:
printf("Enter the position of the node from start: ");
scanf("%d", &n);
nthNode = getNthNodeFromStart(head, n);
if (nthNode != NULL)
printf("Node value at position %d from start: %d\n", n, nthNode->val);
else
printf("Invalid position.\n");
break;
case 3:
printf("Enter the position of the node from end: ");
scanf("%d", &n);
nthNode = getNthNodeFromEnd(head, n);
if (nthNode != NULL)
printf("Node value at position %d from end: %d\n", n, nthNode->val);
else
printf("Invalid position.\n");
break;
case 4:
printf("Linked list: ");
displayList(head);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 5);
if (head != NULL)
freeList(head);
return 0;
}