473,654 Members | 3,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deleting a node from linked list based on the pointer from the vector

nabh4u
62 New Member
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 the nth value enetered in the list, in constant time. how do i do that?
i got the address of the element to be deleted from the vector but how do i relate that address with the element in the list and then delete it and rearrange the pointers for the previous and next elements in the list?
can i get a sample code for tht?
thank you very much in advance...
Mar 10 '07 #1
7 2237
DeMan
1,806 Top Contributor
If I understand your question correctly: you have a doubly linked list, and you have a Vector storing a reference to each element in the list in order....
The Vector is presumably added so that you can make this removal in constant time.....
There are several ways to approach this, but I think the easiest is (where Vector is v, and I assume you have methods to set next pointer and previous pointer of a node and a remove function in your Vector. (This is not necesasarily accurate c code but should give you an idea of the process)

Expand|Select|Wrap|Line Numbers
  1. (*v[n+1]).prevElement(*v[n-1]);  /* Set element after n to point befor en */
  2. (*v[n-1]).nextElement(*v[n+1]); /* Set element pefore n to point after n */
  3. v.remove(n);
  4.  
This of course assumes that the vector n can do access operations in constant time (an array implementation certainly could, but most vector implementations I've seen would do this in linear time).
Mar 10 '07 #2
nabh4u
62 New Member
If I understand your question correctly: you have a doubly linked list, and you have a Vector storing a reference to each element in the list in order....
The Vector is presumably added so that you can make this removal in constant time.....
There are several ways to approach this, but I think the easiest is (where Vector is v, and I assume you have methods to set next pointer and previous pointer of a node and a remove function in your Vector. (This is not necesasarily accurate c code but should give you an idea of the process)

Expand|Select|Wrap|Line Numbers
  1. (*v[n+1]).prevElement(*v[n-1]);  /* Set element after n to point befor en */
  2. (*v[n-1]).nextElement(*v[n+1]); /* Set element pefore n to point after n */
  3. v.remove(n);
  4.  
This of course assumes that the vector n can do access operations in constant time (an array implementation certainly could, but most vector implementations I've seen would do this in linear time).
you got my point but i didnt really understand what you meant to say. how can we access a function with vectors? and what does the remove function consist of? can you give me an example of the remove function.
one more point - if the user enters like delete the first element entered in the list, that means the first value entered in the vector which will be at index 0. now if we use (*v[n+1]).prevElement(* v[n-1]);
(*v[n-1]).nextElement(* v[n+1]);
then n-1 will be -1 which is not acceptable..
please give some sample code
Mar 10 '07 #3
DeMan
1,806 Top Contributor
OK....

Not sure what you meabn by accessing a function with Vectros, but the idea of Object Oriented Programming is that given an object you can do things to it because it is that object (so if you know that v is a pointer to an object whose definition includes a toString method, you know that (*v).toString() will call that mehtod)

The remove function (presumably) is defined by the Vector class..... the specific implementation of this class will actually determine whether the operation is constant or linear (if you have to implement the Vector class, I would suggest an Array in this instance)

Good point....
You should probably check where your pointers are in the linked list (although since you have the Vector, this should be reasonably trivial (0 and (Size -1) should eb the tricky ones -> and you can make a special case for that))
Mar 11 '07 #4
nabh4u
62 New Member
OK....

Not sure what you meabn by accessing a function with Vectros, but the idea of Object Oriented Programming is that given an object you can do things to it because it is that object (so if you know that v is a pointer to an object whose definition includes a toString method, you know that (*v).toString() will call that mehtod)

The remove function (presumably) is defined by the Vector class..... the specific implementation of this class will actually determine whether the operation is constant or linear (if you have to implement the Vector class, I would suggest an Array in this instance)

Good point....
You should probably check where your pointers are in the linked list (although since you have the Vector, this should be reasonably trivial (0 and (Size -1) should eb the tricky ones -> and you can make a special case for that))
thank you very much
Mar 11 '07 #5
DeMan
1,806 Top Contributor
Incidently (I was thinking about this last night)....The remove operatiobns is constant time in the Double Linked List, but I'm noit sure that the removal of an eleemnt in the Vector would be, so a puritan would probably say that this operation is not done in constant tim,e......
Mar 11 '07 #6
nabh4u
62 New Member
Incidently (I was thinking about this last night)....The remove operatiobns is constant time in the Double Linked List, but I'm noit sure that the removal of an eleemnt in the Vector would be, so a puritan would probably say that this operation is not done in constant tim,e......
i have resolved the problem of deleting....
now i have 2 vectors and first vector consists of the list values to be sorted. the second vector is the final vector where the sorted values will be there. while sorting i have to count inversions (eg. list has 2 1 then on sorting we get 1 2 and number of inversions will be 1. while if the list is 1 1 3 then the number of inversions will be zero.). while using the recurrsive function i want the first vector to be updated with the second vector values at same positions.
eg: if first vector [1 3 1]
second vector [1 1 3]. so i want the first vector to take d values of the second vector.

please suggest some solution...

thanks
Mar 13 '07 #7
DeMan
1,806 Top Contributor
There are loads of approaches to this problem (and you may do well to google something like "sort c" or one of the many different sorting algorithms (do you maybe need to use a particular one, or is any reasonable?)

one (longish approach) would be to iterate through the whole vector to find the lowest value, then the next lowest etc....

Slightly more efficient in the average case (but at its' simplest equally bad in the worst case), you take the first value of Vector1 and store it in Vector2, then you take the next value in Vector1 and work out where it goes in Vector2.
To make this algorithm a little more efficient, you can use a binary search approach, to find where in the Vector2 you would need to insert the next node....To do this efficiently, you would probably need an array or something keeping track of nodes (whose overhead would counteract the saving).

If there is no requirement that you use Vectors, you could use a binary tree, which you populate from Vector1, and then traverse to populate Vector2....In the worst case (depending on how you implement the tree, but assuming you don't insist on a balanced tree (and depending on the size of the data, the overhead for a balanced tree may not be worth it), the worst case would still be as bad as the other options (n -squared I think)), but the average case would be n log n.

There are many other options also, but hopefully these give you some ideas....
Mar 13 '07 #8

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

Similar topics

10
5486
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the following: struct Node { int integer_value;
3
2627
by: Francois Grieu | last post by:
Given that next is the first field in struct node, and head is a pointer to node, does assigning ((node*)&head)->next safely assign head ? Illustration (this code works on many platforms) #include <stdlib.h> #include <stdio.h>
13
6555
by: Raj | last post by:
Is there any way to delete a particular node from a singly linked list where the header of the list is unknown.Only pointer available is the one which points to the node to be deleted
10
10223
by: Daniel Vukadinovic | last post by:
OK, here's the deal. I'd like to delete the list.Here's what I do: node* p_temp = p_beginning; while (p_beginning != 0) { p_beginning = p_beginning->p_next; delete p_beginning; }
16
11228
by: sangram | last post by:
how to delete last node of a Linked list if you only know the address of last node. thanks sangram
10
1115
by: ac.c.2k7 | last post by:
Hello Everyone, The solution to this is to copy the data from the next node into this node and delete the next node!. 1. But if the node to be deleted is the last node. Then what should we do ? 2. If the list is Head node? 3 If the list is circular then what all conditions we need to check? Thanks,
42
4545
by: Avon | last post by:
Hello there. I have a problem regarding node deletion in singly-linked lists in C. What happens is that I can't hold the previous node efficiently. Here is the code I use. struct lista *search (struct lista *list, int m, char *s, struct lista *previous) ......... Not problem-related code goes here....
2
5609
by: Subodh | last post by:
Hi All, I want to use a C++ API in a static lib that returns a linked List in C# I am planning to P/Invoke to perform the Interop, I would like to know which way will be better to interop a Linked list to C# in terms of performance, I have came across following methods I am creating a flat DLL to export the static lib functionality.
10
10497
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in between then what is the best solution.
0
8379
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
8294
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
8816
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...
0
8596
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...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
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
4150
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...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.