Split from - http://www.daniweb.com/forums/thread255859.html
I think I may be in the same class as rwill357 and I am having some problems getting my code correct too. I appreciate the help that you have given so far. I have made the changes that you suggested. I'm not sure where I'm going wrong now though. I am able to get my code to compile but it freezes up after I enter the ints I want to populate my list. Could you take a look and provide a little feedback?
#include<iostream>
#include <assert.h>
#include<stdio.h>
#include<conio.h>
using namespace std;
class IntSLLNode {
public:
int info;
IntSLLNode *next;
IntSLLNode() {next = 0; }
IntSLLNode *tmp;
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
};
class IntSLList {
public:
IntSLList() {
head = tail = 0;
}
void addToHead ( int el){
IntSLLNode *tmp = new IntSLLNode(el);
if (head == NULL)
head = tail = tmp;
else
tmp->next = head;
head = tmp;
}
void addToTail(int el) {
if (tail !=0)
{
tail->next = new IntSLLNode(el);
tail = tail ->next;
}
else
head = tail = new IntSLLNode (el);
}
int deleteFromHead(){
assert(head!=0);
int el = head -> info;
IntSLLNode *tmp = head;
if (head == tail)
head = tail = 0;
else
head = head -> next;
delete tmp;
return el;
}
int deleteFromTail (){
int el = 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 el;
}
int deleteIthNode(int el)
{
IntSLLNode *tmp, *prev;
int i;
int counter = 0;
if(head == NULL)
{
cout<<"\nEmpty List\n";
}
else
{
int counter = 1;
tmp=head->next;
prev= head;
while (tmp!= tail && counter < i)
prev = tmp;
tmp= tmp->next;
}
if (counter != i)
{
cout << "list doesn't contain that many nodes";
}
else if (i= 1)
{
deleteFromHead();
}
else if (tmp= tail)
{
deleteFromTail();
}
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;
}
}
return 0;
}
void printALL()
{
IntSLLNode *tmp;
if(head == NULL)
cout <<"\nEmpty List\n";
else {
for (tmp = head; tmp->next != tail; tmp = tmp->next);
cout << tmp << " ";
}
cout << "\n";
}
private:
IntSLLNode *head, *tail;
};
int main() {
int n = 0;
int i= 0;
IntSLList list1;
/*write the codes to add some nodes by using addtoTail function*/
cout << "Please enter a number you would like to add to the list.\nAfter each number press enter and when done enter n.\n";
while (n != 'n')
{
cin >> n;
list1.addToTail(n);
}
list1.printALL();
cout <<"\nWhat is the position of the node that you would like to delete? ";
cin >> i;
int err = list1.deleteIthNode(i);
cout<<"\ndeleting "<<endl;
list1.printALL();
return 0;
system ("PAUSE");
return 0;
}