I am almost finished with this but am in need of knowing how to write the main.
here is what i have so far and what I need to complete the thing.
create an empty list object, such as oList,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 and last node, delete a node with value 5, display all nodes (node order should be 13, 6, 8)
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST
class IntSLLNode {
public:
IntSLLNode() {
next = 0;
}
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; 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(); // delete the head and return its info;
int deleteFromTail(); // delete the tail and return its info;
void deleteNode(int);
bool isInList(int) const;
private:
IntSLLNode *head, *tail;
};
#endif
//************************ intSLList.cpp **************************
#include <iostream.h>
#include "intSLList.h"
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 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) // if only one node in the list;
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 { // 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 nonempty list;
if (head == tail && el == head->info) { // if only one
delete head; // node in the list;
head = tail = 0;
}
elseif (el == head->info) {// if more than one node in 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 nonhead 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;
}
}
}
void showList( ) {IntSLLNode *p; // p is a temporary pointer to Node object
if (head = = 0)
cout << "No Node in the list" << endl;
else {
for (p = head; p->next != 0; p = p->next)
cout << p->info << " ";
cout << p->info << endl;
}
}
bool IntSLList::isInList(int el) const {
IntSLLNode *tmp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
int main()