-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_lists.c
168 lines (139 loc) · 2.68 KB
/
linked_lists.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
char *name;
struct node *link;
} node_t;
void count_of_nodes(node_t *head)
{
int count = 0;
if (head == NULL)
printf("Linked List is empty");
node_t *ptr = NULL;
ptr = head;
while (ptr)
{
count++;
ptr = ptr->link;
}
printf("%d\n", count);
}
void print_data(node_t *head)
{
int count = 0;
if (!head)
printf("Linked list is empty");
node_t *ptr = head;
while (ptr)
{
count++;
printf("Name: %s, Data position : %d, Value : %d\n", ptr->name, count, ptr->data);
ptr = ptr->link;
}
putchar('\n');
}
void add_at_end(node_t *head, int data, char *name)
{
node_t *ptr, *temp;
ptr = head;
temp = (node_t *)malloc(sizeof(node_t));
temp->data = data;
temp->name = name;
temp->link = NULL;
while (ptr->link)
ptr = ptr->link;
ptr->link = temp;
}
node_t* add_beg(node_t *head, int data, char *name)
{
node_t *ptr = malloc(sizeof(node_t));
ptr->link = head;
ptr->data = data;
ptr->name = name;
head = ptr;
return (head);
}
void del_pos(node_t **head, int position)
{
node_t *current = *head;
node_t *previous = *head;
if(!(*head))
printf("List is already empt!");
else if (position == 1)
{
*head = current->link;
free(current);
current = NULL;
}
else
{
while (position != 1)
{
previous = current;
current = current->link;
position--;
}
previous->link = current->link;
free(current);
current = NULL;
}
}
node_t* del_list(node_t *head)
{
node_t *temp = malloc(sizeof(node_t));
temp = head;
while (temp != NULL)
{
head = temp->link;
free(temp);
temp = head;
}
return (NULL);
}
node_t* reverse(node_t *head)
{
node_t *prev = NULL;
node_t *next = NULL;
while (head)
{
next = head->link;
head->link = prev;
prev = head;
head = next;
}
head = prev;
return (head);
}
int main(void)
{
node_t *head = NULL;
head = (node_t *)malloc(sizeof(node_t));
head->data = 45;
head->name = "Joe";
head->link = NULL;
node_t *current = malloc(sizeof(node_t));
current->data = 100;
current->name = "Julia";
current->link = NULL;
head->link = current;
current = malloc(sizeof(node_t));
current->data = 67;
current->name = "Daniel";
current->link = NULL;
head->link->link = current;
//count_of_nodes(head);
print_data(head);
add_at_end(head, 6788, "Mane");
print_data(head);
head = add_beg(head, 76, "John");
print_data(head);
del_pos(&head, 3);
print_data(head);
head = reverse(head);
print_data(head);
printf("%d and %d and %d", head->data, head->link->data, head->link->link->data);
head = del_list(head);
print_data(head);
return 0;
}