Answer to Question #243056 in C++ for love

Question #243056

class List{

public:

int item;

int pos;

List* nextNode;

};


Task:

1)create the following functions

void Insert_Element_at(int X, int pos),

bool Delete_Element(int X),

bool is_Empty()

void Empty_List(),

void Copy_List(…)

2)Call the functions in main by makeing menu for them using switch_case statement and while loop.


1
Expert's answer
2021-09-27T15:47:02-0400
#include<iostream>
using namespace std;


class List{


public:


int item;


int pos;


List* nextNode;


};


int loc = 0;
List* newNode(int data)
{
    
    List* new_node= new List();
 
   
    new_node->item = data;
    loc++;
    new_node->nextNode = NULL;
    return new_node;
}
void displayList(struct List* head)
{
    while (head != NULL) {
        cout << " " << head->item;
        head = head->nextNode;
    }
    cout << endl;
}


void insertPos(List** current, int position, int data)
{
   
    
    if (position < 1 || position > loc+ 1)
        cout << "Invalid position!" << endl;
    else {
 
       
        while (position--) {
 
            if (position == 0) {
 
                
                List* temp = newNode(data);
 
                temp->nextNode = *current;
 
                
                *current = temp;
            }
            else
              
              current = &(*current)->nextNode;
        }
        loc++;
    }
}
void deleteNode(List** head, int X)
{
     
    
    List* temp = *head;
    List* prev = NULL;
     
    
    if (temp != NULL && temp->item == X)
    {
        *head = temp->nextNode; 
        delete temp;           
        return;
    }
 
    
      else
    {
    while (temp != NULL && temp->item!= X)
    {
        prev = temp;
        temp = temp->nextNode;
    }
 
    
    if (temp == NULL)
        return;
 
    
    prev->nextNode = temp->nextNode;
 
   
    delete temp;
    }
}
 bool is_Empty(){
 	if(loc==0){
 		return true;
	 }
	 else{
	 	return false;
	 }
 }
 void Empty_List(List *head){
 	head->item = 0;
 	head->nextNode = NULL;
 	head->pos = 0;
 }
 
 void push(List** head, int data)
{
    List* new_node = new List();
    new_node->item = data;
    new_node->nextNode = (*head);
    (*head) = new_node;
}
 
 
 void Copy_List(List*node, List *list){




List * temp = node;




while(temp !=NULL){




int data = temp->item;




push(&list, data);




temp = temp->nextNode;
}
displayList(list);


}


int main(){
	 List* head = NULL;
    head = newNode(3);
    head->nextNode = newNode(5);
    head->nextNode->nextNode = newNode(8);
    head->nextNode->nextNode->nextNode = newNode(10);
    
    
   char choice;
    do{
    	cout<<"Select an option\n1. Insert an item\n2. Delete an element.\n3. Check if the list is empty \n4. Make the linked list empty\n";
    cout<<"5. Make a copy of the linked list\n6.Exit the program\n";
    cin>>choice;
    switch(choice){
    	case '1':
    		cout<<"Enter the data to insert\n";
    		 int data , pos;
    		 cin>>data;
    		 cout<<"Enter the position to insert the data\n";
    		 cin>>pos;
    		  insertPos(&head, pos, data);
    		  cout<<"The new linked list is:   ";
    		   displayList(head);
    		   break;
    	case '2':
    		cout<<"Enter an item to delete\n";
    		int item;
    		cin>>item;
    		 deleteNode(&head, item);
    		displayList(head);
    		break;
    	case '3':
    		if(is_Empty()==true){
    			cout<<"The linked list is empty\n";
			}
			else{
				cout<<"The linked list is not empty\n";
			}
		case '4':
				Empty_List(head);
				displayList(head);
				break;
		case '5':
			 List* list = NULL;
    Copy_List(head,list);
    	break;	
	}
    
	}while(choice!='6');
    
}

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