i recently made a programme to implement simple hash tables in C++. i want to know if this command - ‘delete[] start’ would do the job for deleting the elements. or i have to delete each and every element individually. i am attaching my code. the code is working properly and effectively. just i have to check for memory leaks.
#include <iostream>
#include<cstdlib> //exit and EXIT_SUCCESS and system("CLS") and system("PAUSE")
using namespace std;
struct node{
int num;
node * next;
};
node * start[10], * save;
void init(); //initialises every element of - start (node *) - to NULL
void mainmenu(); //displays mainmenu
int hashgen(char *); //generates hash
void ist(); //insets elements
void view(); //displays elements
void srch(); //searches through the elements
int main()
{
init(); //every element is now NULL for the array of pointers of type 'node'
mainmenu(); //calls recursive function mainmenu()
return 0;
}
void init()
{
for(int i=0;i<10;i++)
{
start[i]=NULL;
}
}
void mainmenu()
{
system("CLS"); //clears screen
cout<<"-----------------PROGRAMME BY VARTIK---------------------\n";
int choice;
cout<<"\t1. INSERT\n\t2. VIEW CURRENT TABLE\n\t3. SEARCH TABLE\n\t4. EXIT\n";
cin>>choice;
switch(choice)
{
case 1:
{
ist();
break;
}
case 2:
{
view();
break;
}
case 3:
{
srch();
break;
}
case 4:
{
exit(EXIT_SUCCESS); //EXIT_SUCCESS is a const int
break;
}
default:
{
cout<<"wrong choice entered\n";
system("PAUSE");
}
}
mainmenu();
}
int hashgen(int num)
{
return num%10; //generates hash and returns value
}
void ist() //inserts elements
{
int n;
cout<<"enter the value to be inserted \n";
cin>>n;
int hed=hashgen(n);
save=new node; //inserts elements at the start of list
save->num=n;
save->next=NULL;
if(start[hed]==NULL)
{
start[hed]=save;
}
else
{
save->next=start[hed];
start[hed]=save;
}
cout<<"value inserted successfully\n";
system("PAUSE");
}
void view()
{
node * trav; //displays the entire list of entire indexes
for(int i=0;i<10;i++)
{
trav=start[i];
cout<<"index "<<i<<" ";
if(trav==NULL)
{
cout<<"empty cell\n"; //no value in the cell right now
continue;
}
while(trav!=NULL) //reach untill the end of the loop
{
cout<<trav->num<<"<<";
trav=trav->next;
}
cout<<endl;
}
system("PAUSE"); //wait for the user to enter a key
}
void srch() //searches the element by generating its hash
{
int n;
cout<<"enter the element to be searched\n";
cin>>n;
int hashed,found=0;
hashed=hashgen(n);
node * trav;
trav=start[hashed];
while(trav!=NULL)
{
if(trav->num==n)
{
found=1;
break;
}
trav=trav->next;
}
if(found==1)
{
cout<<"element found at index number "<<hashed<<endl;
}
else
{
cout<<"element not found\n";
}
system("PAUSE");
}
helpful advices suggestions are thanked in advance.