Are there any memory leaks in this linklist implementation.Also is the implementation correct?Can the time complexity be optimized?
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
struct node
{
int n;
node* point;
};
node* first=NULL;
void search(int n);
void insert(int n);
void del(int n);
void print();
void menu()
{
int s,n;
cout<<"Enter 1 to Search"<<endl;
cout<<"Enter 2 to Insert"<<endl;
cout<<"Enter 3 to Delete"<<endl;
cout<<"Enter 4 to Print"<<endl;
cout<<"Enter 5 to Exit"<<endl;
cin>>s;
switch(s)
{
case 1: cout<<"Enter number to Search: ";
cin>>n;
search(n);
break;
case 2: cout<<"Enter number to Insert: ";
cin>>n;
insert(n);
break;
case 3: cout<<"Enter number to Delete: ";
cin>>n;
del(n);
break;
case 4: print();
break;
case 5: exit(0);
default: cout<<"Wrong Input"<<endl<<endl<<endl;
}
}
int main()
{
cout<<"Welcome to Linked List"<<endl;
while(1)
menu();
}
void search(int n)
{
node* ptr=first;
while(ptr!=NULL)
{
if(ptr->n==n)
{
cout<<endl<<n<<" Found!"<<endl<<endl<<endl;
return;
}
else
ptr=ptr->point;
}
cout<<endl<<n<<" Not Found!"<<endl<<endl<<endl;
return;
}
void insert(int n)
{
node* prev=NULL;
node* next=first;
if(first!=NULL and first->n>n)
{
first=new node;
first->n=n;
first->point=next;
cout<<endl<<endl;
return;
}
while(next!=NULL)
{
if(next->n>n)
{
prev->point=new node;
(prev->point)->n=n;
(prev->point)->point=next;
cout<<endl<<endl;
return;
}
else
{
prev=next;
next=next->point;
}
}
if(prev==NULL)
{
first=new node;
first->n=n;
first->point=NULL;
}
else
{
prev->point=new node;
(prev->point)->n=n;
(prev->point)->point=NULL;
}
cout<<endl<<endl;
return;
}
void print()
{
node* next=first;
while(next!=NULL)
{
cout<<next->n<<" ";
next=next->point;
}
cout<<endl<<endl<<endl;
return;
}
void del(int n)
{
node* prev=NULL;
node* next=first;
while(next!=NULL)
{
if(next->n==n)
{
if(prev!=NULL)
{
prev->point=next->point;
delete next;
}
else
{
first=next->point;
delete next;
}
cout<<n<<" Deleted!";
cout<<endl<<endl;
return;
}
else
{
prev=next;
next=next->point;
}
}
cout<<"Number not in list"<<endl<<endl<<endl;
return;
}