-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntNode.java
147 lines (120 loc) · 2.81 KB
/
IntNode.java
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
/**
* @author Rylee Davis
* cs272 lab4
*/
package myprograms;
public class IntNode
{
//instance variables
private int data;
private IntNode link;
//no argument constructor
public IntNode()
{
data = 0;
link = null;
}//end IntNode()
/**
*@pre _node is type integer and points to the head
*@post creates new node that contains data and link to the next node
*
*@param _data is the initial data of new node
*@param _node is reference the new node points to
*/
//constructor
public IntNode(int _data, IntNode _node)
{
data = _data;
link = _node;
}//end IntNode()
//accessor for Data
public int getData()
{
return data;
}//end getData()
//mutator for Data
public void setData(int _data)
{
data = _data;
}//end setData()
//accessor for link
public IntNode getLink()
{
return link;
}//end getLink()
//mutator for link
public void setLink(IntNode _link)
{
link = _link;
}//end setLink()
//calls and prints nodes
public String toString()
{
String temp = String.format("%s", this.getData());
IntNode cursor = this.getLink();
while( cursor != null)
{
temp = temp + " -> " + cursor.getData();
cursor = cursor.getLink();
}//end while
return temp;
}//end toString
/**
* @pre new data is an integer
* @post new node is created and placed after head
*
* @param newdata is the data that is going to be placed in new node
*/
//method to add node after current node
public void addNodeAfterThis(int newdata)
{
IntNode temp2 = new IntNode(newdata, this.getLink());
this.setLink(temp2);
}//end addNode
/**
* @pre node cannot be end of list
* @post newest node is removed from list
*/
//method to remove node after current node
public void removeNodeAfterThis()
{
setLink(getLink().getLink());
}//end removeNode
/**
*@param head is the starting point of the linked list
*@return an integer value telling the number of nodes in the list
*/
//method to get number of nodes in the list starting from a given node head
public static int listLength(IntNode head)
{
IntNode cursor = head;
int count = 0;
while(cursor != null)
{
count++;
cursor = cursor.link;
}//end while
return count;
}//end listLength
/**
* @pre the head is not null
*
* @param head is the starting point of linked list
* @param data is the data to search for
* @return returns true if data exists in list starting with head, otherwise returns false
*/
//method to search whether linked list contains given value data
public static boolean search(IntNode head, int data)
{
IntNode cursor = head;
while(cursor != null)
{
if(cursor.getData() == data)
{
return true;
}//end if
cursor = cursor.getLink();
}//end while
return false; //DONT FORGET TO COMMENT OUT
}//end search
}//end class