473,799 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 3234
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
kingparthi
2 New Member
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...kindl y give ur suggestions on this issue...
Sep 17 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
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
7959
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' => 'aaa') , array( 'id' => 'B', 'parent_id' => 'A', 'value' => 'bbb') , array( 'id' => 'C', 'parent_id' => 'B', 'value' => 'ccc') , array( 'id' => 'D', 'parent_id' => 'A', 'value' => 'ddd')
11
3101
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
2521
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 problem I get is that I am trying to link a struct that I have defined and its refusing to link. I have tried casting my struct into char * but attempts to cast it back to its original struct to access its contents only seg faults.
12
15101
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 should be kept for references to previous element of list. Here is my solution below: struct node {
4
3606
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; };   struct pcb *pcb_pointer;
6
5335
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 splitAt to split a linked list at a node whose Data is given. Suppose you have a list with the elements
13
2100
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 properly formatted. I'll post my code below. Thanks in advance. This is the code for the linked list. It tests using int's and string's. #ifndef LIST_H #define LIST_H
1
15556
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 list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the list. Sorry if it doesn't make sense trying to explain best I can. So far I have // list.cpp
6
3264
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 read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10238
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.