0% found this document useful (0 votes)
35 views

Array Implementation of List

This C++ program implements a list using an array data structure. The Array class contains an integer array to store list elements and methods for inserting elements into the array at a given position, displaying all elements, and deleting elements. The main function demonstrates using the Array class by inserting, displaying, and deleting elements from the list.

Uploaded by

Narendran K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Array Implementation of List

This C++ program implements a list using an array data structure. The Array class contains an integer array to store list elements and methods for inserting elements into the array at a given position, displaying all elements, and deleting elements. The main function demonstrates using the Array class by inserting, displaying, and deleting elements from the list.

Uploaded by

Narendran K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Array implementation of list

#include<iostream>
using namespace std;
class Array
{
public:
int a[100],n;
Array()
{
n=0;
}
void insert(int val,int pos)
{
if(n==0)
a[0]=val;
else
{
if(pos>n)
pos=n+1;
for(int i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=val;
}
n++;
}
void display()
{
if(n==0)
cout<<"Empty array";
else
{
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<"\n";

}
}
void del(int pos)
{
if(n==0)
cout<<"Empty array";
else
{
if(pos>=n)
pos=n;
for(int i=pos-1;i<n;i++)
a[i]=a[i+1];
n--;
}
}
};
main()
{
Array a;
a.insert(5,4);
a.display();
a.insert(6,2);
a.display();
a.insert(7,1);
a.display();
a.insert(8,2);
a.display();
a.insert(5,1);
a.display();
a.del(3);
a.display();
a.del(1);
a.display();
a.del(7);
a.display();
}

You might also like