#include "list.h"
/*
constructor's purpose is to make nodes i.e. dynamic variables containing 1
or more values and a pointer variable that references the next node, else NULL
*/
Node:: Node ( int _val, Node* _next)
{
next = _next;
value = _val;
}
Node* Node:: prefix ( int value, Node* head)
{
head = new Node( value,head) ;
return head;
}
Node* Node:: searchElem ( Node* head,int searchKey)
{
Node* trav = head;
while ( searchKey ! = trav- > getValue( ) && trav- > getNext( ) ! = NULL )
trav = trav- > getNext( ) ;
if ( searchKey == trav- > getValue( ) )
return trav;
else
return NULL ;
}
/*
FUNCTION: appends in the middle or at the end of a linked list
INPUT:
1. pointer to the previous node you are appending to
2. the value contained in the successive node you are appending
*/
void Node:: insert ( Node* prev, int value)
{
prev- > setNext( new Node( value,prev- > next) ) ;
}
/*
FUNCTION: removes a node and deallocates the memory assigned to its dynamic variable
INPUT:
1. pointer to the previous node
2. pointer to the node we would like to remove
*/
void Node:: remove ( Node* prev, Node* target)
{
prev- > next = target- > next;
delete target;
/*
memory allocated to dynamic variables comes from the freestore area of main memory which is limited, for this reason we use delete to restore/deallocate the memory used to the operating system which governs the freestore so we can use this memory for other processes
*/
}
int Node:: getValue ( )
const
{
return value;
}
Node* Node:: getNext ( )
const
{
return next;
}
void Node:: setValue ( int _value)
{
value = _value;
}
void Node:: setNext ( Node* _next)
{
next = _next;
}
I2luY2x1ZGUgImxpc3QuaCIKCi8qCmNvbnN0cnVjdG9yJ3MgcHVycG9zZSBpcyB0byBtYWtlIG5vZGVzIGkuZS4gZHluYW1pYyB2YXJpYWJsZXMgY29udGFpbmluZyAxCm9yIG1vcmUgdmFsdWVzIGFuZCBhIHBvaW50ZXIgdmFyaWFibGUgdGhhdCByZWZlcmVuY2VzIHRoZSBuZXh0IG5vZGUsIGVsc2UgTlVMTAoqLwoKTm9kZTo6Tm9kZShpbnQgX3ZhbCwgTm9kZSogX25leHQpCnsKICBuZXh0ID0gX25leHQ7CiAgdmFsdWUgPSBfdmFsOwp9CgpOb2RlKiBOb2RlOjpwcmVmaXgoaW50IHZ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