Implement the getNodeAtPosition function of the LinkedList class .
Node * LinkedList::getNodeAtPosition(int position)
The job of this function is to return a pointer to the node the node at the given position. If the list is empty, position <= 0 or position > length, then the function should return the null pointer.
This is a helper function that can be used by various other member functions (e.g. insertAtPosition, removeAtPosition, getValue. setValue).
I started with this:
LinkedList::LinkedList(int myArray[], int size) {
if (position <= 0 || position > length)
return false;
}
// ConsoleApplication10.cpp : Defines the entry point for the
console application.
//
#include "stdafx.h"
struct Node
{
int info;
struct Node *next;
}*start;
class LinkedList
{
int main()
{
return 0;
}
Node * LinkedList::getNodeAtPosition(int
position)
{
int pos, counter = 0;
struct Node *temp, *s, *ptr;
pos = position;
int i;
s = start;// start points to start
of liked list
while (s != NULL)
{
s =
s->next;
counter++;
}
if (pos == 1)
{
if (start ==
NULL)
{
return NULL;
}
else
{
ptr = start;
return ptr;
}
}
else if (pos > 1 && pos
<= counter)
{
s = start;
for (i = 1; i
< pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next =
temp;
return
temp;
}
else
{
return
NULL;
}
}
};
Get Answers For Free
Most questions answered within 1 hours.