473,396 Members | 1,933 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,396 software developers and data experts.

unable to delete a node in double linked list

I am trying this without a head/start pointer which would generally hold the address of first node. I have 3 nodes here from which I am trying to delete last node, but its not happening. I might be wrong in my logic and this is my first linked list program, so please help me!
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct dll{
  5.             struct dll *prev;
  6.             int data;
  7.             struct dll *next;
  8.           };
  9.  
  10. int main()
  11. {
  12.     struct dll *p1,*p2,*p3, *temp;
  13.     p1=malloc(sizeof(struct dll));
  14.     p2=malloc(sizeof(struct dll));
  15.     p3=malloc(sizeof(struct dll));
  16.     temp=malloc(sizeof(struct dll));
  17.     p1->prev=NULL;
  18.     p1->data=1;
  19.     p1->next=p2;
  20.     p2->prev=p1;
  21.     p2->data=2;
  22.     p2->next=p3;
  23.     p3->prev=p2;
  24.     p3->data=3;
  25.     p3->next=NULL;  
  26.     struct dll *add=NULL;
  27.     int count=0;
  28.     printf("add of p1::%p add of p2::%p add of p3::%p add of p1->prev::%p add of p1->next::%p add of p2->prev::%p add of p2->next::%p add of p3->prev::%p add of p3->next::%p\n",p1,p2,p3,p1->prev,p1->next,p2->prev,p2->next,p3->prev,p3->next);
  29.     while(p1->next!=NULL)
  30.     {
  31.         count++;
  32.         p1=p1->next;
  33.     }
  34.     printf("no of nodes %d\n",count+1);
  35.     puts("enter the addresss of node to delete it");
  36.     scanf("%p",&add);
  37.     while(p1->next!=NULL)
  38.     {
  39.         if(p1->next==add)
  40.         {
  41.             temp = p1->next;
  42.             p1->next=NULL;
  43.             free(temp);
  44.             temp=NULL;
  45.         }
  46.         else
  47.         p1=p1->next;
  48.     }
  49.     puts("after deletion attempted");
  50. printf("add of p1::%p add of p2::%p add of p3::%p add of p1->prev::%p add of p1->next::%p add of p2->prev::%p add of p2->next::%p add of p3->prev::%p add of p3->next::%p\n",p1,p2,p3,p1->prev,p1->next,p2->prev,p2->next,p3->prev,p3->next);
  51.  
  52.     while(p1->next!=NULL)
  53.     {
  54.         count++;
  55.         p1=p1->next;
  56.     }
  57.     printf("no of nodes %d\n",count+1);
  58.     free(p1);
  59.     p1=NULL;
  60.     free(p2);
  61.     p2=NULL;
  62.     free(p3);
  63.     p3=NULL;
  64. return 0;
output::::

add of p1::0x9605008 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::(nil) add of p1->next::0x9605018 add of p2->prev::0x9605008 add of p2->next::0x9605028 add of p3->prev::0x9605018 add of p3->next::(nil)

no of nodes 3

enter the addresss of node to delete it
0x9605028

after deletion attempted

add of p1::0x9605028 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::0x9605018 add of p1->next::(nil) add of p2->prev::0x9605008 add of p2->next::0x9605028 add of p3->prev::0x9605018 add of p3->next::(nil)

no of nodes 3

In this example i am trying to delete the node 3 which is p3, by deleting its address 0x9605028. But after deletion the node count is still 3 and addresses are like unexpected!
Aug 22 '14 #1

✓ answered by weaknessforcats

Using a double linked list, one with next/prev pointers, should allow you to delete any node.

Assume p1 is a pointer to the last node. That is, p1->next is 0.

To delete this node, set p1->prev->next to p1->next, free the p1 members and then free p1.

To delete a node in the middle of the list, let's call it cur for the current node, you must:
1) set cur->prev->next to cur->next
2) set cur->next->prev to cur->prev->next

The cur node is now out of the list. free the members ansd then free the node.

1 1214
weaknessforcats
9,208 Expert Mod 8TB
Using a double linked list, one with next/prev pointers, should allow you to delete any node.

Assume p1 is a pointer to the last node. That is, p1->next is 0.

To delete this node, set p1->prev->next to p1->next, free the p1 members and then free p1.

To delete a node in the middle of the list, let's call it cur for the current node, you must:
1) set cur->prev->next to cur->next
2) set cur->next->prev to cur->prev->next

The cur node is now out of the list. free the members ansd then free the node.
Aug 24 '14 #2

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

Similar topics

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; }; ...
2
by: PRadyut | last post by:
in this code the delete function does not delete the last node of the double linked list the code ---------------------------------------------------------------- #include <stdio.h>...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
1
by: PhilB | last post by:
I have been having issues trying to merge sort a double linked list. (I would supply the code, but it is locked away on an inaccessable computer.) The list always results in an unsorted list, but...
4
by: peo_leo | last post by:
I have a simple implementation of double linked list this code is crashing if i enter values not present in the linked list(during insertion) or if i try to delete values not present in the list...
7
nabh4u
by: nabh4u | last post by:
hi, i have a double linked list containing some elements and i have a vector which stores the address of the elements in the list (pointer to the list). i want to delete a value from the list,like...
2
nabh4u
by: nabh4u | last post by:
hi friends, i have a program where i have to sort a double linked list using merge sort. i am having problem with changing the pointers of the list, like the previous and the next pointers of a...
9
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no...
3
by: kingparthi | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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,...

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.