-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.m
More file actions
115 lines (97 loc) · 1.74 KB
/
List.m
File metadata and controls
115 lines (97 loc) · 1.74 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
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
// Created by Zackery Leman on 2/13/14.
// List.m
//
//
// Modified by Zackery Leman on 11/20/13
// Copyright (c) 2014All rights reserved.
//Sets up the list data structure
#import "List.h"
@implementation List
@synthesize tail = _tail;
@synthesize head = _head;
@synthesize count = _count;
- (id)init
{
if (self = [super init]) {
self.count = 0;
self.head = NULL;
self.tail = NULL;
}
return self;
}
- (void)addToHead:(id)anObject
{
Node *temp = [[Node alloc] init];
temp.data = anObject;
temp.next = self.head;
self.head = temp;
if (self.count == 0) {
self.tail = temp;
}
self.count++;
}
- (void)addToTail:(id)anObject
{
Node *temp = [[Node alloc] init];
temp.data = anObject;
temp.next = nil;
self.tail.next = temp;
self.tail = temp;
if (self.count == 0) {
self.head = temp;
}
self.count++;
}
- (id)removeFromHead
{
id object = self.head.data;
if (self.count == 1) {
self.tail = nil;
}
if (self.count > 0) {
self.count--;
}
self.head = self.head.next;
return object;
}
- (id)removeFromTail
{
id temp = self.tail.data;
Node *finger = self.head;
while (finger.next.next) {
finger = finger.next;
}
self.tail = finger;
self.tail.next = nil;
if (self.count > 1) {
self.count--;
} else {
self.count = 0;
self.head = nil;
self.tail = nil;
}
return temp;
}
- (id)getHeadData
{
return self.head.data;
}
- (id)getTailData
{
return self.tail.data;
}
- (void)clear
{
self.head = nil;
self.tail = nil;
self.count = 0;
}
- (int)size
{
return self.count;
}
- (BOOL)isEmpty
{
return self.count == 0;
}
@end