You are on page 1of 26

SUBMISSION BY

Kshitij Kumar Singh Chauhan


CSE, 201000022

README

Author Of Code : Kshitij Kumar Singh Chauhan


Language : C++ 17
Compiler Used For Testing : g++

Fig (i) : Compiler info

LIST OF UTILITIES AVAILABLE

INITIALIZE
1. Fill all : fill list completely.
2. Get element from position : return an element from the list at any given position.
3. Reinitialize to empty : reinitialize list as empty
INSERTION
4. Insert element at position : insert a new element at any position of the list.
5. Insert at end : insert a new element at the end of the list.

DELETION
6. Remove a given element : remove the first occurrence of any element from a non-empty list.
7. Remove element at given position : remove the element at a specified location from a non-empty list.

SEARCHING
8. Search an element : search an element and return position, if found.
9. Search and Replace an element with another element : replace an element by another element.

PRINTING
10. Print all : print complete list
11. Print after a position : print elements after a certain position.
12. Print till a position : print elements till a certain position.
13. Print in range : print elements in an interval of positions.

MISCELLANEOUS
14. Tell current size : return the number of elements in the list.
15. Is my list empty ? : return true if the list is empty, otherwise return false
16. Is my list full ? : return true if the list is full, otherwise return false
17. Exit
LIST OF FIGURES

Fig 1. Execution

OPERTAIONS FOR INITIALIZATION


Fig 2. Fill all (a)
Fig 3. Fill all (b)
Fig 4. Get elements from position (a)
Fig 5. Get elements from position (b)
Fig 6. Reinitialize to empty (a)
Fig 7. Reinitialize to empty (b)

OPERATIONS TO INSERTION
Fig 8. Insert element at position (a)
Fig 9. Insert element at position (b)
Fig 10. Insert element at end (a)
Fig 10. Insert element at end (b)

OPERATIONS FOR DELETION


Fig 12. Remove a given element (a)
Fig 13. Remove a given element (b)
Fig 14. Remove element at given position (a)
Fig 15. Remove element at given position (b)

OPERATIONS FOR SEARCHING


Fig 16. Search an element (a)
Fig 17. Search an element (b)
Fig 18. Search and replace element with a given element (a)
Fig 19. Search and replace element with a given element (b)
OPERATIONS FOR PRINTING
Fig 20. Print all
Fig 21. Print after a given position (a)
Fig 22. Print after a given position (b)
Fig 23. Print till a given position (a)
Fig 24. Print till a given position (b)
Fig 25. Print in range (a)
Fig 26. Print in range (b)

MISCELLANEOUS OPERATIONS
Fig 27. Tell current size (a)
Fig 28. Tell current size (b)
Fig 29. Is my list empty ? (a)
Fig 30. Is my list empty ? (b)
Fig 31. Is my list full ? (a)
Fig 32. Is my list full ? (b)

Fig 33. Terminating

SOURCE CODE

#include<iostream>

//set of functions
//MISCELLANEOUS
void tell_size(int current_size, int size) //f1
{
std::cout<<"Total size : "<<size<<"\n";
std::cout<<"Used size : "<<current_size<<"\n";
std::cout<<"Available size : "<<(size - current_size)<<"\n";
}

bool is_empty(int current_size, int size) //f2


{
if(current_size == 0)
{
std::cout<<"List is empty\n";
return true;
}
else
{
std::cout<<"List full | ("<<current_size<<" out of "<<size<<") filled\n";
return false;
}
}

bool is_full(int current_size, int size) //f3


{
if(size == current_size)
{
std::cout<<"List is full\n";
return true;
}
else
{
std::cout<<"("<<current_size<<" out of "<<size<<") filled\n";
return false;
}
}

//INITIALIZATION
void fill_all(int my_list[], int &current_size, int size) //f4
{
int run_again = 1;
while(run_again == 1)
{
if(!is_empty(current_size, size))
{
int choice = 0;
std::cout<<"List already has some elements\n";
std::cout<<"Continuing will overwrite those elements\n";
std::cout<<"Press 1 if you still wish to continue (else press anyother
number) : ";
std::cin>>choice;
std::cout<<"\n";
if(choice != 1)
return;
}

std::cout<<"Enter "<<size<<" elements :\n";


for(int i = 0; i<size; i++)
std::cin>>my_list[i];

current_size = size;

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

void get_from_pos(int my_list[], int current_size, int size) //f5


{
int run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

int pos;
std::cout<<"Which position do you wish to access (enter position)? : ";
std::cin>>pos;
std::cout<<"\n";
if( pos < 1)
std::cout<<"Position must be greater than 0\n";
else if(pos > current_size)
std::cout<<"Position must be less than current size of list\n";
else
std::cout<<"Element present at position "<<pos<<" is : "<<my_list[pos-
1]<<"\n";

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

void make_empty(int &current_size, int size) //f6


{
if(is_empty(current_size, size))
return;

current_size = 0;
}

//INSERTION
void insert_at_pos(int my_list[], int &current_size, int size) //f7
{
int run_again = 1;
while(run_again == 1)
{
if(is_full(current_size, size))
return;

int insert_it, insert_at, temp;


std::cout<<"What to insert (enter value)? : ";
std::cin>>insert_it;
std::cout<<"Where to insert (enter position)? : ";
std::cin>>insert_at;
std::cout<<"\n";
if( insert_at < 1)
std::cout<<"Position to insert must be greater than 0\n";
else if(insert_at > size)
std::cout<<"Position to insert must be less than size of list\n";
else if(insert_at > current_size)
{
my_list[insert_at-1] = insert_it;
current_size++;
}
else
{
temp = my_list[insert_at-1];
my_list[insert_at-1] = insert_it;
current_size++;
for(int i = current_size-1; i>insert_at-1; i--)
my_list[i] = my_list[i-1];
}

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

void insert_at_end(int my_list[], int &current_size, int size) //f8


{
int run_again = 1;
while(run_again == 1)
{
if(is_full(current_size, size))
return;

int insert_it;
std::cout<<"What to insert (enter value)? : ";
std::cin>>insert_it;
std::cout<<"\n";
my_list[current_size] = insert_it;
current_size++;

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

//DELETION
void remove_by_val(int my_list[], int &current_size, int size) //f9
{
int run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

int remove_it, found_at = 0, found = 0;


std::cout<<"What to remove (enter value)? : ";
std::cin>>remove_it;
std::cout<<"\n";

for(int i = 0; i<= current_size-1; i++)


{
if(my_list[i] == remove_it)
{
std::cout<<"Element "<<remove_it<<" found and removed from position :
"<<i+1<<"\n";
found = i+1;
for(int i = found-1; i< current_size-1; i++)
my_list[i] = my_list[i+1];
current_size--;
break;
}
}

if(found == 0)
std::cout<<"Not Found\n";

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

void remove_by_pos(int my_list[], int &current_size, int size) //f10


{
int run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

int remove_from, found_at = 0;


std::cout<<"Where to remove from (enter position)? : ";
std::cin>>remove_from;
std::cout<<"\n";

if(remove_from < 1)
std::cout<<"Value of remove from must be greater tha 1\n";
else if(remove_from > current_size)
std::cout<<"Position "<<remove_from<<" is already empty\n";
else
{
std::cout<<my_list[remove_from-1]<<" present at position
"<<remove_from<<" has been removed\n";
for(int i = remove_from-1; i< current_size-1; i++)
my_list[i] = my_list[i+1];
current_size--;
}

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

//SEARCHING
void search_by_val(int my_list[], int current_size, int size) //f11
{
int run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

int search_it, found = 0;


std::cout<<"What to search ? : ";
std::cin>>search_it;
std::cout<<"\n";
for(int i = 0; i<= current_size-1; i++)
{
if(my_list[i] == search_it)
{
std::cout<<"Element "<<search_it<<" found at position : "<<i+1<<"\n";
found = 1;
break;
}
}
if(found == 0)
std::cout<<"Not Found\n";

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

void search_and_replace(int my_list[], int current_size, int size) //f12


{
int run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

int search_it, replace_with, found = 0;


std::cout<<"What to search ? : ";
std::cin>>search_it;
std::cout<<"\nWhat to replace with ? : ";
std::cin>>replace_with;
std::cout<<"\n";

for(int i = 0; i<= current_size-1; i++)


{
if(my_list[i] == search_it)
{
my_list[i] = replace_with;
found = 1;
break;
}
}
if(found == 0)
std::cout<<"Not Found\n";
std::cout<<"Press 1 to run the operation again (else press any other
number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

//PRINTING
void print_all(int my_list[], int current_size, int size) //f13
{
int run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

for(int i = 0; i<= current_size-1; i++)


{
std::cout<<my_list[i]<<" ";
}
std::cout<<"\n";

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

void print_after(int my_list[], int current_size, int size) //f14


{
int after, run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

std::cout<<"\nFrom where to print ? (enter beginning position) -: ";


std::cin>>after;
std::cout<<"\n";
if(after<1)
std::cout<<"Value of after must be greater than zero\n";
else if(after>current_size)
std::cout<<"List is not filled till given value\n";
else
{
for(int i = after-1; i<= current_size-1; i++)
{
std::cout<<my_list[i]<<" ";
}
std::cout<<"\n";
}

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

void print_till(int my_list[], int current_size, int size) //f15


{
int till, run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

std::cout<<"\nTill where to print ? (enter ending position) -: ";


std::cin>>till;
std::cout<<"\n";
if(till<1)
std::cout<<"Value of till must be greater than zero\n";
else if(till>current_size)
std::cout<<"List is not filled till given value\n";
else
{
for(int i = 0; i<= till-1; i++)
{
std::cout<<my_list[i]<<" ";
}
std::cout<<"\n";
}

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}

void print_in_range(int my_list[], int current_size, int size) //f16


{
int from, to, run_again = 1;
while(run_again == 1)
{
if(is_empty(current_size, size))
return;

std::cout<<"From where to print ? (enter beginning position) -: ";


std::cin>>from;
std::cout<<"\nTill where to print ? (enter ending position) -: ";
std::cin>>to;
std::cout<<"\n";
if(from<1 || to<1)
std::cout<<"From and To must be greater than zero\n";
else if(from>to)
std::cout<<"From must be less than To\n";
else if(to>current_size)
std::cout<<"List is not yet filled in given range\n";
else
{
for(int i = from-1; i<= to-1; i++)
{
std::cout<<my_list[i]<<" ";
}
std::cout<<"\n";
}

std::cout<<"Press 1 to run the operation again (else press any other


number): ";
std::cin>>run_again;
std::cout<<"\n";
}
}

//main
int main()
{
int size; // specifies size of list to be declared
std::cout<<"Enter the size of list to be created : ";
std::cin>>size;
int my_list[size]; // declared a list of integers with size as provided by
user

int choice, current_size = 0;


/*
declaration of variables
choice -: to get user's choice of operation from available options
current_size -: to keep track of current status of list's storage
*/
while(1)
{
std::cout<<"\nAvailanle Operations :\n";
std::cout<<"\nINITIALIZE\n";
std::cout<<"1. Fill all\n"; //fill list completely.
std::cout<<"2. Get element from position\n"; //return an element from the
list at any given position.
std::cout<<"3. Reinitialise to empty\n"; //reinitialise list as empty

std::cout<<"\nINSERTION\n";
std::cout<<"4. Insert element at position\n"; //insert a new element at
any position of the list.
std::cout<<"5. Insert at end\n"; //insert a new element at the end of the
list.

std::cout<<"\nDELETION\n";
std::cout<<"6. Remove a given element\n"; //remove the first occurrence of
any element from a non empty list.
std::cout<<"7. Remove element at given position\n"; //remove the element
at a specified location from a non empty list.

std::cout<<"\nSEARCHING\n";
std::cout<<"8. Search an element\n"; //search an element and return
position, if found.
std::cout<<"9. Search and Replace an element with other element\n";
//replace an element by another element.

std::cout<<"\nPRINTING-\n";
std::cout<<"10. Print all\n"; //print complete list
std::cout<<"11. Print after a position\n"; //print elements after a
certain position.
std::cout<<"12. Print till a position\n"; // print elements till a certain
position.
std::cout<<"13. Print in range\n"; // print elements in an interval of
positions.

std::cout<<"\nMISCELLANEOUS\n";
std::cout<<"14. Tell current size\n"; //return the number of elements in
the list.
std::cout<<"15. Is my list empty ?\n"; //return true if the list is empty,
otherwise return false
std::cout<<"16. Is my list full ?\n"; //return true if the list is full,
otherwise return false
std::cout<<"17. Exit\n";
std::cout<<"\n";

std::cout<<"\nYour choice : ";


std::cin>>choice; //taking user's choice

switch(choice)
{
case 1 :
fill_all(my_list, current_size, size);
break;
case 2 :
get_from_pos(my_list, current_size, size);
break;
case 3 :
make_empty(current_size, size);
break;
case 4 :
insert_at_pos(my_list, current_size, size);
break;
case 5 :
insert_at_end(my_list, current_size, size);
break;
case 6 :
remove_by_val(my_list, current_size, size);
break;
case 7 :
remove_by_pos(my_list, current_size, size);
break;
case 8 :
search_by_val(my_list, current_size, size);
break;
case 9 :
search_and_replace(my_list, current_size, size);
break;
case 10 :
print_all(my_list, current_size, size);
break;
case 11 :
print_after(my_list, current_size, size);
break;
case 12 :
print_till(my_list, current_size, size);
break;
case 13 :
print_in_range(my_list, current_size, size);
break;
case 14 :
tell_size(current_size, size);
break;
case 15 :
is_empty(current_size, size);
break;
case 16 :
is_full(current_size, size);
break;
case 17 :
std::cout<<"Terminating...\n";
exit(0);
default :
std::cout<<"Invalid choice | Given choice not found in set of
available operations";

return 0;
}

SAMPLE OUTPUTS

NOTE
Following outputs have been classified into operations
as listed in available utilities. Outputs have been split
into multiple figures, wherever considered necessary, to
demonstrate different aspects of operations. List of
figures provided above may be used to navigate to
sample output of desired operation.
Fig 1. Execution
Fig 2. Fill all (a)

Fig 3. Fill all (b)

Fig 4. Get elements from position (a)

Fig 5. Get elements from position (b)


Fig 6. Reinitialize to empty (a)

Fig 7. Reinitialize to empty (b)

Fig 8. Insert element at position (a)


Fig 9. Insert element at position (b)

Fig 10. Insert element at end (a)

Fig 10. Insert element at end (b)

Fig 12. Remove a given element (a)


Fig 13. Remove a given element (b)

Fig 14. Remove element at given position (a)

Fig 15. Remove element at given position (b)


Fig 16. Search an element (a)

Fig 17. Search an element (b)

Fig 18. Search and replace element with a given element (a)

Fig 19. Search and replace element with a given element (b)
Fig 20. Print all

Fig 21. Print after a given position (a)

Fig 22. Print after a given position (b)


Fig 23. Print till a given position (a)

Fig 24. Print till a given position (b)

Fig 25. Print in range (a)


Fig 26. Print in range (b)

Fig 27. Tell current size (a)

Fig 28. Tell current size (b)

Fig 29. Is my list empty ? (a)


Fig 30. Is my list empty ? (b)

Fig 31. Is my list full ? (a)

Fig 32. Is my list full ? (b)

Fig 33. Terminating

You might also like