I get this error during execution.
You see I had a free(temp) before the
cout<<
statements.I removed them.I thought it was because of bad dereferencing turns out its something more to it.
This is my program:
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
struct node{
int data;
node* next;
};
node* head=NULL;
node* current=NULL;
void insert_node()
{
int num=0;
cout<<"\nEnter the value of the node to insert\n:";
cin>>num;
if(head==NULL)
{
head=(node*)malloc(sizeof(*head));
//current=(node*)malloc(sizeof(*current));
head->data=num;
head->next=NULL;
current=head;
cout<<"Created list\n";
}
else
{
node* temp=(node*)malloc(sizeof(*temp));
temp->data=num;
temp->next=NULL;
current->next=temp;
current=temp;
cout<<"Added element\n";
free(temp);
cout<<"dereferenced element\n";
}
}
void delete_node()
{
if(head!=NULL && head->next==NULL )//only one node
{
current=head=NULL;
cout<<"Deleted Head\n";
}
else if(head!=NULL && head->next!=NULL)//>= 2 nodes
{
node* temp;
//temp=NULL;
temp=head;
while(temp->next!=current)
{
temp=temp->next;
}
temp->next=NULL;
current=temp;
cout<<"Deleted last element\n";
// free(temp);
cout<<"Dereferenced temp\n";
}
else
{
cout<<"delete was not performed";
}
}
void list_linked_list()
{
node* temp=(node*)malloc(sizeof(* temp));
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"displayed list\n";
//free(temp);
cout<<"dereferenced temp";
}
void search_node()
{
cout<<"\nenter a number to search";
int search=0,found=0;
cin>>search;
node* temp=(node*)malloc(sizeof(* temp));
temp=head;
while(temp!=NULL)
{
if(temp->data==search)
found=1;
}
if(found==1)
cout<<"found\n";
else
cout<<"not found\n";
//free(temp);
cout<<"dereferenced temp";
}
void main()
{
int n=0;
k:
cout<<"Linked List operations: \n1. insert \n2. delete \n3. search\n 4. view List \n5. Exit";
cin>>n;
switch(n)
{
case 1: insert_node();break;
case 2: delete_node();break;
case 3: search_node();break;
case 4: list_linked_list();break;
case 5: exit(0);break;
default: cout<<" Please enter valid number between 1 and 5";
break;
}
goto k;
}
I dont think i misunderstood linked list concept.
Im pretty clear on it.I think there a mistake with the pointer.
Thank you.
EDIT: NEW CODE:
struct node{
int data;
struct node* next;
};
struct node* head=NULL;
struct node* current=NULL;
void insert_node()
{
int num=0;
cout<<"\nEnter the value of the node to insert\n:";
cin>>num;
if(head==NULL)
{
head->data=num;
head->next=NULL;
current=head;
cout<<"Created list\n";
}
else
{
struct node* temp=(node*)malloc(sizeof(node));
temp->data=num;
temp->next=NULL;
current->next=temp;
current=temp;
cout<<"Added element\n";
cout<<"dereferenced element\n";
}
}
void delete_node()
{
if(head!=NULL && head->next==NULL )//only one node
{
current=head=NULL; //Am I supposed to do anything else here??
cout<<"Deleted Head\n";
}
else
if(head!=NULL && head->next!=NULL)//>= 2 nodes
{
struct node* temp=(node*)malloc(sizeof(node));;
//temp=NULL;
temp=head;
while(temp->next!=current)
{
temp=temp->next;
}
temp->next=NULL;
current=temp;
cout<<"Deleted last element\n";
free(temp->next);
cout<<"Dereferenced temp\n";
}
else
{
cout<<"delete was not performed";
}
}
void list_linked_list()
{
node* temp=(node*)malloc(sizeof(node));
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"displayed list\n";
//free(temp); //should I free temp?
cout<<"dereferenced temp";
}
void search_node()
{
cout<<"\nenter a number to search";
int search=0,found=0;
cin>>search;
node* temp=(node*)malloc(sizeof(node));
temp=head;
while(temp!=NULL)
{
if(temp->data==search)
found=1;
else
temp=temp->next;
}
if(found==1)
cout<<"found\n";
else
cout<<"not found\n";
free(temp); //shoudl I free temp?
cout<<"dereferenced temp";
}
There are multiple problems in your code:
You are free()ing a node in your insert function which is not what you want. So remove the line free(temp) from your insert function.
You do want to free the node when you delete an element from your linked list. So uncomment the line: free(temp);. But this is not the correct current node that you want to free(). Here temp is your new current whereas you want to free() your old current which temp->next. So your free() statement should be: free(temp->next); in delete_node() function (Not free(temp); ).
Return value of main should an int.
If you are using C++ there are better ways to implement linked lists. You may want to use new and delete instead of malloc and free. Use C++ headers instead of C headers.
If you do use C, then don't cast the value returned by malloc in C.
You are using goto as a replacement for a loop which is unnecessary when you can simply use for(;;) { } or while(1) { }.
In insert function's else part you free new node just after adding it in Linked List that causes Undefined behaviour at runtime:
else
{
node* temp=(node*)malloc(sizeof(*temp));
temp->data=num;
temp->next=NULL;
current->next=temp;
current=temp;
cout<<"Added element\n";
free(temp); <------"Bug"
cout<<"dereferenced element\n";
}
Note: You can't access a node for which memory is deallocated (free()), doing this is an illegal operation. You should free memory for node when you are done with the program (and don't need access that memory again).
Related
Here I created a doubly circular linked list and try to add a node between the list but it is giving wrong output
I first created a doubly circular linked list and then displayed it and then I added a node between them and then again displayed it
#include<iostream>
using namespace std;
class node //node class
{
public:
int data;
node *next;
node *prev;
node(int a)
{
data=a;
next=nullptr;
prev=nullptr;
}
};
class linkedlist //linkedlist class
{
node *head,*tail;
public:
linkedlist()
{
head=nullptr;
tail=nullptr;
}
void addnode(int val) //creating node function
{
node *newnode;
newnode=new node(val);
if(head==0)
head=tail=newnode;
else
{
tail->next=newnode;
head->prev=newnode;
newnode->prev=tail;
newnode->next=head;
tail=newnode;
}
}
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
temp=temp->next;
cout<<" "<<temp->data;
}
void addin(int val,int pos) //addin function
{
node *newnode=new node(val);
node *temp=head;
for(int i=0;i<pos;i++)
{
temp=temp->next;
}
newnode->next=temp->next;
newnode->prev=temp;
temp->next->prev=newnode;
temp->next=newnode;
}
};
int main()
{
linkedlist l1;
int s,val,val1,pos;
cin>>s;
for(int i=0;i<s;i++)
{
cin>>val;
l1.addnode(val);
}
l1.disp(); //display function
cout<<"\n\n";
cin>>pos;
cin>>val1;
l1.addin(val,pos); //calling addin function
l1.disp();
}
Input
3
1 2 3
1
0
Expected output
1 2 3
1 2 0 3
Current output
1 2 3 1
I don't know what mistake I am doing and also I am beginner so any tip is also too helpful for me
You are printing the head node instead of the tail node due to the extra temp=temp->next; after the loop in the disp() function.
You are adding val instead of val1 after the loop in the main() function.
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
// remove this
// temp=temp->next;
cout<<" "<<temp->data;
}
cin>>pos;
cin>>val1;
// add val1, not val
//l1.addin(val,pos); //calling addin function
l1.addin(val1,pos); //calling addin function
l1.disp();
in main function
you mistakenly add val instead of val1
cin>>val1;
l1.addin(val1,pos);
then in disp() function, remove temp=temp->next which is outside the loop.
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
// temp=temp->next;
cout<<" "<<temp->data;
}
and finally in addin() function, change pos to pos-1
for(int i=0;i<pos-1;i++)
{
temp=temp->next;
}
that's all.
#MikeCAT was spot on, you are using val1 and passing val. However, I think the while loop in disp will skip the last element and not print it. and the logic in addin are not going to add the new element in the new place. Here is my version of your code
#include<iostream>
using namespace std;
class node //node class
{
public:
int data;
node *next;
node *prev;
node(int a)
{
data=a;
next=nullptr;
prev=nullptr;
}
};
class linkedlist //linkedlist class
{
node *head,*tail;
public:
linkedlist()
{
head=nullptr;
tail=nullptr;
}
void addnode(int val) //creating node function
{
node *newnode;
newnode = new node(val);
if(head == NULL)
head = tail = newnode;
else
{
tail->next = newnode;
head->prev=newnode;
newnode->prev=tail;
newnode->next=head;
tail=newnode;
}
}
void disp() //display function
{
node *temp = head;
if (temp == NULL)
{
cout << "List is empty " << endl;
return;
}
do
{
cout << temp->data << endl;
temp = temp->next;
} while (temp != head);
}
void addin(int val,int pos) //addin function
{
node *newnode=new node(val);
node *temp=head;
for(int i=0;i<pos;i++)
{
temp=temp->next;
}
//newnode->next=temp->next;
newnode->next=temp;
newnode->prev=temp->prev;
temp = temp->prev;
temp->next = newnode;
}
};
int main()
{
linkedlist l1;
int s,val,pos;
cin>>s;
cout << "val of size is " << s << endl;
for(int i=0;i<s;i++)
{
cin>>val;
l1.addnode(val);
}
l1.disp(); //display function
cout<<"\n\n";
cin>>pos;
cin>>val;
l1.addin(val,pos); //calling addin function
l1.disp();
}
I didn't understand why i am getting error in line head=NULL I am just pointing head towards NULL
I have also tried creating a new node for head and then pointing towards NULL but still it doesn't work
Code show here
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *next;
};
node *head=NULL;
node *tail=NULL;
void createnode(int value)
{
node *temp=new node;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;
}
}
void deletek(int n,int k)
{
if(head==NULL)
return;
node *temp;
temp=head;
while(temp->data<temp->next->data)
{
head=temp->next;
temp=temp->next;
k--;
}
while(temp!=NULL&&k!=0)
{
if(temp->next->data<temp->next->next->data)
{
temp->next=temp->next->next;
k--;
}
temp=temp->next;
}
}
void display()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main() {
int t;
cin>>t;
int n,k;
for(int i=0;i<t;i++)
{
cin>>n>>k;
int arr[n];
for(int j=0;j<n;j++)
{
cin>>arr[j];
createnode(arr[j]);
}
deletek(n,k);
display();
head=NULL;
}
}
I expect that whenever the line head=NULL runs the previous linked list should get delete and I should be able to enter elements in the linked list from the beginning
First of all: C++ likes "nullptr" (since C++11) for this, instead of "NULL".
=> e.g. https://en.cppreference.com/w/cpp/types/NULL vs. https://en.cppreference.com/w/cpp/language/nullptr
Second: The exact compiler output could help.
Third: The list will not be deleted if you throw away the address of it's location. maybe you should rather create a class and/or add destructor for your node. If you want to implement LL by yourself
Or have a look at std::list -> https://en.cppreference.com/w/cpp/container/list. I use this rather often.
Or if you like to add boost dependency: https://www.boost.org/doc/libs/1_35_0/doc/html/intrusive/list.html more fancy but you'll add an external library
Then: The Segfault (on my PC) happens in if(temp->next->datanext->next->data) I guess you can never know if temp->next->next->data exists... maybe not even if temp->next->data => if temp->next points to NULL (or nullptr) it is not a valid instance of node to dereference
try:
`
#include <bits/stdc++.h>
#define HERE() printf("HERE: %d\n", __LINE__)
using namespace std;
struct node
{
int data;
node *next;
};
node *head=NULL;
node *tail=NULL;
void createnode(int value)
{
node *temp=new node;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;
}
}
void deletek(int n,int k)
{
if(head==NULL)
return;
node *temp;
temp=head;
while(temp->data<temp->next->data)
{
head=temp->next;
temp=temp->next;
k--;
}
HERE();
while(temp!=NULL&&k!=0)
{
HERE();
int d1 = temp->next->data;
HERE();
int d2 = temp->next->next->data;
if(temp->next->data < temp->next->next->data)
{
temp->next=temp->next->next;
k--;
}
temp=temp->next;
}
}
void display()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main() {
int t;
cout << "input t:" << endl;
cin>>t;
int n,k;
for(int i=0;i<t;i++)
{
cout << "input n/k" << endl;
cin>>n>>k;
int arr[n];
for(int j=0;j<n;j++)
{
cout << "input arr[" << j <<"]" << endl;
cin>>arr[j];
createnode(arr[j]);
}
deletek(n,k);
display();
head=NULL;
}
}
`
Here's my C++ program which is used to insert values at the beginning of the linked list. The logic of the program seems to be fine for me but it is unable to display the values of the list. I guess the problem is in the Print() function. Please help!
#include<iostream.h>
struct Node
{
int data;
Node* next;
};
struct Node* head;
void Insert(int x)
{
Node *temp=new Node();
temp ->data=x;
temp ->next=NULL;
if(head!=NULL)
{
temp->next=head;
head=temp;
}
}
void Print()
{
Node *temp=head;
cout<<"List is:";
do
{
cout<<temp->data<<"->";
temp=temp->next;
}while(temp!=NULL);
cout<<endl;
}
int main()
{
int n,i,x;
head=NULL;
cout<<"How many numbers \n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the number \n";
cin>>x;
Insert(x);
Print();
}
return 0;
}
void Insert(int x)
{
Node *temp=new Node();
temp ->data=x;
temp ->next=NULL;
if(head!=NULL)
{
temp->next=head;
head=temp;
}
}
in main program head is null so in insert function it will never update because of if(head!=NULL) check.
Correct Solution is
#include<iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
struct Node* head;
void Insert(int x)
{
Node *temp=new Node();
temp ->data=x;
temp ->next=NULL;
if(temp!=NULL)
{
temp->next=head;
head=temp;
}
}
void Print()
{
Node *temp=head;
cout<<"List is:";
do
{
cout<<temp->data<<"->";
temp=temp->next;
}while(temp!=NULL);
cout<<endl;
}
int main()
{
int n,i,x;
head=NULL;
cout<<"How many numbers \n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the number \n";
cin>>x;
Insert(x);
}
Print();
return 0;
}
You need to update head which is never changed from initial NULL because of if(head!=NULL) condition checking.
Change
if(head!=NULL)
{
temp->next=head;
head=temp;
}
to
if(head!=NULL)
{
temp->next=head;
}
head=temp;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've been learning the implementation of single linked list in c++.
Thing is that I understood the concept behind single linked list but I am not able to guess where I have done mistake in the code.
Whenever I insert a new node, it takes the first position (i.e head) and the size of the list is always 1.
I tried to solve it but sometimes display function turns into infinite loop.
I am getting no clue on this.
Its been pricking my mind for a week.
All I can guess is, I am not referencing the addresses properly in the code.
Am I messing up with the pointers? Help by making me understand the mistake I have done.Thank you.
SLLCLAS.CPP
#include<iostream>
#include<conio.h>
class node{
public:
int data;
node *next;
};
class sll{
private:
node *head;
public:
ssl(){
head=NULL;
}
void display();
void insert(int,int);
};
void sll::display(){
if(head==NULL){
cout<<"List is Empty";
}else{
cout<<"\n";
for(node *t=head;t!=NULL;t=t->next){
cout<<t->data<<"->";
}
cout<<"\n";
}
void sll::insert(int position,int data){
node *temp=new node();
temp->data=data;
if(position<0){
cout<<"\nPosition not valid";
}else if(head==NULL || position==1){
temp->next=head;
head=temp;
}else{
node *p,*q;
q=head;
int count=1;
while(count<position && q!=NULL){
count++;
cout<<"\nCount:"<<count;
p=q;
q=q->next;
}
p->next=temp;
temp->next=q;
delete(temp);
}
int main(){
clrscr();
sll list;
int ch,val,pos;
do{
cout<<"\nSINGLY LINKED LIST\n1: Insert\n2: Delete\n3: Display\n Enter your choice:";
cin>>ch;
switch(ch){
case 1:
cout<<"\nEnter the position:";
cin>>pos;
cout<<"\nEnter the value:";
cin>>val;
list.insert(pos,val);
list.display();
break;
case 2:
break;
case 3:
list.display();
break;
default:
cout<<"\nWrong choice";
}
cout<<"\nDo you want to continue(1/0):";
cin>>ch;
}while(ch!=0);
getch();
}
EDIT:
I am running the code on Turbo C++ Version 3.0 on windows 8.1 64bit
using dosbox.
This is a fixed version of the function insert:
void sll::insert(int position, int data) {
node* temp = new node();
temp->data = data;
if (position <= 0) {
cout << "\nPosition not valid";
} else if (head == NULL || position == 1) {
temp->next = head;
head = temp; // ---------------------- problem here
} else {
node* p, *q;
q = head;
int count = 1;
while (count < position && q != NULL) {
count++;
cout << "\nCount:" << count;
p = q;
q = q->next;
}
p->next = temp;
temp->next = q;
}
//delete (temp); //<---------------- problem here
}
You create a node to insert in the list but at the end of the function deleted (with pointer referencing to this memory address), when accessing the memory deleted anything could happen.
The other problem would give you compiler error you are assigning a node variable to a node* variable, with what compiler you are testing.
The are other problems as #include <iostream.h>, iostream is a header from C++ that don't have extension (ex: #include <vector> or #include <iostream>).
The main function it's not standart, changed to int main() if the parameters are not used or int main(int argc, char* argv[]) if they are. You would need to return and integer from main function.
I don't have clrscr(); function defined, probably platform dependent (i am testing with MinGW GCC 4.9.0 with C++11 in Windows).
The statement
delete(temp);
is highly suspicious in the insert function.
Basically temp reference the new node you take care to create and setup. But you delete it before exiting the function. After the first insert, you are manipulating freed memory.
The statement
head=*temp;
make no sense. head is a node*, and you are assigning a node value. If it ever compiles, head is also containing garbage value.
#include <iostream>
using namespace std;
struct node
{
int info;
node *next;
};
class DS
{
private:
node *temp;
node *temp1;
node *head;
int key;
int x;
public:
DS()
{
head=temp=temp1=NULL;
}
void insert()
{
for (int i=0; i<1; i--)
{
cout<<"Enter 0 to stop and 1 to continue:";
cin>>x;
cout<<endl;
if (x==1)
{
if (head==NULL)
{
head=new node;
cout<<"Enter the value in head's Info";
cin>>head->info;
cout<<endl;
head->next=NULL;
}
cout<<"Enter the number after which you want to add node:";
cin>>key;
cout<<endl;
temp=head;
while (temp!=NULL)
{
if (temp->info==key)
{
temp1=new node;
cout<<"Enter value in new node:";
cin>>temp1->info;
cout<<endl;
temp1->next=temp->next;
temp->next=temp1;
}
temp=temp->next;
}
}
else break;
}
}
void Del()
{
temp=head;
if(head==NULL)
return;
cout<<"Enter the number after which you want to delete the node:";
cin>>key;
cout<<endl;
while( temp!=NULL)
{
if (temp->next->info==key)
{
temp1=temp->next;
temp->next=temp->next->next;
delete temp1;
break;
}
temp=temp->next;
}
}
void searching()
{
temp=head;
cout<<"Enter key to found node:";
cin>>key;
cout<<endl;
while (temp!=NULL)
{
if (temp->info==key)
{
cout<<"Node found"<<endl;
break;
}
temp=temp->next;
}
}
void printing()
{
temp=head;
while (temp!=NULL)
{
cout<<endl;
cout<<temp->info<<endl;
temp=temp->next;
}
}
void emptiness()
{
temp=head;
if (temp->next==NULL)
{
cout<<"Empty list";
cout<<endl;
}
else if (temp!=NULL)
cout<<"Not empty";
}
};
int main()
{
DS obj;
obj.insert();
cout<<endl;
obj.Del();
cout<<endl;
obj.searching();
cout<<endl;
obj.printing();
cout<<endl;
obj.emptiness();
return 0;
}
The delete from last functionality does not work properly. It shows that the node has been deleted but when I display it, it gets into an infinite loop and displays junk. Couldn't figure what's wrong!
Here's the code:
using namespace std;
class List
{
struct NODE
{
int item;
NODE *next;
};
NODE *Head,*Tail;
public:
List()
{
Head=NULL;
Tail=NULL;
}
~List()
{
while(Head->next!=NULL)
{
Delete_At_Head();
}
Delete_At_Head();
}
void Add_At_First(int);
void Add_At_Last(int);
void Delete_At_Head();
void Delete_At_Tail();
int Is_Empty();
void display();
};
void List::Add_At_First(int data)
{
NODE *temp;
temp=new NODE;
if(Head==NULL)
{
temp->item=data;
temp->next=NULL;
Head=temp;
Tail=Head;
}
else
{
temp->item=data;
temp->next=Head;
Head=temp;
}
cout<<"Node added at first!\n";
}
void List::Add_At_Last(int data)
{
NODE *temp;
temp=new NODE;
temp->item=data;
temp->next=NULL;
if(Head==NULL)
{
Head=temp;
Tail=temp;
}
else
{
Tail->next=temp;
Tail=temp;
}
cout<<"Node added at last!\n";
}
void List::Delete_At_Head()
{
NODE *temp;
temp=new NODE;
temp->item=Head->item;
temp->next=Head->next;
delete Head;
Head=temp->next;
delete temp;
cout<<"Node deleted from head!\n";
}
void List::Delete_At_Tail()//Problematic part
{
NODE *temp,*prev;
temp=new NODE;
prev=new NODE;
temp=Head;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
delete temp;
delete Tail;
Tail=prev;
delete prev;
cout<<"Node deleted from tail!\n";
}
int List::Is_Empty()
{
if(Head==NULL)
return 1;
else
return 0;
}
void List::display()//does not display after delete from tail
{
NODE *temp;
temp=new NODE;
temp->item=Head->item;
temp->next=Head->next;
do
{
cout<<temp->item<<"-->";
temp=temp->next;
}while(temp->next!=NULL);
cout<<temp->item;
}
int main()
{
List obj;
int ch,data;
do
{
cout<<"\n1.Display\n2.Add at first\n3.Add at last\n4.Delete at
head\n5.Delete at tail\n6.Exit\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
{
if(obj.Is_Empty())
cout<<"List is Empty!\n";
else
obj.display();
break;
}
case 2:
{
cout<<"Enter data: ";
cin>>data;
obj.Add_At_First(data);
break;
}
case 3:
{
cout<<"Enter data: ";
cin>>data;
obj.Add_At_Last(data);
break;
}
case 4:
{
if(obj.Is_Empty())
cout<<"List is Empty!\n";
else
obj.Delete_At_Head();
break;
}
case 5:
{
if(obj.Is_Empty())
cout<<"List is Empty!\n";
else
obj.Delete_At_Tail();
break;
}
case 6:
{
break;
}
}
}while(ch!=6);
return 0;
}
The following should correctly delete the last item in the list:
void List::Delete_At_Tail()
{
if(Head == NULL) return;
// If only one item in the list, delete it and empty the list...
if(Head->next == NULL) {
delete Head;
Head = NULL;
Tail = NULL;
return;
}
// Find the last item in the list
NODE *temp = Head;
while(temp->next!=Tail)
{
temp=temp->next;
}
delete Tail;
temp->next=NULL;
Tail=temp;
cout<<"Node deleted from tail!\n";
}
You are definitely deleting too many objects in your Delete_At_Tail() (I assume this is the function you meant when you wrote delete_from_tail() or "delete from last"; when programming precision is everything!) function: you want to get rid of one object but you are deleteing three objects, including the predecessor of the deleted object. You should only delete the object you are removing and no other. Other than that the function looks OK.
BTW, since you are maintaining a pointer to the Tail you can improve the way to find the predecessor: you can find the node whose next is Tail. This avoids the awkward need to keep two variables up to date in the loop looking for the predecessor.