Uploaded by furquan108549

uuuu

advertisement
NAME: Syed Furquan Haider Zaidi
Enrollment: 01-132202-041
Class: BCE-4A
Subject: Data Structures & Algorithms
Code:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
/*initialization of global variables and dynamic array */
int ts=0;
int *list=new int[ts];
int cs=0;
int a=-1;
/*declaration of functions */
int menu();
void create_list();
void delete_list();
void print_list();
void MAKENULL_list();
void CONCATINATE_LIST(int,int);
int insert (int);
int NEXT_list(int);
void locate(int,int);
int RETRIEVE(int);
int first;
int menu()
{
char option;
cout<<" Grocery items Category : "<<endl;
cout<<" Grocery items Name : "<<endl;
cout<<" Menu driven program\n";
cout<<"Please Enter e for exit\n"; //
cout<<"Please Enter c for create list\n"; //
cout<<"Please Enter d for delete list\n"; //
cout<<"Please Enter p for print list\n"; //
cout<<"Please Enter n for MAKENULL list\n";
cout<<"Please Enter o for CONCATINATE LIST\n";//
cout<<"Please Enter i for insert list\n"; //
cout<<"Please Enter x for NEXT list\n"; //
cout<<"Please Enter l for locate\n"; //
cout<<"Please Enter r for RETRIEVE\n"; //
cout<<"Please Enter f for first\n"; //
cout<<"Please Enter option:";
cin>>option;
///////////////////////////////////////////////////
switch(option)
{
case 'e':
{
exit(0); //built in function for exit from program
}
break;
//////////////////////////////////////////////////
case 'c':
{
create_list();
menu();
}
break;
/////////////////////////////////////////////////
case 'd':
{
delete_list();
menu();
}
break;
////////////////////////////////////////////////
case 'p':
{
print_list();
menu();
}
break;
///////////////////////////////////////////////
case 'n':
{
if(cs==0)
cout<<"list is empty\n";
else
if(a==-1)
cout<<"list is not created\n";
else
MAKENULL_list();
cout<<"item is successfully removed\n";
}
menu();
break;
////////////////////////////////////////////
////////////////////////////////////////////
case 'o':
{
int it1,it2;
if(cs==0)
cout<<"list is empty\n";
else
if(a==-1)
cout<<"list is not created\n";
else
do
{
cout<<"enter item 1: ";
cin>>it1;
if(insert (it1)!=1)
cout<<"item is not in list\n";
}
while(insert (it1)!=1);
do
{
cout<<"enter item 2: ";
cin>>it2;
if(insert (it1)!=1)
cout<<"item is not in list\n";
}
while(insert (it2)!=1);
CONCATINATE_LIST(it1,it2);
cout<<"items successfully \n";
}
menu();
break;
///////////////////////////////////////////
case 'i':
{
int it;
if(cs==0)
cout<<"list is empty\n";
else
if(a==-1)
cout<<"list is not created\n";
else
{
cout<<"enter item for check its presence in list\n";
cin>>it;
insert (it);
if(insert (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 'x':
{
int pos;
if(cs==0)
cout<<"list is empty\n";
else
if(a==-1)
cout<<"list is not created\n";
else
{
do
{
cout<<"enter position for find its item\n";
cin>>pos;
}
while(pos<0||pos>ts);
NEXT_list(pos);
}
menu();
}
break;
//////////////////////////////////////////////
case 'l':
{
int it,pos;
if(cs==0)
cout<<"list is empty\n";
else
if(a==-1)
cout<<"list is not created\n";
else
{
do
{
cout<<"enter item:";
cin>>it;
if(insert (it)==1)
cout<<"item already present\n";
}
while(insert (it)==1);
cout<<"enter position:";
cin>>pos;
if(pos<cs)
locate(it,pos);
else
cout<<"item at "<<pos<<" is not available for be updated\n";
}
}
menu();
break;
///////////////////////////////////////////
default:
cout<<"invalid option\n";
menu();
break;
} //switch closed
} //funtion closed
/////////////////////////////////////////////////////////////////////////////////////////////////
/*definitions of functions*/
////////////////////////////////////////////////////////////////////////////////////////////////
void create_list()
{
int it;
cout<<"Enter the grocery items Quantity list:";
cin>>ts;
do
{
cout<<"enter current size of list:";
cin>>cs;
}
while(cs<1||cs>=ts);
int i=0;
while(i<cs)
{
cout<<" Enter grocery item "<<i+1<<" : ";
cin>>it;
if(insert (it)!=1)
{
list[i]=it;
i++;
}
else
cout<<"item already present\n";
}
a=1;
}
int insert (int it)
{
int i;
for(i=0;i<ts;i++)
if(list[i]==it)
return 1;
}
int NEXT_list(int pos)
{
cout<<"item at position "<<pos<<" is "<< list[pos]<<endl;
return 1;
}
void print_list()
{
if(cs==0)
cout<<"list is empty\n";
else
if(a==-1)
cout<<"list is not created\n";
else
for(int i=0;i<cs;i++)
cout<<list[i]<<endl;
}
void delete_list()
{
if(a==-1)
cout<<"list is not created\n";
else
a=-1;
cout<<"list is successfully deleted\n";
}
void clear_list()
{
if(a==-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";
}
void add_item(int item)
{
list[cs]=item;
}
void add_item_by_position(int item,int pos)
{
list[pos]=item;
}
int isfull()
{
if(cs==ts)
return 1;
else
return -1;
}
int isempty()
{
if(cs==0)
return 1;
else
return 0;
}
void update_item(int it1,int it2)
{
if(insert (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";
}
void locate(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;
}
/////////////////////////////////////////////////////////////////////////////////
void check_size_of_list()
{
cout<<"current size of list is:"<<cs<<endl;
}
int RETRIEVE(int pos)
{
return list[pos];
}
void CONCATINATE_LIST(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;
}
void CONCATINATE_LIST_by_position(int pos1,int pos2)
{
int temp=0;
temp=list[pos1];
list[pos1]=list[pos2];
list[pos2]=temp;
}
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;
}
void MAKENULL_list()
{
int it;
do
{
cout<<"enter item for delete from list:";
cin>>it;
if(insert (it)!=1)
cout<<"item is not in list\n";
}
while(insert (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--;
}
void MAKENULL_list_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--;
}
int main() /* main part */
{
menu();
/* menu function */
getch();
return 0;
}}
Note:
sorry sir I tried to make structure of name category and brand but the pro gram was not running
And I done the code on DEV c++. And fail to make 3 file structure.
Download