Hello Im quite Confused right now. I am trying to insert a Node in between nodes and it is not being displayed.
Please Help.
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
struct Node {
int DATA;
Node * Next;
};
Node * head = NULL;
void Display();
void Createnode(int data);
void Removenode(int data);
void Display()
{
Node *tmp;
tmp = head;
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return ;
}
if ( tmp->Next == NULL ) {
cout << tmp->DATA;
cout << "->";
cout << "NULL" << endl;
}else {
do {
cout << tmp->DATA;
cout << "Last Node";
tmp = tmp->Next;
} while ( tmp != NULL );
cout << "NULL" << endl;
}
}
void Createnode(int data)
{
Node *front, *tail, *temp;
tail = new Node;
tail->DATA = data;
tail->Next = NULL;
if(head == NULL)
{
head = tail;
//tail->Next=front->Next;
}
else
{
front = head;
temp=head;
while(front->Next!=NULL)
front=tail;
tail->Next=front->Next;
tail->Next=front;
front->Next=NULL;;
}
}
void Removenode(int data)
{
Node * curr, *prev;
curr=head;
prev=NULL;
while ( curr != NULL)
{
if (curr->DATA == data)
{
if (prev == NULL)
{
curr->Next= prev;
curr=curr->Next;
prev=NULL;
delete head;
}
else
{
prev->Next=curr->Next;
prev->Next=NULL;
//delete curr;
curr=prev->Next;
curr=NULL;
}
}
else
{
prev=curr;
curr=curr->Next;
}
}
}
int main() {
int n[100];
char choice;
for(int i=0; i<=50; i++)
{
start: cout << "\n[i]-Insert head Node, \n[d]- Delete head Node" << endl;
cout << "choice: ";
cin >> choice;
switch(choice){
case 'i':
case 'I':
do
{
cout<<"Enter a number to be inserted:";
cin>>n[i];
if(!isdigit(n[i]))
{
Createnode(n[i]);
cout << "\t\t\tList:";
i++;
Display();
}else{
cout << "Enter a Number please: " << endl;}
goto start;
}while(isdigit(n[i]));
break;
case 'd':
case 'D':
for(int x=1; x<10; x++){
Removenode(n[i]);
cout << "\t\t\tList:";
i--;
Display();
goto start;
}
break;
default:
cout << " Invalid Choice" << endl;
break;
}
}
system("pause");
return 0;
}