-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
72 lines (58 loc) · 1.48 KB
/
list.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
#include "selforg.h"
Node *createNode(int x){
Node *newnode = (Node *)malloc(sizeof(Node));
if(newnode == NULL){
printf("No memory created");
exit(MALLOC_FAILURE);
}
newnode->data=x;
newnode->next=NULL;
newnode->count=1;
newnode->prev=NULL;
return newnode;
}
void insertatbegin(Node **head , int x){
Node *newnode = createNode(x);
if(*head == NULL){
*head = newnode;
//printf("New Node attached successfully");
return;
}
newnode->next=(*head);
(*head)->prev=newnode;
*head=newnode;
//printf("Node attached successfully");
}
Node *find(Node** head, int x){
Node *current = *head;
while(current != NULL){
if(current->data == x){
current->count++;
return current;
}
current = current->next;
}
return NULL;
}
void move_to_front(Node **head, Node *node){
if (*head == node){
return;
}
if(node->prev){
node->prev->next = node->next;
}
if(node->next){
node->next->prev = node->prev;
}
node->next = *head;
node->prev = NULL;
(*head)->prev = node;
*head = node;
}
void write_to_file(Node *head, FILE *output_file){
Node *current = head;
while (current != NULL) {
fprintf(output_file, "Number: %d, Access Count: %d\n", current->data, current->count);
current = current->next;
}
}