Looking for a Simple Doubly Linked List Implementation
Ron Tarrant
rontarrant at gmail.com
Sat Sep 21 20:31:41 UTC 2019
Sorry. I posted the wrong file. This is the one that works:
```
import std.stdio;
import std.conv;
class TabList
{
private:
Tab _head;
int _lastUniqueID = 0;
string labelText;
this()
{
append();
}
void append()
{
string labelText = "Tab " ~ _lastUniqueID.to!string();
Tab current;
if(_head is null)
{
_head = new Tab(_lastUniqueID, labelText);
_head.setPrev(null);
_head.setNext(null);
}
else
{
current = _head;
//writeln("before the while loop");
//current.showThings();
while(current.getNext() !is null)
{
// writeln("in the while loop...");
// current.showThings();
// if(current.getPrev() !is null)
// {
// writeln("prev = ", current.getPrev().getTabID());
// }
// else
// {
// writeln("prev = null");
// }
current = current.getNext();
}
//writeln("out of the while loop\n");
Tab tab = new Tab(_lastUniqueID, labelText);
current.setNext(tab);
tab.setPrev(current);
}
_lastUniqueID++;
} // append()
Tab getHead()
{
return(_head);
} // getHead()
void removeTab(int uniqueID)
{
// get the head
Tab current = _head;
// walk the list to find the Tab with the uniqueID
while(current.getNext() !is null)
{
// if the uniqueID matches the head's ID
if(current.getTabID() is uniqueID)
{
// destroy the Tab object
current.destroy(uniqueID);
break;
}
// go to the next Tab
current = current.getNext();
}
} // removeTab()
} // class TabList
class Tab
{
private:
int _tabID;
string _label;
Tab _prev = null, _next = null;
public:
this(int uniqueID, string labelText)
{
_tabID = uniqueID;
_label = labelText;
} // this()
void showThings()
{
writeln("Tab = ", getTabID());
if(getPrev() !is null)
{
writeln("Tab.prev = ", getPrev().getTabID());
}
else
{
writeln("Tab.prev is null");
}
if(getNext() !is null)
{
writeln("Tab.next = ", getNext().getTabID());
}
else
{
writeln("Tab.next = null");
}
} // showThings()
void destroy(int id)
{
if(_tabID is id)
{
// destroy the TextView
// destroy the Label
_prev.setNext(_next);
_next.setPrev(_prev);
}
} // destroy()
Tab getNext()
{
return(_next);
} // getNext()
Tab getPrev()
{
return(_prev);
} // getPrev()
int getTabID()
{
return(_tabID);
} // getTabID()
void setNext(Tab tab)
{
_next = tab;
} // setNext()
void setPrev(Tab tab)
{
_prev = tab;
} // setPrev()
} // class Tab
void main(string[] args)
{
TabList tabList;
tabList = new TabList();
for(int i = 0; i < 7; i++)
{
// writeln("building Tab #", i);
tabList.append();
// writeln("------------------------------");
}
writeln();
Tab tab = tabList.getHead();
while(tab !is null)
{
tab.showThings();
tab = tab.getNext();
writeln("-----");
}
writeln("------------------------------");
// delete one
tabList.removeTab(3);
tab = tabList.getHead();
while(tab !is null)
{
tab.showThings();
tab = tab.getNext();
writeln("-----");
}
} // main()
```
More information about the Digitalmars-d-learn
mailing list