C++ community
I need some help in writing a node program that follows the following instruction:
Write a main( ) function that adds and removes node objects.
- create an empty list object, such as oList
- add nodes with values of 5, 8, and 3
- display all nodes
- add nodes at the beginning with values of 6, 13, and 9
- display all nodes (node order should be 9, 13, 6, 5, 8, 3)
- delete the first node
- delete the last node
- delete a node with value 5
- display all nodes (node order should be 13, 6, 8)
Can someone show me how to start this off. I have the main program written and it looks like this
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include "Assignment5A.h"
#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;
void showList( ) {
IntSLLNode *p; // p is a temporary pointer to Node object
if (head == 0)
std::cout << "No Node in the list" << std::endl;
else {
for (p = head; p->next != 0; p = p->next)
std::cout << p->info << " ";
std::cout << p->info << std::endl;
}
};
IntSLList::~IntSLList() {
for (IntSLLNode *p; !isEmpty(); ) {
p = head->next;
delete head;
head = p;
}
}
void IntSLList::addToHead(int el) {
head = new IntSLLNode (el,head);
if (tail ==0)
tail = head;
}
void IntSLList::addToTail(int el) {
if (tail != 0) { //if list is not empty;
tail->next = new IntSLLNode(el);
tail = tail->next;
}
else head = tail = new IntSLLNode(el);
}
int IntSLList::deleteFromHead() {
int el = head->info;
IntSLLNode *tmp = head;
if (head == tail)
head = tail = 0;
else head = head->next;
delete tmp;
return el;
}
int IntSLList::deleteFromTail() {
int el = tail->info;
if (head == tail) { //if only one node in the list;
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 el;
}
void IntSLList::deleteNode(int el) {
if (head == tail && el == head->info) {
delete head;
head = tail = 0;
}
else if(el == head->info) {
IntSLLNode *tmp = head;
head = head->next;
delete tmp;
}
else {
IntSLLNode *pred, *tmp;
for (pred = head, tmp = head->next;
tmp != 0 && !(tmp->info == el);
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 *temp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
|