473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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
3 3212
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
6
by: Fazana | last post by:
I was doing one of the question but my program was not working properly. Can anyone help me with it pliz........ Here is the question for the program Question. Part 1. Add the function...
13
by: B. Williams | last post by:
I have written some code to accept input and place this input at the beginning or end of a list, but I need assistance including a class so that it will allow input of a phone number that is...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.