problem with Delete nodes From Doubly linked List

i Had wrote a program that deletes a node from a Doubly liked list.
it dose not work. please help

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
 #include<iostream>
using namespace std;
struct node{
	int data;
	node *next;
	node *prev;
};

class dl{
public:
	dl();
	void addnode(int x);
	void delnode(int x);
	bool search(int x);
	void traverse();
	int total();

private:
	node* head;
	node *tail;


};


int dl::total()
{
	int c=0;
	if(head==NULL)
		return 0;
	else
	{
		for(node *cur=head;cur!=NULL;cur=cur->next)
			c++;
	}
	return c;
}

void dl::traverse()
{
	for(node *cur=head;cur!=NULL;cur=cur->next)
		cout<<cur->data<<" ";

	
}

bool dl::search(int x)
{
	node *cur=head;
	bool found=false;
	while(cur!=NULL&&!found)
		{if(cur->data==x)
		found=true;
		else
			cur=cur->next;
	}
	return found;
}
	
void dl::delnode(int x)
{
	if(head==NULL)
	{
		cout<<"Empty Doubly Linked List"<<endl;
		return;
	}
	else
	{
	if(search(x))
	{
		node *ptr=head,*cur=head;
		for(cur=head;cur->data!=x;cur=cur->next)
			ptr=ptr->next;
		
		cur=ptr;
		cur->next=ptr->next;
		cur->prev=ptr->prev;
		delete ptr;
		

		
	}
			
	
	else{
		cout<<"the element is not  exist"<<endl;
		return ;
	}
}
}

 void dl::addnode(int x)
{
	node *ptr;
	ptr=new node;
	ptr->data=x;
	ptr->prev=NULL;
	ptr->next=head;
	if(head==NULL)
		tail=ptr;
	else
		(head)->prev=ptr;
		head=ptr;


 }

dl ::dl()
{
	head=tail=NULL;
}
int main()
{
	dl y;
	int z;
	int x;
	do{
		system("cls");
		
		cout<<"1. Insert a node\n"
			<<"2. Delete a particular node\n"
			<<"3. Search for a particular item\n"
			<<"4. Traverse the list in reverse order\n"
			<<"5. Find the total nodes of the list\n"
			<<"6. Exit\n"
			<<"please enter your choice :";
		cin>>x;
		cout<<endl;
		switch(x)
		{
		case 1:{
			
			cout<<"please enter an element to insert :";
			cin>>z;
			y.addnode(z);}break;
		case 2:{
			cout<<"please enter an element to delete :";
			cin>>z;
			y.delnode(z);
			   }break;
		case 3:
			{
				cout<<"please enter a element to search : ";
				cin>>z;
				if(y.search(z))
					cout<<"the element is in the Doubly linked list"<<endl;
				else
					cout<<"the element is not exist"<<endl;
			}break;
		case 4:
			{
				y.traverse();
			}break;
		case 5:{
			cout<<"The number of nodes is "<<y.total()<<endl;
			   }break;
		case 6:{
			cout<<"bye bye"<<endl;
		}break;
		
			}
		cout<<endl;
		system("pause");
	}while(x!=6);
	return 0;
}



		
			



	
There are some optimisations you should consider:
1
2
3
4
5
6
7
// If you perform a search, you expect to get the results back!
node*  dl::search(int x)
{
    // Traverses list and compares data to x. 
    // returns pointer to node with matching data, 
    // or NULL pointer if search failed
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void  dl::delnode(int x)
{
    // ...
    // Do not reimplement search inside delete!
    node* toBeDeleted = this.search(x);
    
    if (toBeDeleted != NULL )
    {
        // Unplug toBeDeleted from the chain and kill it.
        
        // Update neighbouring nodes contents, and not just 
        // pointers to neighbouring nodes alone
        toBeDeleted->prev->next = toBeDeleted->next;
        toBeDeleted->next->prev = toBeDeleted->prev;
        
        // What you did (lines 76-77):
        // toBeDeleted->prev = toBeDeleted->next;
        // toBeDeleted->next = toBeDeleted->prev;

        delete toBeDeleted;
    }
}

You should also consider what delete should accept as an argument - it probably should not be an int (node data), because many nodes could have the same data stored. It should accept actual pointer to the node that needs deleting:
1
2
3
4
5
// Declared like this
void delnode(node* toBeDeleted) {/* ... */ };

// Called like this:
delnode( search(x) );


Last edited on
Topic archived. No new replies allowed.