-
Notifications
You must be signed in to change notification settings - Fork 1
/
sllLib.c
164 lines (149 loc) · 3.76 KB
/
sllLib.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
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* Copyright (c) 2002 Berliner Speicherring-Gesellschaft fuer Synchrotron-
* Strahlung mbH (BESSY).
* Copyright (c) 2002 Southeastern Universities Research Association, as
* Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/* sllLib.c */
/************************DESCRIPTION***********************************
*PUBLIC* functions for single link list operations.
**********************************************************************/
#include <stdio.h>
#include "sllLib.h"
/*********************************************************
add a node at the end of the list
**********************************************************/
int sllAdd(SLIST *list,SNODE *new)
{
if (list->count > 0) {
list->last->next = new;
}
else
list->first = new;
new->next = NULL;
list->last = new;
list->count++;
return(0);
}
/*********************************************************
find the N th node of the list
**********************************************************/
SNODE *sllNth(SLIST *list,int n)
{
SNODE *pt;
int i,count;
count = list->count;
if (n >= count) return(list->last);
if ( n <= 0) return(list->first);
pt = list->first;
for (i=1;i<n;i++)
pt = pt->next;
return(pt);
}
/*********************************************************
insert a node after prev node of the list
**********************************************************/
void sllInsert(SLIST *list,SNODE *prev,SNODE *new)
{
if (prev == NULL) {
new->next = list->first;
list->first = new;
}
else if (prev == list->last) {
prev->next = new;
list->last = new;
new->next = NULL;
}
else {
new->next = prev->next;
prev->next = new;
}
list->count++;
return;
}
/*********************************************************
insert a new node before next node of the list
**********************************************************/
int sllPrecede(SLIST *list,SNODE *next,SNODE *new)
{
SNODE *prev,*last,*now;
if (next == NULL) {
if (list->count == 0){
list->first = new;
list->last = new;
}
else {
last = list->last;
last->next = new;
list->last = new;
}
}
else {
prev = NULL;
now = list->first;
while (now ) {
if (now == next) break;
prev = now;
now = now->next;
}
if (now) {
new->next = now;
if (prev == NULL)
list->first = new;
else prev->next = new;
}
else return(-1);
}
list->count++;
return(0);
}
/*********************************************************
remove the node from the list
**********************************************************/
void sllRemove(SLIST *list,SNODE *node)
{
SNODE *prev,*now,*last;
prev = NULL;
now = list->first;
last = list->last;
while (now ) {
if (now == node) break;
else {
prev = now;
now = now->next;
}
}
if (now) {
if (prev == NULL){
list->first = now->next;
prev = node;
}
else prev->next = now->next;
if (node == last) list->last = prev;
list->count--;
}
}
/*********************************************************
find the node from the list
**********************************************************/
int sllFind(SLIST *plist,SNODE *pnode)
{
int i;
SNODE *pt;
pt = plist->first;
for (i=1;i<=plist->count;i++) {
if (pt == pnode) {
return(i);
}
pt = sllNext(pt);
}
return(-1);
}