By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

Double linked list - Remove node when node contents/value given

P: 2
This is a program to add, display & delete an item from the double linked list ...
Here the add & display works correctly... where my delete is having some
problem, when ever I delete an item, it also deletes the tails..
kindly can someone help out...



Here is my data types used in my program
Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3. char data[MAX_PRID_LEN];
  4. int OidLen;
  5. }Data_Type;
  6.  
  7. //structure for global data
  8. typedef struct TAG_DATA
  9. {
  10.     Data_Type Data;
  11.     int Status;
  12.     struct TAG_DATA *next;
  13.     struct TAG_DATA *prev;
  14. }__stGData;
  15.  
  16.  
  17. typedef struct TAG_LIST
  18. {
  19.     __stGData *head;
  20.     __stGData *tail;
  21.     int count;
  22. }__stGList;
  23.  
  24. __stGList g_PridList;
  25.  
  26.  
  27. // my main() function
  28. {
  29.     GData=(__stGData *)malloc(sizeof(__stGData));
  30.     len=sizeof(int);
  31.     printf("\nEnter the Prid value: ");
  32.     scanf("%d",&prid);
  33.  
  34.     memcpy(GData->Data.data,&prid,len);
  35.     GData->Data.OidLen = len;
  36.     res=AddtoGList(&g_PridList, GData);
  37.     if(res!=0)
  38.     {
  39.       printf("Adding created PRID to list failed");
  40.       free(GData);
  41.     }
  42.     DisplayPridGList(g_PridList);
  43.  
  44.     printf("\nEnter the Prid value u wish to remove: ");
  45.     scanf("%d",&prid);
  46.     memcpy(GData->Data.data,&prid,(len));
  47.  
  48.    res =RemovefromGList(&g_PridList, GData);
  49. }
  50.  
  51.  
  52. //Function to add element
  53. {   __stGData *temp=list->head;
  54.     if (list->head == NULL)
  55.     {
  56.         /* no list items right now */
  57.         list->head = list->tail=GData;
  58.         GData->next=NULL;
  59.         GData->prev=NULL;
  60.     }
  61.     else
  62.     {
  63.         while(temp->next!=NULL)
  64.         {
  65.             temp=temp->next;
  66.         }
  67.         /* there are one or more item */
  68.         temp->next = GData;
  69.         GData->prev=temp;
  70.         GData->next=NULL;
  71.         list->tail=GData;
  72.     }
  73.     /* increment number of items */
  74.     list->count ++;
  75. }    
  76.  
  77. //Function to display the list elements
  78. {
  79.   __stGData *temp=list.head;
  80.     int i=1,j,val;
  81.         while(temp)
  82.         {
  83.             printf("%d>",i);
  84.             memcpy(&val,temp->Data.data,temp->Data.OidLen);
  85.             printf("%d",val);
  86.             i++;
  87.             temp=temp->next;
  88.             printf("\n");
  89.         }
  90. }    
  91. // Function to remove 
  92. int RemovefromGList(__stGList *list,__stGData *GData)
  93. {
  94.      int printval;
  95.     __stGData *temp=list->head;
  96.     if(list->head==NULL)
  97.     {
  98.         printf("Global Table is Empty");
  99.         return -1;
  100.     }
  101.     if(temp->next==NULL)
  102.     {
  103.         if(strncmp(temp->Data.data,GData->Data.data,temp->Data.OidLen)==0)
  104.         {
  105.             temp->prev=NULL;
  106.             temp->next=NULL;
  107.             free(temp);
  108.             list->head=NULL;
  109.         }
  110.     }
  111.     else
  112.     {
  113.     while(temp)
  114.         {
  115.         if(strncmp(temp->Data.data,GData->Data.data,temp->Data.OidLen)==0)
  116.          {
  117.            if(temp->prev == NULL)
  118.             {
  119.         list->head=temp->next;
  120.                     list->head->prev=NULL;
  121.              }
  122.                 else
  123.                 {
  124.                     temp->prev->next=temp->next;
  125.                 }
  126.                 if(temp->next == NULL)
  127.                 {
  128.                     list->tail=temp->prev;
  129.                 }
  130.                 else
  131.     {
  132.                     temp->next->prev=temp->prev;
  133.     }
  134.     break;    
  135.             }
  136.             temp=temp->next;
  137.         }
  138.     }
  139.     list->count--;
  140.     return 0;
  141. }
Sep 16 '08 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You have way, way too much code in your remove function.

Your linked list has only one scenario

A--->B--->C
A<---B<---C

To remove B:
1) Set B->prev->next to B-->next
Note: B->prev->next is A and B->next is C so you are setting A->next to C

2) Set B->next->prev to B->prev
Note: B->next->prev is C->prev and B->prev is A so you are setting C->prev to A

3) Node B is disconnected. Disconnecting A or C follows the same pattern but a nex/prev pointer will be zero, so skip that part.

This is about 5 lines of code.
Sep 16 '08 #2

P: 2
Sir,
Obviously I am doing the same wht you have said... already.. please do look at the code..
//============================================
if(temp->prev == NULL)
{
list->head=temp->next;
list->head->prev=NULL;
}
else
{
temp->prev->next=temp->next;
}
if(temp->next == NULL)
{
list->tail=temp->prev;
list->tail->next=NULL;
}
else
{
temp->next->prev=temp->prev;
}
free(temp);
//========================================

Here the problem is I need to find the exact node to remove.... so in a while loop I am iterating to compare the node contents... and then removing..
by doing this even when I give a node content which not existing.. the tail of list gets altered...kindly give ur suggestions on this issue...
Sep 17 '08 #3

Banfa
Expert Mod 5K+
P: 8,916
In your code to remove an item you decrement list->count regardless of whether you actually did or didn't remove an item.

In your code to insert an item you needlessly use a loop to find the final item in the list when you could use list->tail.
Sep 17 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.