I have created a simple Linked List. The list stores a username, a IP address associated with the username, and a socket associated with the username. Most of the functions I have created for it work fine except the del() function. I wanted this function to delete a node, but it doesn't delete the node. Could someone show me where I went wrong?
Using OpenWatcom compiler on Windows XP.
Here is the code:
#ifndef LIST_H
#define LIST_H
struct user
{
char uname[50];
char address[15];
int socket;
user *next;
};
user *head_ptr;
user *current_ptr;
void move_current_to_end()
{
current_ptr=head_ptr;
while(current_ptr->next!=NULL)
{
current_ptr=current_ptr->next;
}
}
void add_user(char *name, char *addr, int sock)
{
user *new_rec_ptr;
new_rec_ptr=new user;
strcpy(new_rec_ptr->uname,name);
strcpy(new_rec_ptr->address,addr);
new_rec_ptr->socket=sock;
move_current_to_end();
current_ptr->next=new_rec_ptr;
}
void disp()
{
current_ptr=head_ptr;
cout<<"--USERS--ONLINE--\n";
do
{
cout<<"-----------------------\n";
cout<<"username: "<<current_ptr->uname<<endl;
cout<<"IP: "<<current_ptr->address<<endl;
cout<<"socket: "<<current_ptr->socket<<endl;
cout<<"-----------------------\n";
}while(current_ptr!=NULL);
}
void del(char *name)
{
//user *temp_ptr;
current_ptr=head_ptr;
while(current_ptr!=NULL)
{
if(strcmp(current_ptr->uname,name)==0)
{
delete [] current_ptr;
//current_ptr=temp_ptr;
break;
}
else
{
current_ptr=current_ptr->next;
}
}
}
int search(char *name)
{
current_ptr=head_ptr;
while(current_ptr != NULL)
{
if(strcmp(current_ptr->uname,name)==0)
{
return -1;
}
else
{
current_ptr=current_ptr->next;
}
}
return -2;
}
#endif