Sunday, 18 August 2013

C++ clueless about addToDLLHead( ) and deleteFromDLLHead( ) code

C++ clueless about addToDLLHead( ) and deleteFromDLLHead( ) code

I have been trying to figure out this C++ addtoDLLhead and
detelefromDLLhead for some reason i cant get it to work everything i have
tried comes back as an error is there someone who can show me what i need
to put. i have got to my wits end and need all the help i can get. here is
what i have so far. i just can not figure out what i need to put to get it
to work.
`enter code here`#include <iostream>
using namespace std;
template <class T>
class DLLNode {
public:
DLLNode()
{
next = prev = 0;
}
DLLNode(const T& el, DLLNode *n = 0, DLLNode *p = 0)
{
info = el; next = n; prev = p;
}
T info;
DLLNode *next, *prev;
};
template <class T>
class DLList {
public:
DLList() {
head=tail=0;
}
~DLList();
int isEmpty() {
return head==0;
}
void addToDLLHead(const T&);
void addToDLLTail(const T&);
T deleteFromDLLHead();
T deleteFromDLLTail();
void deleteDLLNode(const T&);
bool isInList(const T&) const;
void showList();
private:
DLLNode<T> *head, *tail;
};
template <class T>
DLList<T>::~DLList() {
for (DLLNode<T> *p; !isEmpty(); ) {
p=head->next;
delete head;
head=p;
}
}
template <class T>
void DLList<T>::addToDLLTail (const T& el) {
if (tail != 0) {
tail = new DLLNode<T>(el, 0, tail);
tail->prev->next = tail;
}
else head = tail = new DLLNode<T>(el);
}
template <class T>
T DLList<T>::deleteFromDLLTail() {
T el = tail->info;
if (head == tail) { // if only one node in the list;
delete head;
head = tail = 0;
}
else { // if more than one node in the list;
tail = tail->prev;
delete tail->next;
tail->next = 0;
}
return el;
}
template <class T>
void DLList<T>::deleteDLLNode(const T& el) {
if (head != 0)
if (head == tail && el == head->info) {
delete head;
head = tail = 0;
}
else if (el == head->info) {
head = head->next;
delete head->prev;
head->prev = 0;
}
else {
DLLNode<T> *tmp;
for (tmp=head->next;
tmp != 0 && tmp->info != el;
tmp = tmp->next);
if (tmp != 0) {
tmp->prev->next = tmp->next;
if (tmp == tail) tail = tmp->prev;
delete tmp;
}
}
}
template <class T>
void DLList<T>::addToDLLHead(const T& el) {
// Fill out addToDLLHead code here
}
template <class T>
T DLList<T>::deleteFromDLLHead() {
T el = head->info;
// Fill out deleteFromDLLHead code here
return el;
}
template <class T>
bool DLList<T>::isInList(const T& el) const {
DLLNode<T> *tmp;
for (tmp = head; tmp != 0 && tmp->info != el; tmp = tmp->next);
return tmp != 0;
}
template <class T>
void DLList<T>::showList() {
if (head == 0) cout << endl << "No node in the DLList";
else
for (DLLNode<T> *tmp = head; tmp != 0; tmp = tmp->next)
cout << tmp->info << " ";
cout << endl;
}
int main()
{
DLList<int> oList; // create an empty list object
oList.showList();
oList.addToDLLTail(5);
oList.addToDLLTail(8);
oList.addToDLLTail(3);
oList.showList(); // node order should be 5, 8, 3
oList.addToDLLHead(6);
oList.addToDLLHead(13);
oList.addToDLLHead(9);
oList.showList(); // node order should be 9, 13, 6, 5, 8, 3
oList.deleteFromDLLHead();
oList.showList();
oList.deleteFromDLLTail();
oList.showList();
oList.deleteDLLNode(5);
oList.showList(); // node order should be 13, 6, 8
system("PAUSE");
return 0;
}

No comments:

Post a Comment