Doubly Linked Lists
Hey... Recently I was given a project from school to write the code for the functions they provided. Well I''m having a little problem inserting and deleting. We went over this in school and the code I''m following was written on the board and I''ve worked it out on paper and it seems fine. Maybe you guys could take a look at it and tell me what you think is wrong.
This is the cpp file of it. It does not use a tail and really not even a head. This was what are teacher told us to do. It uses a cursor to iterate.
//Begin listdbl.cpp
#include "listdbl.h"
template
ListNode::ListNode(const LE &elem, ListNode *priorPtr, ListNode *nextPtr )
{
element = elem;
next = nextPtr;
prior = priorPtr;
}
//Create a new list
template
List::List( int ignored )
{
head = 0;
cursor = new ListNode(NULL,NULL,NULL);
size = 0;
count = 0;
}
//Destroy the list
template
List::~List( )
{
clear();
head = cursor = NULL;
}
//Insert something
template
void List::insert( const LE &newElement ){
ListNode *node = new ListNode(newElement, NULL,NULL);
node->next = cursor->next;
node->prior = cursor->prior;
if(count == 0){ //Insert at beginning
cursor->prior = node;
cursor=node;
size = 1;
count++;
}
else{ //Insert in the list
cursor->prior = cursor->next;
cursor->prior->next = node;
cursor = node;
cursor->next = node;
size++;
}
gotoNext();
}
//Remove from list
template
void List::remove(){
ListNode *node = new ListNode(NULL,NULL,NULL);
if(count==1){ //Remove from beginning of list
node = cursor;
if(cursor->next)
cursor->next->prior = NULL;
delete node;
}
if(count==size){ //Remove from end of list
node = cursor;
cursor->prior->next = cursor;
cursor = cursor->prior;
delete node;
}
else //Remove from inside the list
{
node = cursor;
cursor->prior->next = cursor->next;
cursor->next->prior = cursor->prior;
delete node;
cursor = cursor->prior;
}
size--;
}
template
void List::replace(const LE &newElement){
cursor->element = newElement;
}
template
void List::clear(){
while(!empty())
remove();
}
template
int List::empty() const
{
return size==0;
}
template
int List::full() const
{
return 0;
}
template
int List::gotoBeginning()
{
while(cursor->prior){
cursor = cursor->prior;
count--;
}
return 1;
}
template
int List::gotoEnd()
{
while(cursor->next){
cursor=cursor->next;
count++;
}
return 1;
}
template
int List::gotoNext()
{
if(cursor->next){
cursor = cursor->next;
count++;
return 1;
}
return 0;
}
template
int List::gotoPrior()
{
if(cursor->prior){
cursor = cursor->prior;
count--;
return 1;
}
return 0;
}
template
LE List::getCursor() const
{
return cursor->element;
}
template < class LE >
void List:: showStructure () const
// Outputs the elements in a list. If the list is empty, outputs
// "Empty list". This operation is intended for testing and
// debugging purposes only.
{
ListNode *p; // Iterates through the list
if ( cursor == 0 )
cout << "Empty list" << endl;
else
{
p = cursor;
do
{
if ( p == cursor )
cout << "[" << p->element << "] ";
else
cout << p->element << " ";
p = p->next;
}
while ( p != head );
cout << endl;
cout << count << endl;
cout << size << endl;
}
}
//End list
The problem has something to do with prior and next... Whenever I want to go back one it will give me the same thing.. like..
Command: +a +b +c
Insert a
Insert b
Insert c
Cursor is at c.
Command: P
Cursor is at b.
Command: P
Cursor is at b.
You see? Well could anyone tell me what''s wrong? Thanks for your time, I really appreciate it.
I think you need to clarify what you''re doing in your insert function. You''re inserting at the cursor? After creating a new node, node, you''ll need something like:
node->next = cursor;
node->prior = cursor->prior;
cursor->prior = node;
cursor = node;
I can''t figure out what your code does, but I think that''s what it wants to do
Ahh... Other peoples homework...
Dave
node->next = cursor;
node->prior = cursor->prior;
cursor->prior = node;
cursor = node;
I can''t figure out what your code does, but I think that''s what it wants to do
Ahh... Other peoples homework...
Dave
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement