A C++ program for a list with insert, remove, search, display, and reverse print functions. Handle queries for these operations-Data Structure 1
1. Preprocessor Directives and Global Variables:
#include <iostream>
--#include <iostream>: Includes the input-output stream library for basic input and output operations.
#define MAXSIZE 100
-- #define MAXSIZE 100: Defines a macro MAXSIZE with the value 100, representing the maximum size of the list.
using namespace std;
--using namespace std; : Allows the usage of elements from the std namespace without specifying it explicitly.
int arr[MAXSIZE], listSize = 0;
--int arr[MAXSIZE], listSize = 0;: Declares an integer array arr of size MAXSIZE to store list elements and an integer listSize to keep track of the number of elements currently in the list.
2. Function Definitions:
bool isEmpty()
--bool isEmpty(): Checks if the list is empty by comparing `listSize` with 0 and returns a boolean value.
{
return listSize==0;
}
void insert (int a, int b)
--void insert(int a, int b): Inserts element `a` at position `b` in the list. It shifts elements to the right to make space for the new element.
{
b--;
if(listSize>=MAXSIZE)
{
cout<<"List is Full"<<endl;
return;
}
if( b <0 || b+1>listSize+1)
{
cout<<"Invalid Position"<<endl;
return;
}
for(int i=listSize-1;i>=b;i--)
{
arr[i+1]=arr[i];
}
arr[b]=a;
listSize++;
}
void remove(int a)
--void remove(int a):Removes the first occurrence of element `a` from the list.
{
int b= -1;
for(int i=0;i<listSize;i++)
{
if(arr[i]==a)
{
b=i;
break;
}
}
if (b== -1)
{
cout<<"Data not found"<<endl;
return;
}
for(int i=b;i<listSize-1;i++)
{
arr[i]=arr[i+1];
}
listSize--;
}
void search(int a)
--void search(int a): Searches for the first occurrence of element `a` in the list and prints its position if found.
{
for(int i=0;i<listSize; i++)
{
if(arr[i]==a)
{
cout<<"Data found at the position "<<i+1<<endl;
return;
}
}
cout<<"Data not found"<<endl;
}
void display()
--void display(): Displays all elements of the list.
{
if(isEmpty())
{
cout<<"List is Empty"<<endl;
return;
}
for(int i=0;i<listSize;i++)
{
cout<<arr[i] <<” ”;
}
cout<<endl;
}
void printReverse()
--void printReverse(): Prints the elements of the list in reverse order.
{
if(isEmpty())
{
for(int index = listSize-1;index >= 0; index--)
{
cout << arr[index] << “ “;
}
cout << endl;
}
else
{
cout << “List is Empty” << endl;
}
}
3. Main Function:
int main()
--int main(): Entry point of the program.
{
int N, queryType, data, position;
--int N, queryType, data, position;: Declares variables to store input values.
cin >> N;
--cin >> N;: Reads the number of queries to be performed.
for(int query = 1; query <= N; query++)
--for(int query = 1; query <= N; query++)`: Loop to iterate through each query.
{
- Inside the loop:
cin >> queryType;
--cin >> queryType;: Reads the type of query.
switch(queryType)
--switch(queryType): Switch statement based on the type of query.
{
case 1:
--case 1: If the query type is 1, it reads data and position, then calls `insert()` function.
cin >> data >> position;
insert(data, position);
break;
case 2:
--case 2: If the query type is 2, it reads data and calls `remove()` function.
cin >> data;
remove(data);
break;
case 3:
--case 3: If the query type is 3, it reads data and calls `search()` function.
cin >> data;
search(data);
break;
case 4:
--case 4: If the query type is 4, it calls `display()` function.
display();
break;
case 5:
--case 5: If the query type is 5, it calls `printReverse()` function.
printReverse();
break;
}
}
return 0;
--return 0;: Indicates successful termination of the program.
}
***Overall, this program provides a basic implementation of a list with various operations that can be performed on it. Users can input queries to insert, remove, search, display, or print the list in reverse order.****





Comments
Post a Comment