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! - #include <stdio.h>
-
#include <stdlib.h>
-
-
struct dll{
-
struct dll *prev;
-
int data;
-
struct dll *next;
-
};
-
-
int main()
-
{
-
struct dll *p1,*p2,*p3, *temp;
-
p1=malloc(sizeof(struct dll));
-
p2=malloc(sizeof(struct dll));
-
p3=malloc(sizeof(struct dll));
-
temp=malloc(sizeof(struct dll));
-
p1->prev=NULL;
-
p1->data=1;
-
p1->next=p2;
-
p2->prev=p1;
-
p2->data=2;
-
p2->next=p3;
-
p3->prev=p2;
-
p3->data=3;
-
p3->next=NULL;
-
struct dll *add=NULL;
-
int count=0;
-
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);
-
while(p1->next!=NULL)
-
{
-
count++;
-
p1=p1->next;
-
}
-
printf("no of nodes %d\n",count+1);
-
puts("enter the addresss of node to delete it");
-
scanf("%p",&add);
-
while(p1->next!=NULL)
-
{
-
if(p1->next==add)
-
{
-
temp = p1->next;
-
p1->next=NULL;
-
free(temp);
-
temp=NULL;
-
}
-
else
-
p1=p1->next;
-
}
-
puts("after deletion attempted");
-
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);
-
-
while(p1->next!=NULL)
-
{
-
count++;
-
p1=p1->next;
-
}
-
printf("no of nodes %d\n",count+1);
-
free(p1);
-
p1=NULL;
-
free(p2);
-
p2=NULL;
-
free(p3);
-
p3=NULL;
-
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!
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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;
};
...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |