-->




stack implementation in c++ using array source code

Leave a Comment
Stack in C++ program
This c++ program has 10 functions. Each function calls on menu based selection using switch statement (functions names may very in source code). Each function and whole code has proper commented to explain the code.  This program has been tested on Visual Studio IDE.

What is a stack?
Stack is know as LIFO(Last in First Out) functionality which means a thing or an object which has been added last time will out first. Like Placing books on one an other the last book we placed will be picked first to collect all books. To now more about stack in c++ read wiki .


List of Stack Functions


  1. void createStack();
  2. void menu();
  3. void display();
  4. void deleteStack();
  5. void clearStack();                     // declaration of functions 
  6. int pop();
  7. void push(int);
  8. void findelement();
  9. int isfull();
  10. int isempty();
  11. void find_by_position();
Program brief introduction


  • Every function will be called on menu based selection
  • An integer pointer has declared for dynamic memory allocation
  • Global variable size and top has declared to track the stack status in whole program

C++ Source Code


#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int top=-1;                         // -1 shows that stack is not created
int size=0;                         // initial size is zero
int *stk;             // initialization of dynamic array of typ int [works as stack]
/////////////////////////////////////////////////////////////////////////////////////
void createstk();
void menu();
void display();
void deletestk();
void clearstk();                     // declaration of functions 
int pop();
void push(int);
void findelement();
int isfull();
int isempty();
int checkIndex=0;
void find_by_position();
 /////////////////////////////////////////////////////////////////////////////////////
void menu()                      
{
     int option;
     system("pause");
     system("cls"); 
     while(option<1||option>10)                             // continue till we select option from 1-10
     {       
     system("cls"); 
     cout<<"press 0 to find element by position\n";
     cout<<"press 1 to create stack\n";
     cout<<"press 2 to display stack\n";
     cout<<"press 3 to clear stack\n";
     cout<<"press 4 to delete stack\n";                    
     cout<<"press 5 to find element in stack\n";        
     cout<<"press 6 to remove element from stack\n" ;
     cout<<"press 7 to add element in stack\n";
     cout<<"press 8 to check whether stack is full\n";
     cout<<"press 9 to check whether stack is empty\n";
     cout<<"press 10 to exit\n";
     cin>>option;
     
     
     switch(option)
     {
      case 0:
      find_by_position();
      break;

     case 1:
     createstk();
     break;
          
     case 2:
     display();
     break;
     
     case 3:
     clearstk();
     break;
          
     case 4:
     deletestk();
     break;    
     
     case 5:
     findelement();
     break;    
     
     case 6:
     {
     if(top<0)
     cout<<"stack not created\n";                  // check to see if stack is created or not
     if(isempty()==1)
     cout<<"stack is empty\n";                     // check to see if stack is empty 
     else 
     pop();                                        // pop element
     }
     menu();
     break;
     
     case 7:
     {
     int a;                 
     if(top<0)
     cout<<"stack not created\n";                   // check to see if stack is created or not
     else 
     if(isfull()==1 )
     cout<<"stack is full\n";                       //check to see if stack is full 
     else 
     cout<<"enter element to push on stack:";       // take element to push on stack if it is not full
     cin>>a;           
     push(a);                         
     }
     menu();
     break;
     
     
     case 8:
     {
     int a;      
     if(top<0)                                        //check to see if stack is created or not
     cout<<"stack not created\n";
     else
     isfull();                                         //calling isfull funtion
     if(a==1)
     cout<<"stack is full\n";                         //if isfull function returns 1 (true) then stack is full
     else
     cout<<"stack is not full\n";                     //else stack is not full
     }
     menu();
     break;
     
     
     case 9:
     {
     int a;
     if(top<0)                                     
     cout<<"stack not created\n";                    //check to see if stack is created or not
     else
     isempty();                                      //calling isempty function
     if(a==1)                                        //if isempty function returns 1(true) then stack is empty
     cout<<"stack is empty\n";
     else
     cout<<"stack is not empty\n";                  //else stack is not empty
     } 
     menu();
     break;
 
          
                       
     case 10:
     exit(0);                                       //calling default built in function to exit program
     break;
     
     
     default:
     cout<<"invalid option";
     break;
     }    
     }
     }
 ///////////////////////////////////////////////////////////////////////////////////////////    
 void createstk()        
 {
      cout<<"enter total size of stack:";
      cin>>size;     
   stk=new int[size];                                        // take total size of stack
     do
     {
           
                         
     cout<<"how many elements do you want to enter?\n";     //take current size of stack that we are going to use yet
     cin>>top;
                         
     }
     while(top<1||top>size);                               //prompt to enter current size till size entered is less then 1
                                                           // or greater then total size
                                                           
     cout<<"enter elements\n";                             
     for(int i=0;i<top;i++)                                //for loop from index 0 to current size (top)
     {
     cin>>stk[i];                                          //input value in stack
     
     }    
     menu();                                               //function call to enter main menu again
     } 
///////////////////////////////////////////////////////////////////////////////////////////
void display()
{
     if(top<0)
     cout<<"stack not created\n";                         //check to see if stack is created
     else
     cout<<"total number of elements in stack are :"<<top;       //displays total number of elements in stack
                                                                //by diplaying current value of top
     
     cout<<"\nand are as follows\n";                           
                                                                                      
                                                                                      
     for(int i=0;i<top;i++)                                 //loop from 0 index to top
     cout<<stk[i]<<endl;                                    //displays value in stack 
     getch();
     menu();                                              //function call to enter main menu again
}

////////////////////////////////////////////////////////////////////////////////////////////
void clearstk()
{
     if(top<0)
     cout<<"stack not created\n";                          //check to see if stack is created or not
     else
     top=0;                                               //if stack is created then make top=0 and clear it
     cout<<"stack is cleared\n";
     menu();
}


////////////////////////////////////////////////////////////////////////////////////////////////
void  deletestk()
{
      if(top<0)
      cout<<"stack not created\n";                       //check to see if stack is created or not
      else
      top=-1;
      cout<<"stack is deleted\n";                        //if stack is created then make top=-1 and delete it
      menu();
}
 ///////////////////////////////////////////////////////////////////////////////////////////////
void findelement()
{
     int item,temp=0;                                 //initialize temp variable from 0 + intializing variable to enter item
                                                        //to be found + intitailaizing counter variable 'i'
     if(top<0)
     cout<<"stack not created\n";                      //check to see if stack is created or not
     else
     {
     
     cout<<"enter item you want to find\n";
     cin>>item;                                        //input item to be found in stack
     for(int m=0;m<top;m++)                                //loop starts from zero index and continue upto top
     {
             if(stk[m]==item)                          //compare each content of stack with value entere
    {
    cout<<"item found at "<<m<<endl;
     temp=1;
             }
                                                //if item found then change value of temp to false the condition
             
             
     
     }
                                                     //control comes out of loop
    if(temp==0)
    {
    
     cout<<"item not found\n";                       //if temp!=1 displays that item not found
}
}
  menu();
     
  }
 /////////////////////////////////////////////////////////////////////////////////////////
 void push(int i)                                   //take arguement
 {
  
    
     stk[top]=i;                                   //places arguement at top
     top++;                                        //increment top to show next available position
  }
 //////////////////////////////////////////////////////////////////////////////////////// 
  int pop()
  {
    
     int i=stk[top];                               //remove element from top
     top--;                                        //decrements top 
  return i;
  }  
 /////////////////////////////////////////////////////////////////////////////////// 
  int isfull()
  {
    
     if( top==size)                              //check if top is equal to total size
     return 1 ;                                  //return true i-e stack is full
     else                                       
     return 0;                                   //else return false tnat stack is not full
     }
 /////////////////////////////////////////////////////////////////////////////////    
     int isempty()
     {
         if(top==0)                             //check if top is zero
         return 1;                              //return true that stack is empty
         else
         return 0;                              //else return false that stack is not empty
     } 
/////////////////////////////////////////////////////////////////////////////////////////   
void find_by_position()
{
     int pos;
     
     if(top<0)
     cout<<"stack not created\n";
     else
     if(top==0)
     cout<<"stack is empty\n";
     else
     {
     do
     {    
     cout<<"enter position to find its content\n";
     cin>>pos;
     }while(pos<0||pos>size); 
     for(int i=0;i<size;i++)
     {
             if(i==pos)
             {
                       cout<<"item at position "<<i<<" is "<<stk[i]<<endl;
                       
              }   //if closed
     }   //for closed
            
     }  //else closed
     menu();
     } //function closed
int main()
     {
         cout<<"welcome to stack application\n";
         menu();                                //funtion call to main menu
         getch();
         }


Program Sample output

create, enter size, select number of elements to enter then enter elements
stack c++ using array output program
Program output
Push element to stack


Output video

Download Source code from Google Drive

Note: It is recommended to dry run all the code for better understanding. There is always more way to code a program. Try to improve it by adding more functions and validations. Hope this mini c++ project help the beginners.

See also: Queue Implementation Using Array C++ Source Code
More C++ projects can be found here

Simple C++ mini projects for beginners 
Read More...

Array based Queue c++ simple project

Leave a Comment
In this c++ tutorial we will discuss  about Array Based Queue C++ project which have 10 functions including main function. User will select an option from main menu function and the respective function will be called. The code is for array based queue implementation. It comprises of various operations that can be performed on array based queue. This code can be used as a project or self-assignment by beginners of data structure. It is recommended to understand the code thoroughly mainly the “logic” then try to create logic for each individual function by yourself. It will surely enhance your coding capability

What is a Queue?
  • List of items arranged on basis of first in and first out principal is called queue.
  • It has 2 ends.
  • Data can be considered as to pass through hollow cylinder.
  • Data enters from one end and leaves from another end.
  • Data only moves in one direction.


Characteristics Of Queue:


  • It works on the FIFO (first in first out) principal.
  • It is ordered list and has elements/data of same type.
  • It doesn’t allow duplication of data.
  •  Enqueue function is used to insert new item in queue.
  • Dequeue function is used to remove any item from queue.
  •  If you want to dequeue any random item from queue, you will have to first dequeuer all items above it. Then remove the item you want. Then enqueue dequeued items accordingly.
cpp queue source code
queue image example 


Real World Examples:


  1. People standing in a queue for submission of their bills in bank. The person who came first will submit bill first. The person who came later will submit bill later.
  2. Cars on one way road. The car that entered first will exists first while the car that entered later will exit later.


Array Based Queue in C++ Data Structures
It is a linear data structure in which insertion of data/element takes places from end (rear) and deletion of data/element takes place from front (head).It allows you to store a data in form of array.

Project Overview:

Below I am going to share with you a c++ source code that is compiled using dev c++ compiler. The code is for array based queue implementation. It comprises of various operations that can be performed on array based queue. This code can be used as a project or self-assignment by beginners of data structure. It is recommended to understand the code thoroughly mainly the “logic” then try to create logic for each individual function by yourself. It will surely enhance your coding capability.

Code:

#include<iostream>     //header files
#include<conio.h> //header files
#include<stdlib.h>      //header files
#include<stdio.h>     //header files
using namespace std;

//////////////////////////////////////////////////////////////////////////////////////
/*declaration of global variables*/
/* front will show first index of array, initiated with -1 that shows that nothing is present at first position in queue*/
int front=-1;   
/* rear will show first last index of array, initiated with -1 that shows that nothing is present at last position in queue*/
int rear=-1;
int index=0;          // index will represent that how long is queue
int *queue=new int[index];      // for dynamical creation of queue
//////////////////////////////////////////////////////////////////////////////////////
/*declaration of functions*/
The functions below are named in such a way that they will clearly reveal what they are meant for
//////////////////////////////////////////////////////////////////////////////////////
void createqueue();
void enqueue(int a);
int dequeue();
void deletequeue();
void clearqueue();
int isfull();
int isempty();
void find();
void display();
void menu();

/////////////////////////////////////////////////////////////////////////////////////
The function below is the first function that would be called when code is executed. It will display a menu having different functionalities. Just select the number corresponding to operation you want to perform and then you will be on your way to go 
////////////////////////////////////////////////////////////////////////////////////
void menu()
{ 
     system("pause");     /*built in function supported by dev c++ to pause a screen until “enter” or any key is entered to proceed further.*/
     system("cls");   /*It will remove any garbage value that may appear on screen*/
     int option;
     cout<<"****welcome to queue application****\n";
     cout<<"press 1 to create queue\n";
     cout<<"press 2 to display queue\n";
     cout<<"press 3 to delete queue\n";
     cout<<"press 4 to clear queue\n";
     cout<<"press 5 to find number in queue\n";
     cout<<"press 6 to enqueue\n";
     cout<<"press 7 to dequeue\n";
     cout<<"press 8 to check if queue is full\n";
     cout<<"press 9 to check if queue is empty\n";
     cout<<"press 0 to exit\n";
     cout<<"enter option\n";
     cin>>option;
//////////////////////////////////////////////////////////////////////////////////////
Below I have used a switch to call various functions. If you are a beginner and don’t have any idea about “switch” you can go ahead with if-else statements but their drawback is they mess up everything, make confusions as code gets lengthy. 
/////////////////////////////////////////////////////////////////////////////////////     
     switch(option)
     {
     case 1:   // this case will simply call create queue function and will create it.
          createqueue();
          menu();
          break;
          
     case 2:   // this case will call display function and will display queue items
          display();
          menu();
          break;
     
     case 3:  // this function will delete whole queue by calling delete queue function
          deletequeue();
          menu();
          break;
     
     case 4:     // it will only clear items in queue but empty queue will still exist
          clearqueue();
          menu();
          break;
     
     case 5:   // it will search for required item in queue
          find();
          menu();
          break;
     
     case 6:    //
          {
          int a;
/*the check below will be used throughout the code to check if queue is created or not. Note the point that whenever front and rear will have negative value it means that array does not exist because array’s index can never be negative*/
          if(front==-1&&rear==-1)
          cout<<"queue not created\n";
          else  
          if(isfull()==1)
          
          cout<<"queue is already full\n";
                     
          else
          {
          cout<<"enter element to enqueue\n";
          cin>>a;
          enqueue(a);
          }
          menu();
          }
          break;
     
     case 7:
          {
          if(front==-1&&rear==-1)
          cout<<"queue not created\n";
          else 
          if(isempty()==1)
          {
          cout<<"queue is already empty\n";
          }
          else
          {
          cout<<"element dequeued is :"<<dequeue();
          cout<<endl;                
          }
          menu();
          } 
          break;
     
     case 8:
          {     
          int a;     
          if(front==-1&&rear==-1)
          cout<<"queue not created\n";
          else   
          isfull();
          if(a==1)
          cout<<"queue is full\n";
          else
          cout<<"queue is not full\n";
          }
          menu();
          break;
     
     case 9:
          {
          int a;     
          if(front==-1&&rear==-1)
          cout<<"queue not created\n";
          else   
          isempty();
          if(a==1)
          cout<<"queue is empty\n";
          else
          {
          cout<<"queue is not empty\n";   
          }
          menu();
          }
          break;
          
     case 0:
          exit(0);
          break;
   
    default:
          cout<<"invalid input\n";
          break;
          }
          }
//////////////////////////////////////////////////////////////
          
void createqueue()
{
/*The loop below will continue till you enter correct size of queue that must be greater than zero*/
     do
     {
     cout<<"enter size of queue:";
     cin>>index;
     }
     while(index<=0);
//////////////////////////////////////////////////////////////////////////////////////
Suppose you entered 10 then, queue will be like that
Front/rear
queue input value
enter queue value = 10
Dequeue/ Enqueue ////////////////////////////////////////////////////////////////////////////////////// /*The loop below will continue till you enter accurate current size that must be positive and less then index value. The “rear” here is pointing last index. */ do { cout<<"enter current size of queue:"; cin>>rear; } while(rear>index||rear<0); ///////////////////////////////////////////////////////////////////////////////// Suppose you entered rear=5 so,
c++ enter input
queue input = 5
////////////////////////////////////////////////////////////////////////////////// front=0; // front always start from zero it could be 1 also. cout<<"enter elements\n"; for(int i=front;i<rear;i++)// loop will start from ‘0’ and continue till “rear” value { cin>>queue[i]; } cout<<"queue is successfully created\n"; }
c++ queue created function
queue created c++
////////////////////////////////////////////////////////////////////////////////////// The function below will be used for display. ////////////////////////////////////////////////////////////////////////////////////// void display() { if(front==-1&&rear==-1) cout<<"queue not created\n"; else if(front<rear) { cout<<"total number of elements in queue are:"; cout<<rear-front<<endl; // rear=5 , front=0 so 5-0=5 elements cout<<"elements are\n"; for(int i=front;i<rear;i++) cout<<queue[i]<<endl; } else { int length; length=(index-front)+rear; cout<<"total number of elements in queue are:"; cout<<length<<endl; cout<<"elements are\n"; for(int i=front;i<index;i++) for(int j=0;j<rear;j++) cout<<queue[1]<<endl; } } ////////////////////////////////////////////////////////////////// void deletequeue() { if(front==-1&&rear==-1) cout<<"queue not created\n"; else front=-1; rear=-1; cout<<"queue is successfully deleted\n"; } ///////////////////////////////////////////////////////////////// void clearqueue() { if(front==-1&&rear==-1) cout<<"queue not created\n"; else front=0; rear=0; cout<<"queue is successfully cleared\n"; } //////////////////////////////////////////////////////////////// void find() { int temp=0; int number; int i; if(front==-1&&rear==-1) cout<<"queue not created\n"; else cout<<"enter number to find in queue\n"; cin>>number; for(i=front;i<rear;i++) { if(queue[i]==number) { cout<<"number found at position"<<i<<endl; temp=1; } } if(temp==0) cout<<"number not found\n"; } ////////////////////////////////////////////////////////////// void enqueue(int a) { queue[rear]=a; rear=(rear+1)%index; cout<<"element is successfully enqueued\n"; } ////////////////////////////////////////////////////////////// int dequeue() { int a=queue[front]; front=(front+1)%index; return a; } //////////////////////////////////////////////////////////// int isfull() { if(rear-front==rear) return 1; else return 0; } /////////////////////////////////////////////////////////// int isempty() { if(front==rear) return 1; else return 0; } /////////////////////////////////////////////////////////////// int main() { menu(); return 0; }
C++ source code output:

Sample input output
queue project menu
menu output selection
if we select option 1 and enter 1, 2, 3 then option 2 we have
queue display c++ project
display queue c++ project
find more projects here: C++ projects source code

Read More...

Array based list c++ simple project

Leave a Comment
Array based list Implementation
What is Array based list?
  • It is a data structure that is used to store data in a single array.
  • Also known as backing array.
Advantages:
  1. They allow you to randomly access any item in list by providing constant time access to any value in array. 
Disadvantages:
  1. They cannot expand or shrink.
  2. They are not very dynamic. It means if you want to add or remove data in somewhere middle of the list you will need to shift all elements in the array either to fill the gap caused by removal of item or to make a space for inserting an item.
  3. If number of elements exceed the size of backing array then an expensive operations needs to be performed. The operation is to make a new array of size larger than previous array and shift all the elements of previous array in new array accordingly before making new insertions.
C++ project introduction
this project contains following list of functions. Every function is called by a menu option. 

  1. Create list
  2. Delete list
  3. Clear list
  4. Display list
  5. add item in list
  6. add item at particular place
  7. remove item from list
  8. remove item from particular position
  9. swap items by position
  10. find item in list
  11. find item by position
  12. copy list
  13. update item
  14. update item by position
  15. check size of list
  16. get element
  17. check if list is full
  18. check if list is empty


C++ Project Overview:


This code below can be used by beginners as a small project or assignment during semester. It contains menu based working using 19 functions. You can add more functions in it and can extend it to any level using your own customization.



/*inclusion of header files*/
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
////////////////////////////////////////////
/*initialization of global variables and dynamic array */
/*By global variable we mean the variables that can be used anywhere in whole code*/
int total_size=0;                                    // shows total size of list is zero
int *list=new int[total_size];              // dynamically creating array with size ‘0’
int cs=0;                                                 //shows that current size of list is zero
int cl=-1;                                                //shows that list is not created yet,  “cl=clear”

///////////////////////////////////////////
/*declaration of functions */
/*Below Is the list of functions that we will be using in our code*/
/*00*/void menu();
/*01*/void create_list();
/*02*/void delete_list();
/*03*/void clear_list();
/*04*/void display_list();
/*05*/void add_item(int);
/*06*/void add_item_by_position(int,int);
/*07*/void remove_item();
/*08*/void remove_item_by_position();       
/*09*/void swapp_items(int,int);
/*10*/void swapp_items_by_position(int,int);
/*11*/int find_item(int);
/*12*/int find_item_by_pos(int);
/*13*/void copy_list();
/*14*/void update_item(int,int);
/*15*/void update_item_by_position(int,int);
/*16*/void check_size_of_list();
/*17*/int get_element_by_position(int);
/*18*/int isfull();
/*19*/int isempty();
//////////////////////////////////////////////////////////////////////////////////////
The function that is written below will work throughout the code to display a menu of various functions. You will be prompted to enter the corresponding number of function you want to use and move ahead.
//////////////////////////////////////////////////////////////////////////////////////
void menu()
{
   system("pause");
   system("cls"); 
   int option;
   cout<<"****welcome to list application****\n";
   cout<<"press 0  to exit\n";           //
   cout<<"press 1  to create list\n";    //
   cout<<"press 2  to delete list\n";   //
   cout<<"press 3  to clear list\n";   //
   cout<<"press 4  to display list\n";  //
   cout<<"press 5  to add item in list\n"; //
   cout<<"press 6  to add item at particular position\n"; //
   cout<<"press 7  to remove item from list\n";
   cout<<"press 8  to remove item from particular position\n";
   cout<<"press 9  to swap items\n";//
   cout<<"press 10 to swap items by position\n";//
   cout<<"press 11 to find item in list\n";   //
   cout<<"press 12 to find item by position\n";   //
   cout<<"press 13 to copy list\n";//
   cout<<"press 14 to update item\n"; //
   cout<<"press 15 to update item by position\n"; //
   cout<<"press 16 to check size of list\n"; //
   cout<<"press 17 to get element\n"; //
   cout<<"enter 18 to check if list is full\n"; //
   cout<<"enter 19 to check if list is empty\n"; //
   cout<<"enter option:";
   cin>>option;
///////////////////////////////////////////////////  
   switch(option)
   {
    case 0:
    {
    exit(0);                               //built in function to exit from program    
    }
    break;
//////////////////////////////////////////////////   
               
    case 1:      
         {          
         create_list();
         menu();
         }
    break;
/////////////////////////////////////////////////
    case 2:
         {
         delete_list();
         menu();                                       
         }
    break;
////////////////////////////////////////////////
    case 3:
         {
         clear_list();
         menu();                                      
         }
    break;
////////////////////////////////////////////////                                                             
    case 4:
         {
         display_list();
         menu();
         }
    break;
///////////////////////////////////////////////
    case 5:
         {
         int it;                                     
         if(cl==-1)
         cout<<"list is not created\n";
         else
         if(isfull()==1)
         cout<<"list is already full\n";
         else
         do
          {
           cout<<"enter item:";
           cin>>it;
           if(find_item(it)==1)
           cout<<"item is already in list\n";
           }
                     while(find_item(it)==1);
           
           add_item(it);  
           cout<<"item added successfully in list\n";    
           cs++;                        
         }
         menu();
    break;
//////////////////////////////////////////////
    case 6:
         {
         int it,pos;
         if(cl==-1)
         cout<<"list is not created\n";
         else
          do
          {
          cout<<"enter item:";
          cin>>it;
          if(find_item(it)==1)
          cout<<"item is already in list\n";
          }
          while(find_item(it)==1);
         cout<<"enter position:";
         cin>>pos;
         if(cl==1&&pos<cs)
         {
         for(int i=cs-1;i>pos-1;i--)
         {
                 list[i+1]=list[i];
         }
         add_item_by_position(it,pos);
         
         cout<<"item added successfully at pos :"<<pos<<endl;
         cs++;
         }
         else
        
         if(cl==1&&pos>=cs&&pos< total_size)
        
         {
         list[cs]=it;
         cs++;
         }
         else
         cout<<"position out of range\n";
         }
           menu();
           break;      
/////////////////////////////////////////////
    case 7:
         {
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else
         remove_item();
         cout<<"item is successfully removed\n";                                   
         }
         menu();
         break;
////////////////////////////////////////////
    case 8:
         {
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else                                 
         remove_item_by_position();
         cout<<"item successfully deleted\n";                                 
         }
         menu();
         break;
////////////////////////////////////////////
    case 9:
         {
         int it1,it2;                                  
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else
         do
         {
         cout<<"enter item 1: ";
         cin>>it1;
         if(find_item(it1)!=1)
         cout<<"item is not in list\n";
         }
         while(find_item(it1)!=1);
         do
         {
         cout<<"enter item 2: ";
         cin>>it2;
         if(find_item(it1)!=1)
         cout<<"item is not in list\n";
         }
         while(find_item(it2)!=1);
         swapp_items(it1,it2);
         cout<<"items successfully swapped\n";
                                           
         }
         menu();
         break;
///////////////////////////////////////////                                                
    case 10:
         {
          int pos1,pos2;                                  
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else
         do
         {
         cout<<"enter position 1:";
         cin>>pos1;
         }
         while(pos1<0||pos1>=cs);
         do
         {
         cout<<"enter position 2:";
         cin>>pos2;
         }
         while(pos2<0||pos2>=cs);
        
         swapp_items_by_position(pos1,pos2);
         cout<<"items successfully swapped\n";
         }
         menu();
         break;
////////////////////////////////////////////
     case 11:
         {    
         int it;
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else
         {
         cout<<"enter item to check its presence in list\n";
         cin>>it;
         find_item(it);
         if(find_item(it)==1)
         {
         cout<<"item is present in list";
         int i=0;
         while(list[i]!=it)
         {
         i++;
         }
         cout<<"at position "<<i<<endl;                 
         }
         else
         cout<<"item not found\n";
         }
         menu();
         }
         break;
//////////////////////////////////////////////
         case 12:
         {
         int pos;
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else
         {
         do
         {
         cout<<"enter position to find its item\n";
         cin>>pos;  
         }
         while(pos<0||pos>ts);
         find_item_by_pos(pos);
         }
         menu();    
         }
         break;
//////////////////////////////////////////////
         case 13:
         {
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else  
         copy_list();
         cout<<"list copied successfully\n";
         }
         menu();
         break;
/////////////////////////////////////////////
         case 14:
         {
          int it1,it2;   
          if(cs==0)
          cout<<"list is empty\n";
          else
          if(cl==-1)
          cout<<"list is not created\n";
          else
          {
          do
          {       
          cout<<"enter item to be updated\n";
          cin>>it1;
          if(find_item(it1)!=1)
          cout<<"item is not in list\n";
          }
          while(find_item(it1)!=1);
          do
          {
          cout<<"enter new item to be replaced\n";
          cin>>it2;
          if(find_item(it2)==1)
          cout<<"item is already in list\n";
          }
          while(find_item(it2)==1);
          update_item(it1,it2);
         }
         }
         menu();
         break;
////////////////////////////////////////////
         case 15:
         {
          int it,pos;   
          if(cs==0)
          cout<<"list is empty\n";
          else
          if(cl==-1)
          cout<<"list is not created\n";
          else
          {
           do
           {   
           cout<<"enter item:";
           cin>>it;
           if(find_item(it)==1)
           cout<<"item already present\n";
           }
           while(find_item(it)==1);
           cout<<"enter position:";
           cin>>pos;
           if(pos<cs)
           update_item_by_position(it,pos);
           else
           cout<<"item at "<<pos<<" is not available to be updated\n";
          }
             
              
         }
         menu();
         break;
///////////////////////////////////////////
         case 16:
         {
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else    
         check_size_of_list();
        
         }
         menu();
         break;
//////////////////////////////////////////
         case 17:
         {   
              int a,pos;
              if(cs==0)
              cout<<"list is empty\n";
              else
              if(cl==-1)
              cout<<"list is not created\n";
              else
              do
              {
              cout<<"enter position to get from list:";
              cin>>pos;
              }
              while(pos<0||pos>cs);
              a=get_element_by_position(pos);
              cout<<"here is your item from list :"<<a<<endl;
         }
         menu();
         break;
//////////////////////////////////////////
         case 18:
         {
         if(cs==0)
         cout<<"list is empty\n";
         else
         if(cl==-1)
         cout<<"list is not created\n";
         else
         if(isfull()==1)
         cout<<"list is full\n";
         else
         cout<<"list is not full\n";
         }
         menu();
         break;
/////////////////////////////////////////
         case 19:
         {
         if(cl==-1)
         cout<<"list is not created\n";
         else
         if(isempty()==1)
         cout<<"list is empty\n";
         else
         cout<<"list is not empty\n";
         }
         menu();
         break;                 
/////////////////////////////////////////                                                                  
    default:
    cout<<"invalid option\n";
    menu();
    break;
//////////////////////////////////////////   
    }   //switch closed
    }   //funtion closed

/////////////////////////////////////////////////////////////////////////////////////////////////
/*definitions of functions*/
/*////////////////////////////////////////////////////////////////////////////////////
The function below will be used to create a list. In this function you will have to dynamically create a list first. As initially the total size of list was set to zero so that user can create a list of the size of its own choice and requirement. Total size could be any but make sure that it should be greater than current size of list. After entering total size enter current size of list that should be exactly the number of items you want to insert in list.
Suppose we set total_size to 10.
And current size (cs) to 5.
To avoid any errors we have put a check while getting value from user. You’ll be prompted to enter current size again and again if it is less than 1 or exceeds total size of list. After checking your list will be dynamically created.
After creation of list you will be prompted to insert items in a list. To avoid duplication, we have put a check on each entry. Whenever you will insert item in list, [find_item()] function will run and compare all previously inserted items of list with newly inserted item. If it is different, it will be entered otherwise it will prompt again and again to enter new value/item.
10
20
30
40
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts             


////////////////////////////////////////////////////////////////////////////////////*/
    void create_list()
     {
     int it;
     cout<<"enter total size of list:";
     cin>>total_size ;
     do
     {
     cout<<"enter current size of list:";
     cin>>cs;
     }
     while(cs<1||cs>=ts);
    
     int i=0;
     while(i<cs)
     {
       
     cout<<"enter item:";
     cin>>it;
       
     if(find_item(it)!=1)
     {
    
     list[i]=it;
     i++;
     }
     else
     cout<<"item already present\n";
     }       //while closed
     cl=1;
     }       //function closed

The function below can be considered as a heart of the code as it will work for insertion of any new entry in list to check for duplication. It will return 1 that means true if new entry of item matches already existing item in list. Otherwise it will return 0 that means false.
Working:
We will pass any item that we want to check for duplication to this function. It will start checking from them item present at first number of list till end of list. Each time it will compare the item passed to it with item at particular position. It will continue till it reaches end of list or find a duplicate item. If there is no duplicate item and function has reached end of list, it will return 0.
     
int find_item(int it)
     {
     int i;  
     for(i=0;i< total_size;i++)
         
     if(list[i]==it)
     return 1;
     }       
The function below will randomly access any item whose position you will pass to it, and then it will return you that item.
     int find_item_by_pos(int pos)
    
     {
     cout<<"item at position "<<pos<<" is "<< list[pos]<<endl; 
     return 1;
     }
/*Suppose you pass pos = 3
This function will return you 30. According to list.*/
The function below will be used to display status of list whenever needed. But before displaying it will check if list is empty? Or if list is not created yet. If any of the two options come true, it will display the message accordingly. Otherwise it will simply display a list.



     void display_list()
     {
     if(cs==0)
     cout<<"list is empty\n";
     else
     if(cl==-1)
     cout<<"list is not created\n";
     else    
     for(int i=0;i<cs;i++)
     cout<<list[i]<<endl;
     }
/*Output: After the listed you have created first by inserting 5 items, if you will run this function the output you will have will be like:
10
20
30
40
50
*/
/*////////////////////////////////////////////////////////////////////////////////////
The function below will simply delete the list by giving negative value to “cl”. Must note that array can never have negative value, but if you assign it this value it means that it doesn’t exist.
///////////////////////////////////////////////////////////////////////////////////*/

     void delete_list()
     {
     if(cl==-1)
     cout<<"list is not created\n";
     else
     cl=-1;
     cout<<"list is successfully deleted\n";
     }
////////////////////////////////////////////////////////////////////////////////////
/*////////////////////////////////////////////////////////////////////////////////////
The function below will clear the list i-e only remove items from the list. By doing this, list will come to its initial form. It will exist but will be empty. We have put check before clearing the list to check if list exists or not and if it exists is it already clear or not. The function will work if list exists and contains some values otherwise it will show a default message that you already set.
///////////////////////////////////////////////////////////////////////////////////*/

     void clear_list()
     {
     if(cl==-1)
     cout<<"list is not created\n";    
     else
     if(cs==0)
     cout<<"list is already empty\n";
     else
     cs=0;
     cout<<"list is successfully cleared\n";    
     }

The function below will add item at unallocated location right after last item in list.

     void add_item(int item)
     {
      list[cs]=item;
     } 



It will add item to specific location in list. For eg you want to insert item at 7th location then,
10
20
30
40
50

70



     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts            


    void add_item_by_position(int item,int pos)     
    {
      list[pos]=item;
       
    }

This function will return 1 if current size is equal to total size     indicating list if full and there is no more space to add more item
10
20
30
40
50
60
70
80
90
100
     1               2           3             4             5            6             7            8             9            10
                                                                                                                                       Cs/Ts                 
    int isfull()  
    {
    if(cs== total_size)
    return 1;
    else
    return -1;   
    }

This function will return 0 if current size is zero that indicates list is empty










     1               2           3             4             5            6             7            8             9            10
Cs=0                                                                                                                                  Ts

    int isempty()
    {
    if(cs==0)
    return 1;
    else
    return 0;
    }   

The function below will be used to update and item in a list . Suppose you want to update item 40 to 80 then use this function.
Before update:
10
20
30
40
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts            
                                             
After update:

10
20
30
80
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts             


    void update_item(int it1,int it2)
    {
   
    if(find_item(it1)==1)
    {
    int i;                    
    for(i=0;list[i]!=it1;i++)
    int a=list[i];
    list[i]=it2;
    cout<<"at"<<i<<"="<<list[i]<<endl;
    cout<<"item successfully updated\n";
    }
    else
    cout<<it1<<"is not in list\n";    
    }



The function below will be used to update and item in a list by defining position at which place you want to update an item. Suppose you want to update item 40 to 80 and 40 is at position ‘4’ then use this function, pass the position at new item to the function. The function will replace the existing value with new value at position passed as argument.
Before update:
10
20
30
40
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts            
                                             
After update:

10
20
30
80
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts             

////////////////////////////////////////////////////////////////////////////////////*/

    void update_item_by_position(int it,int pos)
    {
    int  a=list[pos];
    list[pos]=it;
    cout<<"item at position "<<pos<<" is successfully updated\n";
    cout<<"old item was:"<<a<<endl;
    cout<<"new item is :"<<it<<endl;   
    }       

Current size basically shows that how many items are in a list, so in order to get size of list the function below will be used that will print value of ‘Current size (cs)’.

    void check_size_of_list()
    {
    
     cout<<"current size of list is:"<<cs<<endl;
    }
The function below can be used to directly get the item present at particular position. Suppose in list below you want to get item present t position 4, so just pass number ‘4’ as argument and function will return ’40’.

10
20
30
40
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts            
                                             
/////////////////////////////////////////////////////////////////////////////*/
   int get_element_by_position(int pos)
   {
 
   return list[pos];
    
   }    
/*///////////////////////////////////////////////////////////////////////////////////   
The function below can be used to swap any two items in a list. You have to pass the items that you want to swap. The function will first find both values in a list and then swap them. Suppose you want to swap 20 and 40 in list so.
Before swapping:
10
20
30
40
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts            
                                             
After swapping:

10
40
30
20
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts             



   void swapp_items(int it1,int it2)
   {
    int j=0,k=0;
   
    do
    {
    j++;
    }
    while(list[j]!=it1);
  
    do
    {
    k++;
    }
    while(list[k]!=it2);
   int temp=0;
   temp=list[j];
   list[j]=list[k];
   list[k]=temp;
   }

The function below can be used to swap any two items in a list. You have to pass the positions of items that you want to swap. This thing will give you instant result and function will not have to find the values first. It will directly go to the positions and then swap items. Suppose you want to swap items at position 2 and 4  in list so.
Before swapping:
10
20
30
40
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts            
                                             
After swapping:

10
40
30
20
50





     1               2           3             4             5            6             7            8             9            10
                                                                 Cs                                                                        Ts             

////////////////////////////////////////////////////////////////////////////////////*/
  void swapp_items_by_position(int pos1,int pos2)
  {
    int temp=0;
    temp=list[pos1];
    list[pos1]=list[pos2];
    list[pos2]=temp;  
   }  
/*/////////////////////////////////////////////////////////////////////////////////
The function below can be used to copy list items from one list to another. 
////////////////////////////////////////////////////////////////////////////////*/
   void copy_list()
   {
    int i,j;   
    int *c_list=new int[cs];
    for(i=0;i<cs;i++)
    c_list[i]=list[i];
    cout<<"your copied list is\n";
    for(j=0;j<cs;j++)
    cout<<c_list[j]<<endl;  
    }
/*////////////////////////////////////////////////////////////////////////////////
The function below can be used to remove an item from list. You have to pass the item you want to delete. The function will first find item in list then delete it.


void remove_item()
 {
  int it;   
  do
  {
  cout<<"enter item to delete from list:";
  cin>>it;   
  if(find_item(it)!=1)
  cout<<"item is not in list\n";
  }
  while(find_item(it)!=1);
   int i=0;  
  do
  {
  i++;    
  }
  while(list[i]!=it);
  int a;
  a=list[i];
  for(int j=i;j<cs-1;j++)
  {
  list[j]=list[j+1];
  }
  cs--;
  }
/*/////////////////////////////////////////////////////////////////////////////
The function below can be used to remove an item from list. You have to pass the position from which you want to delete item. The function will go to that position in list then delete it.
//////////////////////////////////////////////////////////////////////////////*/
   void remove_item_by_position()
   {
    int pos;
    do
    {
    cout<<"enter position:";
    cin>>pos;
    if(pos<0||pos>cs)
    cout<<"there is no item at position "<<pos<<endl;   
    }
    while(pos<0||pos>cs);   
    int a;
    a=list[pos];
    for(int i=pos;i<cs;i++)
    {
     list[i]=list[i+1];
    }
    cs--;
   } 
////////////////////////////////////////////////////////////////////////////////    
/* start of main*/
////////////////////////////////////////////////////////////////////////////////////         
    int main()
    {
        menu();
        getch();
    }
/*end of main */


Download c++ source code

Sample input outputs
array based list c++ project example

Select option create list




 Display all items in list option 4

array code simple project eample



This C++ project has developed by a student and can be improve student like you. You may found bugs in it but they are meant to be solve by you. It is very good for learning to change the logic or flow of code according to your need


Read More...