Answer to Question #239038 in C++ for Rohit

Question #239038
Write C++ program that that contain functions
1) A function to Delete the last node of doubly Linked List
2)A function at deletes the 2nd-last node of the doubly Linked List
3)A function that displays the Linked lists
Call the functions in main

NOTE:
1) make class or struct
2)No global declarations
1
Expert's answer
2021-09-19T17:12:05-0400
#include <iostream>
using namespace std;


//Structure
struct List {
    int item; //To store data items in the linked list
    
   List* nextNode; //Next node to the current node
    List* prevNode; //Previous node to the current node
};
//Class Node 
class Node {
  private:
    List* headNode;
  public:
    Node(){
      headNode = NULL;
    }
 
    //Inserting a new element into the linked list at the end
    void push_item(int data) {
      List* new_node = new List();
      new_node->item = data;
      new_node->nextNode = NULL;
      new_node->prevNode = NULL; 
      if(headNode == NULL) {
        headNode = new_node;
      } else {
        List* temp = headNode;
        while(temp->nextNode != NULL)
          temp = temp->nextNode;
        temp->nextNode = new_node;
        new_node->prevNode = temp;
      }    
    }




//Deleting the last data of the doubly linked list
    void deleteLast() {
      if(headNode != NULL) {
        if(headNode->nextNode == NULL) {
          headNode = NULL;
        } else {
          List* temp = headNode;
          while(temp->nextNode->nextNode != NULL)
            temp = temp->nextNode;
          List* last_node = temp->nextNode;
          temp->nextNode = NULL;
          free(last_node); 
        }
      }
    }
    //A function for deleting the second last element of the doubly linked list 
    //This function takes total number of elements - 1 of the linked list 
    void deleteSecondLast(int pos) {     
      if(pos < 1){
        cout<<"\nList is empty";
      } else if (pos == 1 && headNode != NULL) { 
        List* secondLastNode = headNode;
        headNode = headNode->nextNode;
        free(secondLastNode);
        if(headNode != NULL)
          headNode->prevNode = NULL;
      } else {
        List* temp = headNode;
        for(int i = 1; i < pos-1; i++) {
          if(temp != NULL) {
            temp = temp->nextNode;
          }
        }
        if(temp != NULL && temp->nextNode != NULL) {
          List* secondLastNode = temp->nextNode;
          temp->nextNode = temp->nextNode->nextNode;
          if(temp->nextNode->nextNode != NULL)
            temp->nextNode->nextNode->prevNode = temp->nextNode;  
          free(secondLastNode); 
        } else {
          cout<<"\nNo element left.";
        }       
      }
    } 
 
    //Displaying the content of the linked list
    void displayList() {
      List* tempNode = headNode;
      if(tempNode != NULL) {
        cout<<"List has: ";
        while(tempNode != NULL) {
          cout<<tempNode->item<<" ";
          tempNode = tempNode->nextNode;
        }
        cout<<"\n";
      } else {
        cout<<"The list has no element.\n";
      }
    } 
	//
	//Return the total number of elements in the list
	int getLength(){
		int length = 0; //total number of elements in the doubly linked list
	List* tempNode = headNode;
	
      if(tempNode != NULL) {
       
        while(tempNode != NULL) {
          cout<<tempNode->item<<" ";
          length++;
          tempNode = tempNode->nextNode;
        }
        
      } 
      return length;
}   
};






//Testing code
int main() {
  Node list;


  //Putting element into the linked list
  list.push_item(1);
  list.push_item(2);
  list.push_item(3);
  list.push_item(4);
  list.push_item(5);
  list.push_item(6);
 
  list.displayList(); //Displaying the contents of the linked list


  cout<<"\nDeleting the last element of the linked list\n";
  list.deleteLast();
  list.displayList();
  
 int n = list.getLength(); //Getting the total number of elements of the linked list
 n = n -1 ;//Getting the second last element of the doubly linked list
  //Delete the second last element 
   cout<<"\nDeleting the second last element of the linked list\n";
  list.deleteSecondLast(n);  
  list.displayList();  
  
  return 0; 
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS