-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.c
152 lines (132 loc) · 2.79 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
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
/**
* \file list.c
* A list of equilibrium. This is used to represent a group of
* equilibria, either the positively indexed equilibria or the
* negatively indexed equilibria. Nodes from one list are connected
* to the other list via their link variable, which enables the
* navigation of the bi-partite graph, of equilibria.
*
* Author: Tobenna Peter, Igwe ptigwe@gmail.com August, 2012
*
* Note: This file generates both list.o and glist.o,
* to compile the file with gnump use the -DGLEMKE flag
*/
#include "equilibrium.h"
#include "list.h"
/* Creates a new node */
node* newnode(int n)
{
node* nd;
nd = malloc(sizeof(node));
nd->link = malloc(n * sizeof(int));
int i;
for(i = 0; i < n; ++i)
{
nd->link[i] = -1;
}
nd->next = NULL;
return nd;
}
/* Adds a new node with the given equilibrium to the list
* and returns its index position */
int addNode(node* list, Equilibrium eq)
{
int i = 0;
node* cur = list;
while(cur->next != NULL)
{
cur = cur->next;
++i;
}
node* n = newnode(eq.lcpdim-2);
n->eq = eq;
cur->next = n;
++i;
return i;
}
/*Finds and returns the location of an equilibrium in the list
Returns -1 if the equilibrium is not in the list*/
int contains(node* list, Equilibrium eq)
{
int result = -1;
int i = 0;
node* cur = list;
while(cur != NULL)
{
if(equilibriumisEqual(cur->eq, eq))
{
result = i;
break;
}
cur = cur->next;
++i;
}
return result;
}
/* Adds the equilibrium to the list, and returns its index position
* in the list. If the equilibrium already exists, it returns the
* index where it is stoeed, and doesn't add it. */
int addEquilibrium(node* list, Equilibrium eq)
{
int result = contains(list, eq);
if(result == -1)
{
result = addNode(list, eq);
}
return result;
}
/* Get node at index */
int listlength(node* list)
{
if(list == NULL)
return 0;
int i = 1;
node* next = list;
while(next->next != NULL)
{
++i;
next = next->next;
}
return i;
}
/* Computes the number of elements in the list */
node* getNodeat(node* list, int n)
{
if(n >= listlength(list) || n < 0)
return NULL;
int i = 0;
node* cur = list;
for(i = 0; i < n; ++i)
{
cur = cur->next;
}
return cur;
}
/* Prints all the elements in the list */
void printlist(node* list, char prefix)
{
char smp [2*DIG2DEC(MAX_DIGITS) + 4];
node* cur = list;
int i = 0;
while(cur != NULL)
{
sprintf(smp, "%cEq[%d]: ", prefix, i++);
colpr(smp);
colprEquilibrium(cur->eq);
int k;
colpr("Leads to: ");
colpr("");
for(k = 0; k < cur->eq.nrows; ++k)
{
sprintf(smp, "%d->%d ", k+1, cur->link[k]);
colpr(smp);
}
colpr("");
for(; k < cur->eq.lcpdim - 2; ++k)
{
sprintf(smp, "%d->%d ", k+1, cur->link[k]);
colpr(smp);
}
cur = cur->next;
}
}