If you are going to use this program on Windows then Delete all occurrences of \33[x where x may be 31,32,32.....,37. They have been used to produce colorful text.This program has be developed and tested on gcc.
First time it will ask the user to fill the list.
After that a menu will appear which will contain the following functions
1.View your list.
2.Add element at any position in the list.
3.Delete element(s) taking the value or the location.
4.Finding the location of the element(s) or element if location is provided.
5.Exit
#include<stdio.h>
int element,maxArraySize=65536;
int arr[65536];
int currentSizeOfArray=0;
//////Function Declarition//////
void printList(int x);
void addInput();
void addElement(int addPosition,int addItem);
void deleteInput();
void findItem();
void findByLocation(int location);
int findLocation(int item);
void findByValue(int elemn);
void deleteByLocation(int location);
void addList();
//////*****************//////////
//////Main Finction/////////////
int main()
{
int userOption;
int ch;
printf("\33[36mWelcome to the list Software.\n"); ///\33[36m has been used to Make text colorful.It will work in linux only
printf("The list is empty please enter some values in the list.\n");
addList();
printList(currentSizeOfArray);
do
{
printf("\33[32mWhat operation you want to perform:\nPlease Choose Your option.\n");
printf("\noption\tExplanation\n");
printf("1\tView your list\n2\tAdd an element\n3\tDelete an element\n4\tFind the position of an element\n5\tExit\n");
scanf("%d",&userOption);
switch(userOption)
{
case 1:
printList(currentSizeOfArray);
break;
case 2:
printf("\33[35mPlease enter the location and Item to be added\n");
addInput();
printList(currentSizeOfArray);
break;
case 3:
printf("\n\33[35mPlease enter the position or item to be deleted\n");
deleteInput();
printList(currentSizeOfArray);
break;
case 4:
printf("\n\33[35mEnter the position or value you are willing to find\n");
findItem();
break;
case 5:
printf("\n\33[37mThanks for using this application\n\n");
return 0;
default :
printf("\n\33[31mYou entered an in valid choise\n");
}
printf("\n\33[37mWant to do some more thing on list, press 1 to continue\n");
scanf("%d",&ch);
} while(ch==1);
printf("\n\n\n");
}
void addList()
{
printf("Enter each number and then press enter. When all your elements get inserted.please enter 9876 to go to the Main Menu\n");
int i=0;
int j=maxArraySize;
for(i=0;i<j;i++)
{
scanf("%d",&arr[i]);
if(arr[i]==9876)
{
arr[i]=0;
break;
}
currentSizeOfArray++;
}
}
void printList(int x)
{
int i;
printf("\33[32mYour list is looking like this:\n\nLocation\tValue\n");
for( i=0;i<x;i++)
{
printf("%d \t\t %d \n",i,arr[i]);
}
}
void addInput()
{
int addPosition,addItem;
printf("\33[33m\nEnter the location\n");
scanf("%d",&addPosition);
if(addPosition>(currentSizeOfArray))
{
printf("\nYou can not enter at that location\n");
return;
}
printf("\nEnter the element\n");
scanf("%d",&addItem);
addElement(addPosition,addItem);
}
void addElement(int addPosition,int addItem)
{
currentSizeOfArray++;
int i,settle=currentSizeOfArray;
for(i=currentSizeOfArray;i>addPosition;i--)
{
arr[i]=arr[--settle];
}
arr[addPosition]=addItem;
}
void deleteInput()
{
int choise;
int loca,z=1;
printf("\n\33[34mPlease specify the option wheather you want to delete by Location or by Item Value\n");
printf("\noption\tExplanation\n1\tDelete by Location\n2\tDelete By value\n");
scanf("%d",&choise);
switch(choise)
{
case 1:
{
printf("Enter the location\n");
scanf("%d",&loca);
deleteByLocation(loca);
break;
}
case 2:
{
printf("\nEnter the element/value to be deleted\n");
scanf("%d",&element);
do
{
loca=findLocation(element);
if(loca==-1)
{
if(z==1)
{
printf("\33[31mThe element is not found\n");
}
break;
}
deleteByLocation(loca);
}while(1);
}
}
}
void findItem()
{
int choise,z=1;
int location;
printf("\n\33[35mPlease specify the option wheather you want to find the element by Location or by Item/element Value\n");
printf("\noption\tExplanation\n1\tFind by Location\n2\tFind By value\n");
scanf("%d",&choise);
switch (choise)
{
case 1:
printf("Enter the location\n");
scanf("%d",&location);
findByLocation(location);
break;
case 2:
printf("Enter the element\n");
scanf("%d",&element);
location=findLocation(element);
if(location==-1)
{
printf("\n\33[31mThe element is not found\n");
break;
}
findByValue(element);
break;
default :
printf("\n\33[31mYou have not chossen the correct option\n");
break;
}
}
void findByLocation(int location)
{
int i;
if(location>=currentSizeOfArray ||location<0)
{
printf("\n\33[31mThe element is not found,please checj the element or its location\n");
return;
}
for(i=0;i<currentSizeOfArray;i++)
{
if(i==location)
{
printf("\33[32msearch Sucessful,Itme %d found at %d\n",arr[i],i);
break;
}
}
}
int findLocation(int item)
{
int i;
for (i=0;i<currentSizeOfArray;i++)
{
if(item==arr[i])
return i;
}
return -1;
}
void findByValue(int elemn)
{
int i=1;
for (i=0;i<currentSizeOfArray;i++)
{
if(elemn==arr[i])
printf("\33[32mSearch Sucessful,Itme %d found at %d\n",arr[i],i);
}
}
void deleteByLocation(int location)
{
int i;
if(location>=currentSizeOfArray)
{
printf("\33[31mThe element is not found,please check the element or its location\n");
return;
}
for(i=location;i<currentSizeOfArray;i++)
{
arr[i]=arr[++location];
}
--currentSizeOfArray;
}
add to del.icio.us
saved by
0 users