0

I did a "single linked list" header & Implementation & Main files & everything worked very good. But now i must Implementation for the "doubly linked list" & i just need to know what i should add to my functions so they will work good, should i change all the functions Implementation ???

i have these function,
addToHead(int)
addToTail(int)
deleteFromHead()
deleteFromTail()
deleteNode(int)
isInList(int)
printForward()
printBackward()
clear()

anyone can help, plz ??? :!:

1
Contributor
1
Reply
2
Views
12 Years
Discussion Span
Last Post by kyakobi84
0

This is what I wrote:

doublyLinkedList::doublyLinkedList()
{
	head = tail = 0;
}



doublyLinkedList::~doublyLinkedList()
{
	node *temp;

	while (head != 0)
	{
		head->prev = 0;
		temp = head->next;
		delete head;
		head = temp;
	}
}



void doublyLinkedList::addToHead(int item)
{
	node *temp;
	temp = new node;

	temp->info = item;
	temp->next = head;
	temp->prev = 0;

	head = temp;

	if (tail == 0)
		tail = head;
}



void doublyLinkedList::addToTail(int item)
{
	node *temp;
	temp = new node;

	temp->info = item;
	temp->prev = tail;
	temp->next = 0;

	tail = temp;

	if (head == 0)
		head = tail;
}



void doublyLinkedList::deleteFromHead()
{
	if(head != 0)
	{
		int item = head->info;
		node *temp = head;
	
		if(head == tail)
			head = tail = 0;
		else
			head = head->next;
			

		delete temp;
		cout << ">> " << item << " has been deleted from head";
	}
	else
		cout << "** The list is empty";
}



void doublyLinkedList::deleteFromTail()
{
	if(tail != 0)
	{
		int item = tail->info;
		node *temp = tail;
	
		if(tail == head)
			tail = head = 0;
		else
			tail = tail->prev;
			

		delete temp;
		cout << ">> " << item << " has been deleted from tail";
	}
	else
		cout << "** The list is empty";
}



void doublyLinkedList::deleteNode(int item)
{
	if(head != 0)
	{
		if(item == head->info)
		{
			cout <<"\n";
			deleteFromHead();
		}

		else
		{
			node *temp = head->next, *pred = head;

			while(temp != 0 && !(temp->info == item))
				pred = pred->next, temp = temp->next;
			
			if(temp != 0)
			{	
				pred->next = temp->next;
				
				if(temp == tail)
					tail = pred;
				
				delete temp;

				cout << "\n>> " << item << " has been deleted";			
			}
			else
				cout << "\n>> " << item << " is not in the list";
		}

	}
	else
		cout << "\n** The list is empty";

}



bool doublyLinkedList::isInList(int item)
{
	node *temp;
	temp = head;

	while (temp != 0)
	{
		if(temp->info == item)
			return true;
			temp = temp->next;
	}

	return false;

}



void doublyLinkedList::printForward() const
{
	node *temp = head;

	cout << "{HEAD}--->   | ";
	while(temp != 0)
	{
		cout << temp->info << " | ";
		temp = temp->next;
	}
	cout << "  <---{TAIL}";

}



void doublyLinkedList::printBackward() const
{
	node *temp = tail;

	cout << "{HEAD}--->   | ";
	while(temp != 0)
	{
		cout << temp->info << " | ";
		temp = temp->prev;
	}
	cout << "  <---{TAIL}";
}



void doublyLinkedList::clear()
{
	while(head != 0)
	{
		cout << "\n";
		deleteFromHead();
	}
	cout << "\n";
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.