Everything in my code works except for the part when I try to find a number in the list. If I try to find a number that's not there when there's something in the list, the code works but it crashes and I'm not sure why. Please help! Thank you.
#include<iostream>
#include<cstdlib>
#include<conio.h>
usingnamespace std;
struct Node
{
Node *previous;
int number;
Node *next;
};
int main()
{
Node *first = NULL;
Node *last = NULL;
Node *tempPrevious;
Node *tempNext;
Node *N;
int inList;
int option, temp;
inList = 0;
do
{
system("cls");
cout << "Enter one of the following options: " << endl;
cout << "\n\t1 - Display numbers in list ";
cout << "\n\t2 - Add number to the front of the list ";
cout << "\n\t3 - Add number to the end of the list ";
cout << "\n\t4 - Find number in the list ";
cout << "\n\t5 - Quit " << endl;
cout << "\n\tEnter you option: " << flush;
cin >> option;
switch(option)
{
case 1:
system("cls");
if(inList == 0)
{
cout << "List is empty. " << endl;
}
else
{
cout << "List contains: " << endl;
N = first;
while(N -> next != NULL)
{
cout << N -> number << ", ";
N = N -> next;
}
cout << N -> number;
cout << " Null - End of List";
}
cout << "\nPress any key to continue. ";
getch();
break;
case 2:
system("cls");
N = new Node;
inList += 1;
cout << "Enter number: " << flush;
cin >> temp;
N -> previous = NULL;
N -> number = temp;
N -> next = first;
first = N;
if(inList == 1)
{
last = N;
}
if(inList > 1)
{
N = N -> next;
N -> previous = first;
}
cout << "\nPress any key to continue. ";
getch();
break;
case 3:
system("cls");
N = new Node;
inList += 1;
if(inList == 1)
{
first = NULL;
}
cout << "Enter a Number: " << flush;
cin >> temp;
N -> previous = last;
N -> number = temp;
N -> next = NULL;
last = N;
if(first == NULL)
{
first = N;
N -> previous = NULL;
}
else
{
N = N -> previous;
N -> next = last;
}
cout << "\nPress any key to continue. ";
getch();
break;
case 4:
system("cls");
if(inList == 0)
{
cout << "List is empty. ";
}
else
{
cout << "Enter number: " << flush;
cin >> temp;
N = first;
{
if(N -> number == temp)
{
cout << "\n" << temp << " is in the list. " << endl;
break;
}
else
{
tempPrevious = N -> previous;
tempNext = N -> next;
N = tempNext;
}
if(i = inList && N -> number != temp)
{
cout << "\n" << temp << " not in list." << endl;
}
}
}
cout << "\nPress any key to continue. ";
getch();
break;
case 5:
system("cls");
cout << "Quitting... " << endl;
exit(0);
break;
}
}while(option != 5);
return 0;
}
This is the part that doesn't work:
1 2 3 4 5
if(i = inList && N -> number != temp)
{
cout << "\n" << temp << " not in list." << endl;
}
Here is a photo with what happens when a number isn't on the list, it finds it but the exe crashes: http://imgur.com/a/l0TYD