Singly Linked List Problem

I was assigned a project in which our professor gave us this code for a class which creates a singly linked list but I am having trouble using this code in my main function to actually create a list or insert any nodes onto the list. Thanks in advance for any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST

class IntSLLNode
{
public:
	IntSLLNode()
	{
		next=0;
	}
	IntSLLNode(int e1, IntSLLNode *ptr=0)
	{
		info=e1;
		next=ptr;
	}
	int info;
	IntSLLNode *next;
};

class IntSLList
{
public:
	IntSLList()
	{
		head=tail=0;
	}
	~IntSLList();
	int isEmpty()
	{
		return head==0;
	}
	void addToHead(int);
	void addToTail(int);
	int deleteFromHead();
	int deleteFromTail();
	void deleteNode(int);
	bool isInList(int) const;
private:
	IntSLLNode *head, *tail;

};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include "intSLList.h"
using namespace std;

IntSLList::~IntSLList()
{
	for(IntSLLNode *p;!isEmpty();)
	{
		p=head->next;
		delete head;
		head=p;
	}
}
void IntSLList::addToHead(int e1)
{
	head=new IntSLLNode(e1,head);
	if(tail==0)
		tail=head;
}
void IntSLList::addToTail(int e1)
{
	if(tail !=0)
	{
		tail->next=new IntSLLNode(e1);
		tail=tail->next;
	}
	else 
		head=tail=new IntSLLNode(e1);
}
int IntSLList::deleteFromHead()
{
	int e1=head->info;
	IntSLLNode *tmp=head;
	if (head==tail)
		head=tail=0;
	else head=head->next;
	delete tmp;
	return e1;
}
int IntSLList::deleteFromTail()
{
	int e1=tail->info;
	if (head==tail)
	{
		delete head;
		head=tail=0;
	}
	else
	{
		IntSLLNode *tmp;
		for(tmp=head;tmp->next !=tail;tmp=tmp->next);
		delete tail;
		tail=tmp;
		tail ->next=0;
	}
	return e1;
}
void IntSLList::deleteNode(int e1)
{
	if(head!=0)
		if(head==tail && e1==head->info)
		{
			delete head;
			head=tail=0;
		}
		else if(e1==head->info)
		{
			IntSLLNode *tmp=head;
			head=head->next;
			delete tmp;
		}
		else
		{
			IntSLLNode *pred, *tmp;
			for(pred=head;tmp=head->next;tmp!=0 && !(tmp->info==e1));
			{
				pred=pred->next, tmp=tmp->next;
			}
			if(tmp!=0)
			{
				pred->next = tmp->next;
				if(tmp==tail)
					tail=pred;
				delete tmp;
			}
		}
}
bool IntSLList::isInList(int e1) const 
{
	IntSLLNode *tmp;
	for(tmp=head;tmp!=0 && !(tmp->info==e1);tmp=tmp->next);
	return tmp!=0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "intSLList.h"
#include <iostream>
#include <list> 
using namespace std;

int main()
{
	IntSLList list1;
	int check=0;
	int a=0;
	while(check=0)
	{
		list1.addToHead(a);

		cout << "Would you like to continue? If yes type 0. If no type anything else." << endl;
		cin >> check;
	}

	system("pause");
	return 0;
}



What is your problem?
I am confused on how to actually create a list in the main function and how to use the functions in the class to do things with the class.
Assuming that the class' implementation is correct, from the looks of it, you'd use it as follows:

1
2
3
4
5
6
IntSLList myList;
myList.addToHead(1);
myList.addToHead(2);
myList.addToHead(3);
myList.deleteFromTail();
// etc. 
Topic archived. No new replies allowed.