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.
#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;
}
Comments
Leave a comment