Docstoc

linked list 1

Document Sample
linked list 1 Powered By Docstoc
					//Linked List
//you need to write the main() for the program
//----- List.h -----
#ifndef LINKEDLIST
#define LINKEDLIST
#include <iostream>

using namespace std;
typedef int ElementType;

class List
{
private:

class Node
{
public:
//------ Node DATA MEMBERS
ElementType data;
Node * next;
//------ Node OPERATIONS
//-- Default constructor: initializes next member to 0

Node()
: next(0)
{ }
//-- Explicit-value constructor: initializes data member to dataValue
//-- and next member to 0

Node(ElementType dataValue)
: data (dataValue), next(0)
{ }
}; //--- end of Node class

typedef Node * NodePointer;

public:
//------ List OPERATIONS
List();
/*--------------------------------------------------------------------
Default constructor: builds an empty List object.
Precondition: None
Postcondition: first is 0 and mySize is 0.
--------------------------------------------------------------------*/
List(const List & origList);
/*--------------------------------------------------------------------
Copy constructor
Precondition: A copy of origList is needed.
Postcondition: A distinct copy of origList has been constructed.
--------------------------------------------------------------------*/

~List();
/*--------------------------------------------------------------------
Destructor
Precondition: This list's lifetime is over.
Postcondition: This list has been destroyed.
--------------------------------------------------------------------*/
const List & operator=(const List & rightSide);
/*--------------------------------------------------------------------
Assignment operator
Precondition: This list must be assigned a value.
Postcondition: A copy of rightSide has been assigned to this list.
--------------------------------------------------------------------*/
bool List::empty();
/*--------------------------------------------------------------------
Check if this list is empty
Precondition: None.
Postcondition: true is returned if this list is empty, false if not.
--------------------------------------------------------------------*/
void insert(ElementType dataVal, int index);
/*--------------------------------------------------------------------
Insert a value into a list at a given index.
Precondition: index is a valid list index: 0 <= index <= mySize,
Postcondition: dataVal has been inserted into the list at position
index, provided index is valid..
--------------------------------------------------------------------*/
void erase(int index);
/*--------------------------------------------------------------------
Remove a value from a list at a given index.
Precondition: index is a valid list index: 0 <= index < mySize
Postcondition: dataVal at list position index has been removed,
provided index is valid.
--------------------------------------------------------------------*/
int search(ElementType dataVal);
/*--------------------------------------------------------------------
Search for an data value in this list.
Precondition: None
Postcondition: Index of node containing dataVal is returned
if such a node is found, -1r if not.
--------------------------------------------------------------------*/
void display(ostream & out) const;
/*--------------------------------------------------------------------
Display the contensts of this list.
Precondition: ostream out is open
Postcondition: Elements of this list have been output to out.
--------------------------------------------------------------------*/
int nodeCount();
/*--------------------------------------------------------------------
Count the elements of this list.
Precondition: None
Postcondition: Number of elements in this list is returned.
--------------------------------------------------------------------*/
void reverse ();
/*--------------------------------------------------------------------
Reverse this list.
Precondition: None
Postcondition: Elements in this list have been reversed.
--------------------------------------------------------------------*/
bool ascendingOrder();
/*--------------------------------------------------------------------
Check if the elements of this list are in ascending order.
Precondition: None
Postcondition: true is returned if the list elements are in
ascending order, false if not.
--------------------------------------------------------------------*/
private:
//------ DATA MEMBERS
NodePointer first;
int mySize;
}; //--- end of List class

ostream & operator<<(ostream & out, const List & aList);

istream & operator>>(istream & in, List & aList);
#endif

//----- List.cpp -----
#include <iostream>
using namespace std;
#include "List.h"
//-- Definition of the class constructor

List::List()
: first(0), mySize(0)
{ }

//-- Definition of the copy constructor
List::List(const List & origList)
{
mySize = origList.mySize;
first = 0;
if (mySize == 0) return;
List::NodePointer origPtr, lastPtr;
first = new Node(origList.first->data); // copy first node
lastPtr = first;
origPtr = origList.first->next;
while (origPtr != 0)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}

//-- Definition of the destructor
inline List::~List()
{
List::NodePointer prev = first, ptr;
while (prev != 0)
{
ptr = prev->next;
delete prev;
prev = ptr;
}
}
// Definition of empty()
bool List::empty()
{
return mySize == 0;
}

//-- Definition of the assignment operator
const List & List::operator=(const List & rightSide)
{
mySize = rightSide.mySize;
first = 0;
if (mySize == 0) return *this;
if (this != &rightSide)
{
this->~List();
List::NodePointer origPtr, lastPtr;
first = new Node(rightSide.first->data); // copy first node
lastPtr = first;
origPtr = rightSide.first->next;

while (origPtr != 0)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}
return *this;
}

//-- Definition of insert()
void List::insert(ElementType dataVal, int index)
{
if (index < 0 || index > mySize)
{
cerr << "Illegal location to insert -- " << index << endl;
return;
}
mySize++;
List::NodePointer newPtr = new Node(dataVal),
predPtr = first;
if (index == 0)
{
newPtr->next = first;
first = newPtr;
}
else
{
for(int i = 1; i < index; i++)
predPtr = predPtr->next;
newPtr->next = predPtr->next;
predPtr->next = newPtr;
}
}
//-- Definition of erase()
void List::erase(int index)
{
if (index < 0 || index >= mySize)
{
cerr << "Illegal location to delete -- " << index << endl;
return;
}
mySize--;
List::NodePointer ptr,
predPtr = first;
if (index == 0)
{
ptr = first;
first = ptr->next;
delete ptr;
}

else
{
for(int i = 1; i < index; i++)
predPtr = predPtr->next;
ptr = predPtr->next;
predPtr->next = ptr->next;
delete ptr;
}
}


//-- Definition of search()
int List::search(ElementType dataVal)
{
int loc;
List::NodePointer tempP = first;
for (loc = 0; loc < mySize; loc++)
if (tempP->data == dataVal)
return loc;
else
tempP = tempP->next;
return -1;
}

//-- Definition of display()
void List::display(ostream & out) const
{
List::NodePointer ptr = first;
while (ptr != 0)
{
out << ptr->data << " ";
ptr = ptr->next;
}
}
//-- Definition of the output operator
ostream & operator<<(ostream & out, const List & aList)
{
aList.display(out);
return out;}
//-- Definition of nodeCount()
int List::nodeCount()
{ // or simply, { return mySize; }
int count = 0;
List::NodePointer ptr = first;
while (ptr != 0)
{
count++;
ptr = ptr->next;
}
return count;
}

//-- Definition of reverse()
void List::reverse()
{
NodePointer prevP = 0,
currentP = first,
nextP;
while (currentP != 0)
{
nextP = currentP->next;
currentP->next = prevP;
prevP = currentP;
currentP = nextP;
}
first = prevP; // new head of (reversed) linked list
}


//-- Definition of ascendingOrder()
bool List::ascendingOrder()
{
if (mySize <= 1)
//empty or one element list
return true;
//else
NodePointer prevP = first,
tempP = first->next;
while (tempP != 0 && prevP->data <= tempP->data)
{
prevP = tempP;
tempP = tempP->next;
}
if (tempP != 0)
return false;
// else
return true;
}

				
DOCUMENT INFO
Shared By:
Tags:
Stats:
views:7
posted:2/10/2011
language:English
pages:6