-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelcir.c
More file actions
68 lines (53 loc) · 1.32 KB
/
Copy pathdelcir.c
File metadata and controls
68 lines (53 loc) · 1.32 KB
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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* deleteFirstNode(struct Node* last) {
if (last == NULL) {
// If the list is empty
printf("List is empty\n");
return NULL;
}
struct Node* head = last->next;
if (head == last) {
// If there is only one node in the list
free(head);
last = NULL;
} else {
// More than one node in the list
last->next = head->next;
free(head);
}
return last;
}
void printList(struct Node* last) {
if (last == NULL) return;
struct Node* head = last->next;
while (1) {
printf("%d ", head->data);
head = head->next;
if (head == last->next) break;
}
printf("\n");
}
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
int main() {
struct Node* first = createNode(2);
first->next = createNode(3);
first->next->next = createNode(4);
struct Node* last = first->next->next;
last->next = first;
printf("Original list: ");
printList(last);
last = deleteFirstNode(last);
printf("List after deleting first node: ");
printList(last);
return 0;
}