-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistImplementation.c
More file actions
74 lines (64 loc) · 1.53 KB
/
listImplementation.c
File metadata and controls
74 lines (64 loc) · 1.53 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
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "listTypes.h"
ListNode *head = NULL;
/* Pushes a file path to the wd map */
void list_push(int key, const char *data){
ListNode *newNode = (ListNode *)malloc(sizeof(ListNode));
newNode->key=key;
newNode->data= (char *) malloc(sizeof(char)*(strlen(data)+1));
strcpy(newNode->data,data);
newNode->next = NULL;
if(head == NULL)
head = newNode;
else{
ListNode *temp = head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next = newNode;
}
}
/* Removes the entry of given key */
void list_remove(int key){
ListNode *current = head;
ListNode *previous= NULL;
if(head==NULL)
return;
while(current->key != key){
if(current->next==NULL)
return;
else{
previous = current;
current = current->next;
}
}
if(current == head)
head = head->next;
else
previous->next = current->next;
free(current->data);
free(current);
}
/* Return the path that is mapped to the given wd */
const char *list_find(int key){
if(head==NULL)
return NULL;
ListNode *temp = head;
while(temp!=NULL){
if(temp->key==key)
return temp->data;
temp=temp->next;
}
/* Not found*/
return NULL;
}
/* Print map */
void list_print(){
ListNode *temp = head;
while(temp!=NULL){
printf("Wd:%d File:%s\n",temp->key,temp->data);
temp=temp->next;
}
}