Mar 1, 2017 at 9:34am Mar 1, 2017 at 9:34am UTC
Node is not deleting
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
#include<iostream>
using namespace std;
struct dellist
{
dellist *next;
dellist *add;
dellist *head;
dellist *last;
dellist *del1;
int data;
void insert(int );
void display();
void del(int );
dellist()
{
add = NULL;
del1 = NULL;
}
};
void dellist::insert(int item)
{
if (add == NULL)
{
add = new dellist;
add->next = NULL;
add->data = item;
head = add;
del1 = add;
last = add;
}
else
{
add = new dellist;
add->next = NULL;
add->data = item;
last->next = add;
last = add;
}
}
void dellist::display()
{
if (add == NULL)
{
cout << "\nList is Empty\n" ;
}
else
{
dellist *temp = head;
while (temp != NULL)
{
cout << temp->data << " , " ;
temp = temp->next;
}
}
}
void dellist::del(int del)
{
dellist *deltemp = head;
bool check = false ;
if (del1 == NULL)
cout << "\nList is Empty\n" ;
else
{
if (del1 == head)
{
if (del1->data == del)
{
delete del1;
del1 = del1->next;
head = del1;
}
else
{
while (del1 != NULL)
{
if (del1->data == del)
{
deltemp->next = del1;
delete del1;
check = true ;
break ;
}
else
{
deltemp = del1;
del1 = del1->next;
}
}
}
}
}
if (check == false )
cout << "\nData Not Found in The List\n" ;
else
cout << "\nData is Seccussfully Deleted\n" ;
}
int main()
{
dellist dl;
int num = 0;
while (true )
{
cout << "\n1-Add Data\n" ;
cout << "\n2-Display Data\n" ;
cout << "\n3-Delete Data\n" ;
cout << "\n4-Quit\n" ;
cout << "\nEnter The Choice : " ;
cin >> num;
if (num == 1)
{
cout << "\nEnter The Number : " ;
cin >> num;
dl.insert(num);
}
else if (num == 2)
dl.display();
else if (num == 3)
{
cout << "\nEnter The Number You Want To Delete : " ;
cin >> num;
dl.del(num);
}
else if (num == 4)
{
cout << "\nCome Again\n" ;
break ;
}
else
{
cout << "\nWrong Input\n" ;
}
system("pause" );
system("cls" );
}
system("pause" );
}
Last edited on Mar 1, 2017 at 9:47am Mar 1, 2017 at 9:47am UTC
Mar 1, 2017 at 11:51am Mar 1, 2017 at 11:51am UTC
Your code could use some comments. It's not immediately clear what the purpose of some of your data members is, e.g. add , del1 . The easier you make it for us to understand your code, the more likely people are to take the time and effort to look at it.
Mar 1, 2017 at 12:08pm Mar 1, 2017 at 12:08pm UTC
I would suggest to do this without add
and del1
. The more variables are involved the more problems they may cause.
Line 72: del1
is deleted and then used afterward.
Line 83: del1
is deleted but used as next the line above. I would guess you mean deltemp->next = del1->next ;
Mar 1, 2017 at 12:21pm Mar 1, 2017 at 12:21pm UTC
thnx buddy now its working
Last edited on Mar 1, 2017 at 12:21pm Mar 1, 2017 at 12:21pm UTC
Mar 1, 2017 at 12:28pm Mar 1, 2017 at 12:28pm UTC
can you explain this deltemp->next = del1->next;
Last edited on Mar 1, 2017 at 12:28pm Mar 1, 2017 at 12:28pm UTC