I'm attempting to create a function getPredecessor() in which the function recieves an int in reference to a position in the list and should return a pointer to the node before that position. If the position is the first place in the list, return 0.
Here's my header (followed by my lame attempt to define getPredecessor()) so you can see what I'm attempting. The getPredecessor() will be used in functions following its protype and definition. FYI, some of the functions dont have a data type because I'm still working on them, so please disregard any of those
#include<iostream>
using namespace std;
typedef int ElementType;
class LinkedList
{
private:
class Node
{
public:
ElementType data;
Node * next;
};
typedef Node * NodePointer;
NodePointer ptr; //pointer to nodes
NodePointer first; //points to first node
NodePointer predptr; //points to predecessor node
NodePointer newptr; //points to a new node
int mySize; //number of nodes
public:
LinkedList(int size); //constructor
void display(ostream & out)const;
int getPredecessor(int pos);
int length();
void insert(ElementType item, int pos);
erase();
bool empty();
bool intList();
~LinkedList(); //destructor
LinkedList(const LinkedList & orig); //copy constructor
const LinkedList & operator=(const List & other);
};
ostream & operator <<(ostream & out, const LinkedList & aList);
getPredecessor definition
int LinkedList::getPredecessor(int pos)
{
if(ptr==first)
return 0;
else
{
for(int i = 0; i > pos; i++)
{
ptr->data = predptr->data;
}
return ptr->data;
}
}