![]() |
| | #1 | |
| Registered User Join Date: Mar 2007
Posts: 109
| singly linked circular list Project Quote:
Code: #include <iostream>
template <class T>
class slnode.h
{
//makes slnodes private data available to sclist
template <class> friend class sclist;
public:
//default constructor
slnode();
//constructor that takes a const reference to the Type
//stored in the data portion of the node
slnode(cosnt T&);
private:
T data;
int* nextPtr;
};
#include "slnode.template"
Code: #include "slnode.h"
template<class T>
class sclist
{
public:
//default constructor
sclist();
//deconstructor
~sclist();
//inserts a node at the head of the list
InsertAtHead(const T&);
//inserts a node at the tail of the list
InsertAtTail(const T&);
//returns a pointer to the node removed
//from the head of the list
RemoveFromHead();
//returns a pointer to the node removed
//from the tail of the list
RemoveFromTail();
//true if the list is empty, otherwise false
bool IsEmpty();
//moves headPtr one node forward in the list
void Advance();
//returns a pointer to the first node in the list
//starting from the head of the list, that contains
//the indicated data. If the data is not in the list,
//returns NULL.
slnode<T>* Locate(T data);
private:
T* headPtr;
int size;
};
#include "sclist.template"
Code: #include "slnode.h"
slnode::slnode()
{
data = NULL
nextPtr = NULL
}
slnode::slnode(T data)
{
data = T data
nextPtr = NULL
}
Code: #include sclist.h
sclist::sclist()
{
headPtr = NULL;
size = 0;
}
sclist::~sclist()
{
//here need to delete all the nodes and set the headPtr back to NULL
//and size equal to 0
//temp count variable
int i;
//loops through starting at the front and uses the RemoveFromHead
//function to remove all the nodes till none are left at which point
//the loop is broken and size and headPtr get set back to their
//starting values of 0 and NULL respectively.
for(i=1;i <= 5;i++)
{
RemoveFromHead();
}
size = 0;
headPtr = NULL;
}
sclist::InsertAtHead()
{
//this is a temp pointer that points to the current node
T* currentPtr = new currentPtr;
//if there is nothing in the list
if(headPtr == NULL)
{
// makes a new list
sclist = new T sclist;
//makes a new node and puts data into it
slnode = new T slnode(data);
//sets the headPtr of the list to the newly created node
headPtr = slnode;
//this sets the currentPtr equal to the headPtr
currentPtr = headPtr;
//since its a circular list the only nodes nextPtr must point to
//itself
nextPtr = headPtr;
//increments size
size++;
{
//if there is something in the list
else if(headPtr != NULL)
{
//it will fist make a new node
slnode = new T slnode(data);
//it will first set the headPtr to the new node
headPtr = slnode;
//will set the nextPtr of the slnode equal to teh currentPtr
//so that it will now point to the previous node
nextPtr = currentPtr;
//now needs to make the first nodes nextPtr point to the head
nextPtr = headPtr;
//increments the size
size++;
}
}
sclist::InsertAtTail()
{
//first will walk through size times to get to the correct node then
//will take that node that has the nextPtr pointing to the head and
//set currentPtr equal to that nextPtr. Then it will have that nextPtr
//point to the new node. Then have the new nodes nextPtr set
//equal to the currentPtr.
//temp count variable
int i;
//check to see if this is the first node in the list being added and if it is
//the node will just be inserted at the head
if(headPtr = NULL)
{
InsertAtHead();
}
else if(headPtr != NULL)
{
//will set the currentPtr back to headPtr so it starts at the beginning of
//the list
currentPtr = headPtr;
//this for loop will go through chaging what currentPtr is equal to
for(i=1;i<=size;i++)
{
currentPtr = currentPtr->nextPtr;
}
//creates a new node with the data in it.
slnode = new T slnode(data);
//set the nextPTr of the new node equal to the currentPtr which will be
//pointing to the headPtr
nextPtr = currentPTr;
//sets the last end nodes nextPtr equal to the new node
nextPtr = slnode;
}
}
//returns a pointer to the node removed from the head of the list
sclist::RemoveFromHead()
{
//going to have the currentPtr set equal to the headPtr. Then set the headPtr
//equal to the nextPtr of the node we are removing from the head. Then delete
//the currentPtr because it will be pointing to the now unliked node
currentPtr = headPtr;
headPtr = headPtr->nextPtr;
delete currentPtr;
}
//returns a pointer to the node removed from the tail of the list.
sclist::RemoveFromTail()
{
//Walk through the list size number of times.
//Set up a temp pointer named delete equal to that
//node. Then walk through the list again but this time size-1 times and
//set that nodes nextPtr equal to headPtr.Then delete the delete pointer
//temp count variable
int i;
//the tempPtr named tempEnd
T* delete = new delete;
//set currentPtr equal to the head
delete = head;
//the for loop to walk to the end of the list
for(i=1;i<=size;i++)
{
delete = delete->nextPtr;
}
//now will go through size-1 times
for(i=1;i<=size-1;i++)
{
currentPtr = currentPtr->nextPtr;
}
//this leaves the previous tail node unlinked
nextPtr = headPtr;
//deletes the unlinked node
delete delete;
}
//true if the list is empty, otherwise false.
sclist::IsEmpty()
{
//if the headPtr is equal to null then its empty;
if(headPtr == NULL)
{
IsEmpty = true;
}
else
{
IsEmpty = false;
}
}
//moves headPtr one node forward in the list.
sclist::Advance()
{
//is just advancing one spot so just set currentPtr equal to the nextPtr
currentPtr = currentPtr->nextPtr;
}
//returns a pointer to the first node in the list, starting from the head of
//the list,that contains the indicated data. If the data is not in the list,
//returns NULL.
sclist::Locate(T data)
{
//will first set currentPtr equal to head. Then it will advance one at a
//time and check the data to see if it matches the data in the parameter.
//If not it will continue to move on. A if statement will be used here.
//a temporary counter so that when or if the data is found it will have a
//number that will correspond to where in the size it is.
int count;
if(*currentPtr != data)
{
Advance();
//need an if statement to see if its at the end of the list will compare
//count to the size if they are equal it will stop and print not found.
if(size = count)
{
cout << "Data not found." << endl;
break;
}
count++;
}
//if the data is found
else if(*currentPtr == data)
{
//will print the count number the data is located at and then break.
cout << "Data found at node #" << count << endl;
break;
}
}
| |
| DarkDot is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Singly Linked List | devarishi | C Programming | 4 | 12-24-2008 12:00 AM |
| help! Placement of nodes in a Linked List | lostmyshadow | C Programming | 6 | 12-17-2007 01:21 PM |
| Anyone good with linked list.....I am not.... | chadsxe | C++ Programming | 11 | 11-10-2005 02:48 PM |
| singly linked to doubly linked | jsbeckton | C Programming | 10 | 11-06-2005 07:47 PM |
| singly linked list | clarinetster | C Programming | 2 | 08-26-2001 10:21 PM |