-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.m
201 lines (154 loc) · 3.34 KB
/
List.m
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// List.m
// DoubleLinkList
//
// Created by Jeffrey Johnston on 9/23/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "List.h"
@implementation List
//Allow accessors for head
@synthesize head;
//Prints nodes starting with the head
//and moving down the list. It calls
//the print method in Node for the print
//format.
-(void) print
{
Node *p = head;
while (p != 0)
{
[p print];
p = p.next;
}
}
//Method to put nodes into the list starting
//at the back and adding to the front
-(void) addNode: (Node *) inNode
{
// Retain nodes pointers in the dList since
// the nodes will be released immediately after
// they are put into the list sucessfully by
// Node.m addNode call
[inNode retain];
if( head == 0 )
{
head = inNode;
[inNode setNext: 0];
[inNode setPrev: 0];
}
else
{
[inNode setNext: head];
[head setPrev: inNode];
[inNode setPrev: 0];
head = inNode;
}
}
// Method to locating nodes in the list based on
//their character value. Once found, it returns a
//pointer to the node.
-(Node *) locateNodeByChar: (char) lookupChar
{
Node *currentNode = head;
while (currentNode.c != lookupChar && currentNode != NULL)
{
currentNode = currentNode.next;
}
return currentNode;
}
// Method to remove nodes from the list. It will check to
// see if the node is in the list and/or if it is the head
// first, then it will re-set the node pointers so they
// maintain consistent links.
//
-(void) removeNodeFromList: (Node *) removeNode
{
// Check to see if node to be removed is the head
if (removeNode.prev == NULL && removeNode == head)
{
Node *temp = head;
temp.next.prev = NULL;
head = temp.next;
//temp.release;
}
if (removeNode.next != NULL)
{
removeNode.next.prev = removeNode.prev;
}
if (removeNode.prev != NULL)
{
removeNode.prev.next = removeNode.next;
}
if (removeNode.next != NULL && removeNode.prev != NULL)
{
[removeNode release];
}
}
//Method to sort the list from smallest Morse code value
//to the largest. Method will swap the pointers for the nodes
//and leave the data intact.
-(void) sortList
{
Node *cn;
Node *cnn;
BOOL swap = FALSE;
while (!swap)
{
swap = TRUE;
cn = head;
cnn = head.next;
while (cn.next != NULL)
{
if (cn.value > cnn.value)
{
swap = FALSE;
if(cn.prev == NULL)
{
Node *temp = head;
temp.next.prev = NULL;
head = temp.next;
cn.next = cnn.next;
cnn.prev = cn.prev;
cnn.next.prev = cn;
cn.prev = cnn;
cnn.next = cn;
}
else
{
cn.next = cnn.next;
cnn.prev = cn.prev;
cn.prev.next = cnn;
cnn.next.prev = cn;
cn.prev = cnn;
cnn.next = cn;
}
}
cn = cn.next;
cnn = cn.next;
}
}
}
//Method to clean out the pointer to nodes
//that were retained in the dList from List.m
//Currently, this is getting called correctly
//from main, but doesn't seem to release the
//pointers. Have a post in on the forums for
//help. It's exactly as it was demo'd in class,
//so I'm not sure why it's not working.
//
//I beleive it's because List.m is not creating the list
//and therefore doesn't own the rights to release here
-(void) freeList;
{
Node *p = head;
while (p != 0)
{
Node *s = p;
p = p.next;
[s release];
s = nil;
}
head = 0;
}
@end