Answer to Question #240604 in C++ for jan

Question #240604

Consider the list of integers (4,5,6,7,8) and we are implementing this list using an Array.

Do the following operations on the given list in C++ language:

·        Add(9): Using Add() to add an element 9 between 6 and 7.

·        Next(): Using Next() to move current pointer 1 shift to the right.

·        Remove(): To remove an element 7.

·        Length(): To return size of the list.

·        find(7): traverse the array until 7 is located.


1
Expert's answer
2021-09-22T06:28:48-0400
#include<iostream>
using namespace std;
int arr[7],  n=0;
void add(int x);
int Next(int x);
void Remove(int x);
int Length();
int find(int x);


void add(int x){


	arr[n] = x;
	n++;
	
	
}
int find(int x){
	int index;
	for(int i=0; i<n; i++){
		 
		if(arr[i]==x){
			index = i;
		}
	}
	return index;
}
void display(){
	for(int i=0; i<n; i++){
		cout<<arr[i]<<"  ";
	}
}


void Remove(int x){
	int index;
	for(int i=0; i<n; i++){
		if(arr[i] == x){
			index = i;
		}
	}
	cout<<"index is: "<<index<<endl;
	int arr1[n-1];
	for(int i=0, j=0; i<n; i++){
		if( i != index){
			arr[j++] = arr[i];
		}
	}
	cout<<"The resulting list is: "<<endl;
	for(int i=0; i<n-1; i++){
		cout<<arr1[i]<<"  ";
	}
	cout<<endl;
}


int Length(){
	return n;
}
int Next(int x){
	int next;
	for(int i=0; i<n; i++){
		if(arr[i]==x){
			next = arr[i+1];
		}
	}
	return next;
}
int main(){
	add(4);
	add(5);
	add(6);
	add(9);
	add(7);
	add(8);
	display();
	cout<<"\n7 is found and located at index "<<find(7)<<endl;


}

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