Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Data Structures Department of Software Engineering

General
At the top of every document that you create or modify (word processing files or source files/cpp)
include a comment with your identifying information:
/*
* Description of the class or document.
*
* @author YOUR NAME
* @Reg YOUR ROLL NUMBER
* @Lab Number = Lab 6, 20XX.01.09 (replace with the date that you last edited the lab)
*/
Submit this lab via LMS

AIM

To write a C++ program to implement list using an array

PRE-LAB DISCUSSION

Array of list is an important data structure used in many applications. It is an interesting structure to
form a useful data structure. It combines static and dynamic structure. Static means array and dynamic
means linked list used to form a useful data structure. Array elements can be stored in consecutive
manner in memory. Insert and delete operation takes more time in array. Array elements cannot be
added, deleted once it is declared. In array, elements can be modified easily by identifying the index
value. Pointer cannot be used in array. So, it does not require extra space in memory for pointer.

ALGORITHM

Step 1: Start.

Step 2: Declare the necessary functions for implementation. Step 3: Get the input from the user and
store it an array.

Step 4: In Insertion, half of the elements to be shifted upwards and in deletion half of the elements to be
shifted downwards.

Step 5: Display the output using an array. Step 6: Stop.

PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
void main()
{
//clrscr();
int ch;

1
Data Structures Department of Software Engineering

char g='y';
do
{
cout<<"\n main Menu");
cout<<"\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit
\n");
cout<<"\n Enter your Choice");
cin>>ch;
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
cout<<"\n Enter the correctchoice:";
}
cout<<"\n Do u want tocontinue:::";
cin>>g;
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
cout<<"\n Enter the number of nodes";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n Enter the Element:",i+1;
cin>> b[i];
}
}
void deletion()
{
cout<<"\n Enter the position u want to delete::");
cin>> pos;
if(pos>=n)
{
cout<<"\n Invalid Location::";
}
else
{

2
Data Structures Department of Software Engineering

for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
cout<<"\n The Elements after deletion";
for(i=0;i<n;i++)
{
cout<<b[i];
}
}
void search()
{
cout<<"\n Enter the Element to be searched:";
cin>>e;
for(i=0;i<n;i++)
{
if(b[i]==e)
{
cout<<"Value is in the Position"<< i;
}
else
{
cout<<"Value is not in the list::" << e;
continue;
}
}
}
void insert()
{
cout<<"\n Enter the position u need to insert::";
cin>> pos;
if(pos>=n)
{
cout<<"\n invalid Location::";
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
cout<<"\n Enter the element to insert::\n";
cin>>p;
b[pos]=p;
n++;
}
cout<<"\n The list after insertion::\n";
display();
}
void display()
{
cout<<"\n The Elements of The list ADT are:";
for(i=0;i<n;i++)
{
cout<<"\n” << b[i];

3
Data Structures Department of Software Engineering

}
}

OUTPUT
Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit

Enter your Choice1


Enter the number of nodes 2
Enter theElement:2
Enter theElement:3
Do u want to continue:::y

Main Menu

1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice 5
The Elements of The list ADT are:
2
3
Do u want to continue:::y

Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice 4
Enter the position u need to insert::0
Enter the element to inert::5
The list after insertion::
The Elements of the list ADT are:
5

4
Data Structures Department of Software Engineering

2
3
Do u want to continue:::y

Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice 2
Enter the position u want to delete::1
The Elements after deletion
5
3
Do u want to continue:::

Lab Practice
1. Create a one-dimensional Array. Get random numbers from user and insert into array. One
number should not be inserted 2 times.
2. Write a C++ Program to get input from user and delete the element if present in the array.
3. Define a 2D array of 2x2. Multiply both arrays and print.

Practice Problems / Home Task


1. Write a C++ program to print all elements in an array.
2. Write a C++ Program to get input from user and insert into an array.
3. Write a C++ program to find the largest and smallest number of a given array of numbers.
4. Print all vowels character from an array of random characters.
5. Find the average of all numbers in an array.
6. Define an array and print. Ask user to select an existing value and enter a new value.
Replace the old value with new value.
7. Define an array and print. Ask user to select an index and enter a new value. Update the old
value of specified index with new value.
8. Ask user to define an array of integers specified length. Insert 0 at 1 st index and sum of all
numbers at last index.
9. Define a 2D array in C++ and print all elements as rows and columns.
10. Define a 2D array and print transpose of it.

You might also like