Question: 4. THE BASIC CODE OF DOUBLY LINED LIST IS GIVEN BELLOW IN TWO WAYS (WITH CLASS (2) AND WITHOUT CLASS (1)). YOU SHOULD WRITE THE
4. THE BASIC CODE OF DOUBLY LINED LIST IS GIVEN BELLOW IN TWO WAYS (WITH CLASS (2) AND WITHOUT CLASS (1)). YOU SHOULD WRITE THE MAIN FUNCTION AND THE ADDITIONAL FUNCTIONS NEEDED TO COMPLETE THE PROMLEM EITHER AT CODE 1 OR CODE 2. [28 points] Suppose there is a doubly linked list, which contains odd number of elements. You have to write a Boolean function to check if the elements of the linked list are palindrome. If the function returns Ture delete the middle element of the doubly linked list. Example: Suppose the doubly linked list is: Head: 1<->2<->3<->2<->1: Tail
As the elements of the doubly linked list are palindrome it will return true. Then the middle element of the doubly linked list 3 should be deleted. The list should look like: Head: 1<->2<->2<->1: Tail 1) struct node{ int data; node* prev = NULL; node* next = NULL; };
void add(node * &h, node *& t, int data){ node *newNode = new node; newNode->data = data; if(h == NULL){ h= newNode; t= newNode; } else { node* current = h; while(current->next!=NULL){ current = current->next; } current->next = newNode; newNode->prev = current; t= newNode; }
int main(){ node*head = NULL, *tail = NULL; int n = 5; for(int i = 0; i<=n; ++i){
add(head,tail,rand()%10); } return 0;}
2) struct node { int data; node* next; node* prev; }; class linked_list { private: node* head= NULL; node* tail= NULL; public: void add_node(int n) { node* tmp = new node; tmp->data = n; tmp->next = NULL; tmp->prev = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tmp->prev=tail; tail = tail->next; // tail=tmp; } } }; int main() { linked_list a;
for int (int i = 0; i < 5; i++) { int newData = rand()%100; a.add_node(newData); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
