can anyone solve this please

Consider the following implementation of the node and doubly linked-list:

Extend the class doubly_linked_list by adding the following methods:

*Largest method .This method should return the largest element in a doubly linked-list.

*Delete method. This method should delete the first occurrence of an element (value) from a doubly linked-list.


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

template <class type>
class node
{
public:
	type info;
	node<type> * next;// next
	node<type> * prev;//back

};

template <class type>
class doubly_linked_list
{
	//data members
private:
	node<type> *head, *tail;
	int length;
public:
	doubly_linked_list()
	{
		head = tail = NULL;
		length = 0;
	}
	bool isEmpty()
	{   // return (head==NULL);
		if (head == NULL)
			return true;
		else
			return false;
	}

	void Append(type e)
	{
		node<type> *newnode = new node<type>;
		newnode->info = e;
		if (isEmpty())
		{
			newnode->next = NULL;
			newnode->prev = NULL;
			head = newnode;
			tail = newnode;
		}
		else
		{
			tail->next = newnode;
			newnode->prev = tail;
			newnode->next = NULL;
			tail = newnode;
		}
		++length;

	}

	void Display()
	{
		if (isEmpty()) { cout << "The linked list is empty !!!!"; return; }
		cout << "list elements: ";
		node<type> * current = head;
		while (current != NULL)
		{
			cout << current->info << " ";
			current = current->next;
		}
		cout << endl;

	}

	void ReverseDisplay()
	{
		if (isEmpty()) { cout << "The linked list is empty !!!!"; return; }
		cout << "Reverse list elements: ";
		node<type> * current = tail;
		while (current != NULL)
		{
			cout<< current->info<<" ";
			current = current->prev;
		}
		cout << endl;
	}

	void insert(type e, int index)
	{
		if (index< 1 || index>length + 1)
		{
			cout << "Invalid index !!!!"; return;
		}
		else
		{
			node<type> * newnode = new node <type>;
			newnode->info = e;
			if (index == 1)
			{
				newnode->prev = NULL;
				if (isEmpty())
				{
					newnode->next = NULL;
					head = tail = newnode;
				}
				else {
					newnode->next = head;
					head->prev = newnode;
					head = newnode;
				}



			}
			else
			{
				node<type> * current = head;
				int i = 1;
				while (i != index - 1)
				{
					current = current->next;
					++i;
				}

				if(current !=tail)
				{ 
				current->next->prev = newnode;
				newnode->next = current->next;
				current->next = newnode;
				newnode->prev = current;
				}
				else
				{
					current->next = newnode;
					newnode->prev = current;
					newnode->next = NULL;
					tail = newnode;
				}

			}





		}
	}

};
We shouldn't do your homework.
@OP
DLLists can be walked through in both directions head->tail, tail->head.

You get the maximum by 'walking through the list.
Select a suitable current_maximum value.
Go to the next node and if the value there is greater than that then replace the current_maximum.
Keep going through that to the end and the current-maximum is the value you want.


You delete a value by walking through the list, finding the relevant value. The first will be the one found by going head->tail.
When/if its found you have to disconnect it from the list taking into account whether the value is internal to the list or at the ends.

There are reams of tutorials on this eg https://www.geeksforgeeks.org/doubly-linked-list/
Where did the code for the original class come from? It's not good modern C++ code and can be improved!
her is the required functions for the above code:
but i cant have output



int getMaxNode(node *head){

/* Input Validation */

if(head == NULL){

printf("Error : Invalid Input !!!!\n");

return INT_MIN;

}

int max = head->data;

while(head != NULL){

if(head->data > max){

max = head->data;

}

head = head->next;

}

return max;

}

/* Function to delete a node in a Doubly Linked List.

head_ref --> pointer to head node pointer.

del --> pointer to node to be deleted. */

void deleteNode(node** head_ref, node* del)

{

/* base case */

if (*head_ref == NULL || del == NULL)

return;

/* If node to be deleted is head node */

if (*head_ref == del)

*head_ref = del->next;

/* Change next only if node to be deleted

is NOT the last node */

if (del->next != NULL)

del->next->prev = del->prev;

/* Change prev only if node to be deleted

is NOT the first node */

if (del->prev != NULL)

del->prev->next = del->next;

/* Finally, free the memory occupied by del*/

free(del);

}

/* function to delete all occurrences of the given

key 'x' */

void deleteFirstOccurOfX(struct Node** head_ref, int x)

{

/* if list is empty */

if ((*head_ref) == NULL)

return;

node* current = *head_ref;

node* next;

/* traverse the list up to the end */

while (current != NULL) {

/* if node found with the value 'x' */

if (current->data == x) {

/* save current's next node in the

pointer 'next' */

next = current->next;

/* delete the node pointed to by

'current' */

deleteNode(head_ref, current);

break;

}

/* else simply move to the next node */

else

current = current->next;

}

}
Please format code before posting, and use code tags as per your first post!
Topic archived. No new replies allowed.