-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list_reverse.c
97 lines (80 loc) · 2.2 KB
/
linked_list_reverse.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data; //could be any data type, or more fields
struct node *link; // ONLY ONE LINK
};
void count_nodes(struct node *head);
struct node *add_at_beginning(struct node *head, int value);
void add_at_beginning_pointer(struct node **head, int value);
struct node *reverse_linked_list(struct node *head);
struct node *add_at_end(struct node *ptr, int value);
int main(void)
{
struct node *head = (struct node *)malloc(sizeof(struct node)); //Head Node pointint to first node
head->data = 111;
head->link = NULL;
struct node *ptr = head; //insert at the end - simple way
ptr = add_at_end(ptr, 22);
ptr = add_at_end(ptr, 333);
ptr = head; // to place the pointer at the start of the linked list to traverse
count_nodes(head);
head = reverse_linked_list(head);
count_nodes(head);
return 0;
}
struct node *add_at_beginning(struct node *head, int value)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = value;
ptr->link = NULL;
ptr->link = head;
head = ptr;
return head;
}
void add_at_beginning_pointer(struct node **head, int value) // adds a node at the beginning but w/o head = add_at_beginning(), just add_at...
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = value;
ptr->link = NULL;
ptr->link = *head;
*head = ptr;
}
void count_nodes(struct node *head)
{
int count = 0;
if (head == NULL)
{
printf("Linked List is empty");
}
struct node *ptr = NULL;
ptr = head;
while (ptr != NULL)
{
count++;
printf("Count: %d , Node Value: %d\n", count, ptr->data);
ptr = ptr->link;
}
printf("Count= %d\n", count);
}
struct node *add_at_end(struct node *ptr, int value)
{
struct node *prev = (struct node *)malloc(sizeof(struct node));
prev->data = value;
prev->link = NULL;
ptr->link = prev;
return prev;
}
struct node *reverse_linked_list(struct node *head)
{
struct node *prev, *next = NULL;
while (head != NULL)
{
next = head->link;
head->link = prev;
prev = head;
head = next;
}
head = prev;
}