Hello. I'm having a hard time finding which block of code in this program is "insert in the middle" for Link List, so I can study it. Can someone point out where it is in the program and if insert head code below is included in insert middle. Much appreciate it!
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
typedef struct node
{
int data;
node *next;
}lnode;
void insert_head(int);
//void insert_tail(int);
bool found;
int num;
lnode *head = NULL, *current = NULL, *after = NULL, *before = NULL, *tail = NULL;
int main()
{
infile.open("intext.txt");
infile >> num;
while (!infile.eof( ))
{
found = false;
//before = head;
while (!found)
{
if (head == NULL)
{
cout <<"head is null" << endl;
head = new lnode;
head->data = num;
cout << head->data << endl;
head->next = NULL;
before = head;
found = true;
}
else if ((num <= before->data) && (!found))
{
cout<< "((num <= before->data) && (!found))" << endl;
insert_head (num);
found = true;
}
else if (((num > before->data) && (!found)) && (before->next == NULL))
{
cout << " (((num > before->data) && (!found)) && (before->next == NULL))" << endl;
current = new lnode;
current->data = num;
cout << current->data << endl;
before->next = current;
found = true;
}
else if ((num > before->data) && (!found))
{
cout << "((num > before->data) && (!found))" << endl;
after = before->next;
if ((num > before->data) && (num < after->data))
{
cout << "((num > before->data) && (num < after->data))" << endl;
current = new lnode;
current->data = num;
current->next = after;
before->next = current;
found = true;
}
else
{
before = after;
after = before->next;
}
}
else if (((num > after->data) && (after->next != NULL)) && (!found))
{
cout << "(((num > after->data) && (after->next != NULL)) && (!found))" << endl;
before = after;
after = before->next;
}
else if (((num > after->data) && (after->next == NULL)) && (!found))
{
cout << "(((num > after->data) && (after->next == NULL)) && (!found))"<< endl;
// insert_tail(num);
current = new lnode;
current->data = num;
current->next = NULL;
after->next = current;
found = true;
}
}
infile >> num;
}
//traverse the list
current = head;
while(current != NULL)
{
cout << current->data;
current = current->next;
}
infile.close();
system ("pause");
return 0;
}
void insert_head(int num)
{
lnode *temp = NULL;
cout << "inseerthead"<<endl;
temp = new lnode;
temp->data = num;
cout << temp->data << endl;
temp->next = head;
head = temp;
}
/*void insert_tail(int num)
{
lnode *temp = NULL, *new_current=NULL;
cout <<"inserttail"<<endl;
temp = new lnode;
temp->data = num;
cout << temp->data << endl;
temp->next = NULL;
new_current = head;
while (new_current->next != NULL)
{
new_current = new_current->next;
}
new_current->next = temp;
}
*/