c++ linked lists

Hi: I'm new to c++ programming, I nee to write a code for a linked list. To be more specific I need to write the removeItem, displayList and the sort functions. The other functions were gived to me. I;m not sure if I have write the correct code for the removeItem and the displayList functions. This is what I have.


#include "linkedCircle.h";
linkedCircle::linkedCircle()
{
start = NULL;
}
//insert item into circle
void linkedCircle::addItem(string item)
{
node * someNode;
someNode = new node(item);

if( start == NULL ) //empty list case!
{
someNode->next = someNode;
someNode->previous = someNode;
start = someNode;
}
else
{
someNode->next = start->next;
start->next->previous = someNode;
start->next = someNode;
someNode->previous = start;
}
}
//remove item from list
void linkedCircle::removeItem(string item)
{
Do
{
node *someNode, *temp;
if(start==NULL)
cout<<”Empty list, no nodes to delete”<<endl;
else if(someNode==item)
temp=someNode;
someNode=someNode->next;
delete temp;
else
{
cout<<”node not found”<<endl;
}
someNode->next=start->next;
}
While(someNode->next!=NULL)

}
//move start pointer to right
void linkedCircle::shiftRight()
{
start = start->next;
}
//move start pointer to left
void linkedCircle::shiftLeft()
{
start = start->previous;
}
//return start->data
string linkedCircle::getItem()
{
return start->data;
}
//show contents of list
void linkedCircle::displayList()
{
node * someNode;
someNode=start;
do
{
If(start==NULL)
cout<<”End of list”<<endl;
else
{
Cout<<someNode<<endl;
someNode=start->next;
}
While(someNode->next != NULL)
}
//sort contents into order
void linkedCircle::sort()
{
}

Your aware the STL provides a linked list in C++ for you already?
//remove item from list
void linkedCircle::removeItem(string item)
{
Do
{
node *someNode, *temp;
if(start==NULL)
cout<<”Empty list, no nodes to delete”<<endl;
else if(someNode==item)
temp=someNode;
someNode=someNode->next;
delete temp;
else
{
cout<<”node not found”<<endl;
}
someNode->next=start->next;
}
While(someNode->next!=NULL)

}

your code is difficult to follow since (a) identation is gone so the intent can only be guess (b) {} are not used

reformat and make it more readable. use always always {} around one or more statements in conditions to make the purpose clear.

Topic archived. No new replies allowed.