Hey all,
I've used this site quite a bit as a reference, and couldn't find a solution that worked for me. So I became a member. :)
Anyway, I can't get my insert function to...function when I go to add a node. The node is NOT to be just added to the beginning or end, but it must be inserted at the current location (there is a current pointer in there). The program as a whole is an introduction to Singly Linked Lists, so I can't use a "previous" pointer for anything. It is broken into three files, list.h, list.cpp, and main.cpp (They appear in that order). Any help with this would be much appreciated.
list.h
#ifndef LIST_H
#define LIST_H
#include <cstdlib>
#include <iostream>
class list
{
private:
struct node
//declaration of the nodes
{
int info;
//node's data contents
node *link;
//stores the address of the next
//node in the linked list
};
public:
list();
//constructor
~list();
//destructor
void insert(const int);
//Function to add a node after current
//node with the value provided
bool search();
//search the list for value provided
//return true if found, -1 if empty
void removenode();
//remove current node, return -1 if empty
void display();
//print out the contents of the list
list operator<<(const list);
//print all values of the list, 5 per line
protected:
node *current; //current element
node *head; //first element
node *last; //last element
};
list.cpp
#include <cstdlib>
#include <iostream>
#include "list.h"
using namespace std;
list::list()
{
head->info = 0;
head->link = 0;
}
list::~list()
{
node *temp;
while(head != 0)
{
temp = head;
head = head->link;
delete temp;
}
last = 0;
}
void list::display()
{
if (head->link==0)
{
cout<<"The list is empty.\n\n";
return;
}
int count;
node *temp;
temp->link=head->link;
while (temp->link != 0)
{
//cout << "\nNode " + count ": " + temp->info;
count++;
temp->link=temp->link->link;
}
}
/*************************
where the program currently hangs
***************************/
void list::insert(int newvalue)
{
node *newnode;
newnode -> info = newvalue;
if (head->link==0)
{
head->link=newnode;
}
else
{
newnode->link=current->link;
current->link=newnode;
}
current->link = newnode;
}
void list::removenode()
{
if (head->link==0)
{
cout<<"The list is empty.\n\n";
return;
}
node *temp;
temp->link=head->link;
while (temp->link != 0)
{
if(current->link==temp->link)
{
temp->link=current->link->link;
break;
}
temp->link=temp->link->link;
}
removenode();
current->link=temp;
}
bool list::search()
{
if (head->link==0)
{
cout<<"The list is empty.\n\n";
return false;
}
int searchval;
cout << "\nWhat value would you like to search for?\n";
cin >> searchval;
node *temp;
temp->link=head->link;
while (temp->link != 0)
{
if(temp->info==searchval)
{
current->link=temp;
return true;
}
temp->link=temp->link->link;
}
return false;
}
main.cpp
#include <cstdlib>
#include <iostream>
#include "list.h"
void menu();
//Prints out all of the menu options
using namespace std;
int main()
{
//data
int choice, value;
bool cont=true;
list mylist;
do
{
cout << "What would you like to do?";
menu();
cin >> choice;
switch (choice)
{
case 1:
cout << "\nWhat value would you like to enter?\n";
cin >> value;
mylist.insert(value);
break;
case 2:
//cout << "The node containing " + p->info + "was removed.";
mylist.removenode();
break;
case 3:
mylist.display();
break;
case 4:
mylist.search();
break;
case 5:
cont = false;
break;
default:
cout << "\nInvalid input, please try again.\n";
break;
}
}while(cont);
system("PAUSE");
return 0;
}
void menu()
{
cout << "\n(1) Insert Node";
cout << "\n(2) Remove Current Node";
cout << "\n(3) View List";
cout << "\n(4) Search List";
cout << "\n(5) Quit" << endl;
}