nodes project need help

This is what I have so far, and here is the assignment:
Change the data portion so that each node contains the same game character(single name) and a integer value which represents that characters hierarchy (1 is best / higher number not good)
Modify the addTo function to build list 10 nodes with unique char names and unique numbers that are in order from best to worst (1-100000)
Then create a new function which will search the list correctly and add a new node in the correct position in the hierarchy. if the hierarchy number is the name as another node, replace it.

and modify the printAll function to show everything in the output screen

This has been compiled with Visual Studio 2008
header code
...........
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
#ifndef int_linked_list
#define int_linked_list
#include <iostream>

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

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
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include "intSLLst.h" //includes the header file into the program
using namespace std;


void main() //main to call the node creation,deletion, and print-all functions
{
	IntSLList test;
	test.addToHead(12); //added 12 nodes to the head
	test.addToTail(12);	//added 12 nodes to the head
	//test.deleteFromHead();
	//test.deleteFromTail(); // deletion of the head, tail and specific node
	test.deleteNode(0);
	test.isInList(12);
	test.printAll(); // prints all the nodes
	
	system("pause");
}

IntSLList::~IntSLList() {
    for (IntSLLNode *p; !isEmpty(); ) {		
        p = head->next;
        delete head;
        head = p;
    }
}

void IntSLList::addToHead(int el) {
    head = new IntSLLNode(el,head);		//adds a/or nodes to head
    if (tail == 0)
       tail = head;
}

void IntSLList::addToTail(int el) {
    if (tail != 0) {      // if list not empty;
         tail->next = new IntSLLNode(el); //adds a/or nodes to tail
         tail = tail->next;
    }
    else head = tail = new IntSLLNode(el);
}

int IntSLList::deleteFromHead() {	//deletion of nodes from head
    int el = head->info;
    IntSLLNode *tmp = head;
    if (head == tail)     // if only one node on the list;
         head = tail = 0;
    else head = head->next;
    delete tmp;
    return el;
}

int IntSLList::deleteFromTail() {		//deletion of nodes from tail
    int el = tail->info;
    if (head == tail) {   // if only one node on the list;
         delete head;
         head = tail = 0;
    }
    else {                // if more than one node in the list,
         IntSLLNode *tmp; // find the predecessor of tail;
         for (tmp = head; tmp->next != tail; tmp = tmp->next);
         delete tail;
         tail = tmp;      // the predecessor of tail becomes tail;
         tail->next = 0;
    }
    return el;
}

void IntSLList::deleteNode(int el) {
    if (head != 0)                     // if non-empty list;
         if (head == tail && el == head->info) { // if only one
              delete head;                       // node on the list;
              head = tail = 0;
         }
         else if (el == head->info) {  // if more than one node on the list
              IntSLLNode *tmp = head;
              head = head->next;
              delete tmp;              // and old head is deleted;
         }
         else {                        // if more than one node in the list
              IntSLLNode *pred, *tmp;
              for (pred = head, tmp = head->next; // and a non-head node
                   tmp != 0 && !(tmp->info == el);// is deleted;
                   pred = pred->next, tmp = tmp->next);
              if (tmp != 0) {
                   pred->next = tmp->next;
                   if (tmp == tail)
                      tail = pred;
                   delete tmp;
              }
         }
}

bool IntSLList::isInList(int el) const {
    IntSLLNode *tmp;
    for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
    return tmp != 0;
}

void IntSLList::printAll() const {		//print all function to print nodes
    for (IntSLLNode *tmp = head; tmp != 0; tmp = tmp->next)
        cout << tmp->info << " ";
	cout << endl;
	

}


I got everything to work thanks anyways.
Last edited on
Topic archived. No new replies allowed.