Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help With Link List

SupaflyfrankSupaflyfrank Posts: 61Member
This is my first attempt with creating a link list from scratch. I understand STL has its own linklist template but I just want to make one from scratch for experience. What I need is I am confused I will make this link list not to use a static head but also allow the head node to be delete thus the next node become the head and after all of that the rest of the nodes will know the new head. But the problem Im facing with that unless I have it at static as soon as the head node changes the only way the rest of the nodes change is if i go through the entire link list and set the nodes head to the current head link list. Any suggestion on how I will allow the possibility with having a changing head node with having to use a static head to update the rest of the nodes. I hope what I said made sense. Everything I created so far works perfectly I just need that one problem fix. If anyone has any suggestion it would be great thank you.
[code]
#include
#include
using namespace std;
class Item
{
public:
int a;

};
class LinkList
{

LinkList *NextItem;
static LinkList *head;
public:
LinkList* GetNextItem();
LinkList* FindTail();
LinkList(int value);
static LinkList* GetHead();
void AddNode(int value);
void DeleteNode(int value);
LinkList* Previous(LinkList* FinePrevious);
LinkList* FindNode(int value);
Item data;



};

LinkList* LinkList::FindNode(int value)
{

LinkList *temp = head;
while(temp->data.a!=value&&temp->GetNextItem()!=0)
{

temp = temp->GetNextItem();
}

if(temp->data.a==value)
return temp;
else
return NULL;
}
LinkList* LinkList::GetNextItem()
{
return this->NextItem;
}
LinkList* LinkList::GetHead()
{
return head;
}
LinkList* LinkList::Previous(LinkList* FinePrevious)
{

LinkList *temp = head;
LinkList *temp1 = head;
if(FinePrevious==head)
{
return NULL;
}

while(temp->NextItem != FinePrevious)
{
temp1 = temp->NextItem;
temp = temp1;
}
return temp;
}

LinkList* LinkList::head = NULL;

LinkList::LinkList(int value)
{
data.a = value;
if(head==NULL)
head = this;
NextItem = 0;
}

void LinkList::AddNode(int value)
{
LinkList *newnode;
newnode = FindTail();
newnode->NextItem = new LinkList(value);


}

LinkList* LinkList::FindTail()
{
LinkList *temp = head;
LinkList *temp1 = head;
while(temp->NextItem != 0)
{
temp1 = temp->NextItem;
temp = temp1;
}
return temp;

}

void LinkList::DeleteNode(int value)
{
LinkList *todelete = FindNode(value);
if(todelete==head)
{
LinkList *temp1 = todelete->GetNextItem();
delete todelete;
head = temp1;
return;
}
LinkList *temp = Previous(todelete);
if(todelete->NextItem==0)
temp->NextItem = 0;
else
temp->NextItem = todelete->NextItem;
delete todelete;


}

void main()
{

LinkList *hey = new LinkList(10);
hey->AddNode(20);
hey->AddNode(50);
LinkList* temp = hey;
LinkList* temp1;

while(temp!=0)
{
cout << temp->data.a << " ";
temp1 = temp->GetNextItem();
temp = temp1;
}
cout << endl;


temp->DeleteNode(50);
temp = LinkList::GetHead();

while(temp!=0)
{
cout << temp->data.a << " ";
temp1 = temp->GetNextItem();
temp = temp1;
}
cout << endl;
system("PAUSE");


}
[/code]

Comments

Sign In or Register to comment.