I can seen to fix my linked list, i tried looking into other code for help, but most of the linked list examples online don't even work and are out dated:

#include <iostream>

using namespace std;

class list
{
private:
	struct node
	{
		int data;
		node *next;
	}*head, *tail;

public:
	list() : head(NULL), tail(NULL) {}
	~list();
	void push_front(int &d);
	void push_back(int &d);
	bool pop_front(int &d);
	bool pop_back(int &d);
	bool isEmpty() const;
	void print() const;
};

list::~list()
{
	if(!isEmpty())
	{
		cout << "Destroying nodes..." << endl;
		node *temp = head;
		node *temp2;

		while(temp!= NULL)
		{
			temp2 = temp;
			cout << temp2->data << endl;
			temp = temp->next;
			delete temp2;
		}
	}

	cout << "All nodes destroyed!" << endl;
}

void list::push_front(int &d)
{
	node *temp = new node;

	if(isEmpty())
	{
		temp->data = d;
		head = tail = temp;
		return;
	}

	temp->data = d;
	temp->next = head;
	head = temp;
}

void list::push_back(int &d)
{
	node *temp = new node;

	if(isEmpty())
	{
		temp->data = d;
		head = tail = temp;
		return;
	}

	temp->data = d;
	tail->next = temp;
	tail = temp;
}

bool list::pop_front(int &d)
{
	if(isEmpty())
		return false;
	else
	{
		node *temp = head;
		if(head == tail)
			head = tail = NULL;
		else
			head = head->next;

		d = temp->data;
		delete temp;

		return true;
	}

}

bool list::pop_back(int &d)
{
	if(isEmpty())
		return false;
	else
	{
		node *temp = tail;

		if(head == tail)
			head = tail = NULL;
		else
		{
			node *current = head;

			while(current->next != tail)
				current = current->next;

			tail = current;
			current->next = NULL;
		}

		d = temp->data;
		delete temp;

		return true;
	}
}

void list::print() const
{
	if(isEmpty())
	{
		cout << "The list is Empty" << endl << endl;
		return;
	}

	node *temp = head;
	cout << "the list is: ";

	while(temp != NULL)
	{
		cout << temp->data << ' ';
		temp = temp->next;
	}

	cout << endl << endl;
}

bool list::isEmpty() const
{
	return head == NULL;
}

int main()
{
	list ll;
	int choice;
	int num;

	do
	{
		cout << "1. insert from front." << endl;
		cout << "2. insert from back." << endl;
		cout << "3. delete from front." << endl;
		cout << "4. delete from back." << endl;
		cout << "5. Quit." << endl << endl;
		cout << ">> ";
		cin >> choice;
		switch(choice)
		{
		case 1:
			cout << "enter number: ";
			cin >> num;
			ll.push_front(num);
			ll.print();
			break;

		case 2:
			cout << "enter number: ";
			cin >> num;
			ll.push_front(num);
			ll.print();
			break;

		case 3:
			ll.pop_front(num);
			ll.print();
			break;

		case 4:
			ll.pop_back(num);
			ll.print();
			break;

		case 5:
			break;
		}

	}while(choice != 5);

	return 0;
}

Recommended Answers

All 2 Replies

Exactly what part of the code are you having problems with?

FYI: the functionality you've declared for your list is more that of a double ended queue, or dequeue. Lists generally allow for random insertion of new nodes and don't restrict activity to both ends.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.