CSCI 2010 Lab11 Link-Lists
Lab 11A Linked-Lists
Preparation
- Create a Visual Studio C++ Project C2010Lab11A
- Add the following to the project.
//LinkedList.cpp
#include
#include “LinkedList.h”
using namespace std;
//—————————————————
//List Element Members
//—————————————————
ListElement::ListElement(int d, ListElement * n)
{
datum=d;
next=n;
}
int ListElement::getDatum () const
{
return datum;
}
ListElement const* ListElement::getNext () const
{
return next;
}
//—————————————————
//LinkedList Members
//—————————————————
LinkedList::LinkedList ()
{
head=NULL;
}
void LinkedList::insertItem(int item)
{
ListElement *currPtr = head;
ListElement *prevPtr = NULL;
ListElement *newNodePtr; //points to a new node
while(currPtr != NULL && item >currPtr->datum)
{
prevPtr = currPtr;
currPtr = currPtr->next;
}
newNodePtr = new ListElement(item, currPtr);
if (prevPtr == NULL) // insert at thebeginning
head=newNodePtr;
else
prevPtr->next =newNodePtr;
}
void LinkedList::makeList()
{
int InValue;
ListElement *currPtr;
ListElement *newNodePtr;