-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.c
109 lines (92 loc) · 2.19 KB
/
linkedList.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
#include <stdio.h>
#include <stdlib.h>
#include "linkedList.h"
#include <string.h>
//#define IP_SIZE 128
//#define USER_SIZE 256
#define DEFPORT 6080
//node_t *head = NULL;//malloc(sizeof(struct node_t));
//node_t *tail = NULL;
int add(struct node_t *node, struct node_t *head)
{
//head = ndhead;
//should never reach here only for emergencies
node_t * current = head;
if(head == NULL)
{
head = node;
node->port = DEFPORT;
return 0;
}
while (current->next != NULL && current->next->port == current->port + 1) {
current = current->next;
}
node->port = current->port + 1;
if (current->next == NULL) // end fo the list
{
//its safe to assume the node is already on the heap
current->next=node;
node->previous=current;
node->next=NULL;
node->pid = current->pid +1;
}
// node goes in between two other nodes, so you have to insert it
if (current->port + 1 != current->next->port) {
// set up node
node->previous = current;
node->next = current->next;
// set up node->next
current->next->previous = node;
current->next = node;
node->port = current->port+1;
return 0;
}
return 0;
}
node_t *remv(int port, struct node_t *head)
{
//head = nhead;
node_t *current = head;
struct node_t *temp;
while (current->next != NULL) {
if(current->port == port)break;
current = current->next;
}
if(current->next == NULL && current->port == port)
{
temp=malloc(sizeof(struct node_t));
memcpy(temp,current, sizeof(struct node_t));
current->previous->next = NULL;
free(current);
return temp;
}
else if (current->port == port) {
current->next->previous = current->previous;
current->next->previous->next = current->next;
}
else
{
printf("error\n");
current->error = -1;
return current;
}
return current;
}
node_t *lookup(int port, struct node_t *head)
{
node_t * current = head;
while(current->next != NULL && current->port != port)
{
current = current->next;
}
return (current != NULL && current->port == port) ? current : NULL;
}
void printLst(struct node_t *head)
{
node_t * current = head;
while(current != NULL)
{
printf("port: %d \n pid: %d \n next: %s \n", current->port, current->pid, current-> next);
current = current->next;
}
}