View Single Post
Old Dec 13th, 2007, 6:00 PM   #1
Brent
Highly Adaptive Penguin
 
Brent's Avatar
 
Join Date: May 2005
Location: United States
Posts: 251
Rep Power: 4 Brent is on a distinguished road
Linked List problem - Deleting a node

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:

c Syntax (Toggle Plain Text)
  1. #ifndef LIST_H
  2. #define LIST_H
  3.  
  4. struct user
  5. {
  6. char uname[50];
  7. char address[15];
  8. int socket;
  9. user *next;
  10. };
  11.  
  12. user *head_ptr;
  13. user *current_ptr;
  14.  
  15.  
  16. void move_current_to_end()
  17. {
  18. current_ptr=head_ptr;
  19.  
  20. while(current_ptr->next!=NULL)
  21. {
  22. current_ptr=current_ptr->next;
  23. }
  24. }
  25.  
  26.  
  27. void add_user(char *name, char *addr, int sock)
  28. {
  29. user *new_rec_ptr;
  30.  
  31. new_rec_ptr=new user;
  32.  
  33. strcpy(new_rec_ptr->uname,name);
  34. strcpy(new_rec_ptr->address,addr);
  35. new_rec_ptr->socket=sock;
  36.  
  37. move_current_to_end();
  38. current_ptr->next=new_rec_ptr;
  39. }
  40.  
  41.  
  42.  
  43.  
  44. void disp()
  45. {
  46. current_ptr=head_ptr;
  47. cout<<"--USERS--ONLINE--\n";
  48. do
  49. {
  50. cout<<"-----------------------\n";
  51. cout<<"username: "<<current_ptr->uname<<endl;
  52. cout<<"IP: "<<current_ptr->address<<endl;
  53. cout<<"socket: "<<current_ptr->socket<<endl;
  54. cout<<"-----------------------\n";
  55. }while(current_ptr!=NULL);
  56. }
  57.  
  58. void del(char *name)
  59. {
  60. //user *temp_ptr;
  61.  
  62. current_ptr=head_ptr;
  63.  
  64. while(current_ptr!=NULL)
  65. {
  66. if(strcmp(current_ptr->uname,name)==0)
  67. {
  68. delete [] current_ptr;
  69. //current_ptr=temp_ptr;
  70. break;
  71.  
  72.  
  73. }
  74. else
  75. {
  76. current_ptr=current_ptr->next;
  77. }
  78. }
  79.  
  80. }
  81.  
  82. int search(char *name)
  83. {
  84. current_ptr=head_ptr;
  85.  
  86. while(current_ptr != NULL)
  87. {
  88. if(strcmp(current_ptr->uname,name)==0)
  89. {
  90. return -1;
  91. }
  92. else
  93. {
  94. current_ptr=current_ptr->next;
  95. }
  96. }
  97.  
  98. return -2;
  99. }
  100.  
  101. #endif
Brent is offline   Reply With Quote