|
Go4Expert Member
|
|
| 27Aug2006,06:13 | #21 |
|
I understand that. I know it's just 2 nodes behind, but in terms of making the code go 2 nodes behind, that's a little tricky. Technically, the user needs to enter the length of the node and the size of the longlink - so it could be 20 nodes and longlink is every 6.
|
|
Go4Expert Member
|
|
| 27Aug2006,06:19 | #22 |
|
Here is my code to make it better to understand (Hopefully)
Code:
node *newnode = current;
node *temp = current;
node *start = current;
int number = 0;
int num = 0;
for (int i = 1; i < length; i++) // this function is just ensuring the node before the deleted node points to 2 nodes in front
{
temp = temp -> shortlink;
}
if (newnode -> num == x)
{
number = current -> longlink -> num; // here as you can see i want to store the longlink value of the deleted node into the variable number
current = current -> shortlink;
temp -> shortlink = start -> shortlink;
delete newnode;
length--;
}
else
while (newnode -> shortlink -> num != start -> num)
{
if (newnode -> shortlink -> num == x)
{
temp = newnode -> shortlink;
number = temp -> longlink -> num; // here as you can see i want to store the longlink value of the deleted node into the variable number
newnode -> shortlink = newnode -> shortlink -> shortlink;
delete temp;
length--;
}
newnode = newnode -> shortlink;
}
if (newnode -> shortlink -> shortlink -> num == length && newnode -> shortlink -> num == x)
{
delete newnode -> shortlink -> shortlink;
newnode -> shortlink = start;
length--;
}
for (int z = 1; z < length; z++) // this function goes through each node to see if any of the long links are pointing to the deleted node
{
// if i put a cout statement exactly here to cout number - it recognises the number variable fine
if (longlinker -> longlink -> num == x) // if i've found it // once it looks into this if statement - number isn't recognised - why?
{
longlinker -> longlink -> num = number; // then i want to update the longlink of this which stores number
}
else
{
longlinker = longlinker -> shortlink; // otherwise keep iterating
}
}
|
|
Go4Expert Member
|
|
| 27Aug2006,06:48 | #23 |
|
I'm really stuck now
some of the long links of the nodes that were referncing to the delete node now have ridiculous long link numbers (like 14155566)
|
|
Go4Expert Member
|
|
| 27Aug2006,07:46 | #24 |
|
The problem i got now is the following, and i'd REALLY be grateful if someone could assist me with this. Lets say i have 6 nodes and the long link is 3, then 1 points to 2 and 4, 2 points to 3 and 5, 3 points to 4 and 6 etc .....
When i delete node 1 and 2, then nodes 4 and 5 are longlinking back to 0 (for some reason). Can someone help me please? Code is here: Code:
node *newnode = current;
node *temp = current;
node *start = current;
node *longlinker = current;
int number = 0;
for (int i = 1; i < length; i++)
{
temp = temp -> shortlink;
}
if (newnode -> num == x)
{
start = start -> shortlink;
number = current -> longlink -> num;
current = current -> shortlink;
temp -> shortlink = start;
delete newnode;
length--;
cout << "longlink for 1 is" << number << endl;
}
else
for (int p = 1 ; p < length; p++)
{
if (newnode -> shortlink -> num == x)
{
temp = newnode -> shortlink;
number = temp -> longlink -> num;
newnode -> shortlink = newnode -> shortlink -> shortlink;
delete temp;
length--;
}
else
newnode = newnode -> shortlink;
}
Last edited by coolio2006; 27Aug2006 at 08:07.. |
|
Go4Expert Founder
|
![]() |
| 27Aug2006,08:30 | #25 |
|
Still I dont get what problem you are facing. Its just the delete of a node from a link list then I have already given a code snippet to achieve the same and you can customize it to add extra information like short link and long link. Mine is just a simple where only one pointer exists and thats short link according to your problem and that is the next node.
|
|
Go4Expert Member
|
|
| 27Aug2006,08:40 | #26 |
|
Your code works fine (the sample), but that's only because each node is only linked to it's next node. My scenario is a little more complicated. Lets take this for instance, a list of 6 nodes and longlinks of 3: so the design looks like the following:
1 ----> 2( shortlink) && 1 ----> 4(longlink) 2 ----> 3( shortlink) && 1 ----> 5(longlink) 3 ----> 4( shortlink) && 1 ----> 6(longlink) 4 ----> 5( shortlink) && 1 ----> 1(longlink) 5 ----> 6( shortlink) && 1 ----> 2(longlink) 6 ----> 1( shortlink) && 1 ----> 3(longlink) Shortlinks are working fine. Now in that scenario, if i deleted node 1, that means nothing shall point to it . We must oberve though node 1 has a shortlink to 1 and a longlink to 4. Anyway, as you can see there, node 4 has a longlink to node 1, but node 1 is deleted, therefore this is wrong. So what i need to be able to do is, node 4 must point to the long link of the delete nodes long list. After deleting node 1, it should now look like 2 ----> 3( shortlink) && 1 ----> 5(longlink) 3 ----> 4( shortlink) && 1 ----> 6(longlink) 4 ----> 5( shortlink) && 1 ----> 4(longlink) ( 4's longlink must point to itself because 1's longlink was 4 5 ----> 6( shortlink) && 1 ----> 2(longlink) 6 ----> 2( shortlink) && 1 ----> 3(longlink) (notice 6's shortlink is now 2 - this works fine). My output is giving me this: 2 ----> 3( shortlink) && 1 ----> 5(longlink) 3 ----> 4( shortlink) && 1 ----> 6(longlink) 4 ----> 5( shortlink) && 1 ----> 0(longlink) (this should not be pointing to 0, it should be 4) 5 ----> 6( shortlink) && 1 ----> 2(longlink) 6 ----> 2( shortlink) && 1 ----> 3(longlink) Hopefully that clarifies my situation atm, and i'd really be grateful for some assistance. |
|
Go4Expert Member
|
|
| 27Aug2006,11:16 | #27 |
|
Ok so it's all deleting the nodes fine and updating the links, except when i go to remove the 1st node - it has a problem. Could someone please examine what i'm doing wrong.
Code:
void ring::remove(int x)
{
node *newnode = current;
node *temp = current;
node *start = current;
node *longlinker = NULL;
node *shortdata = current; // this references to the first deleted node and will need it for testing longlink functionality
int number = 0;
for (int i = 1; i < length; i++) // this for loop is used for creating the short link - for .e.g if node 4 is deleted, then node 3 points to 5 and not 3
{
temp = temp -> shortlink;
}
if (newnode -> num == x)
{
start = start -> shortlink;
//number = current -> num;
current = current -> shortlink;
//number = temp -> num;
temp = start;
newnode = start;
length--;
}
else
for (int p = 1 ; p < length; p++)
{
if (newnode -> shortlink -> num == x)
{
temp = newnode -> shortlink;
number = temp -> shortlink -> num;
newnode -> shortlink = newnode -> shortlink -> shortlink;
longlinker = newnode -> shortlink;
//delete newnode;
length--;
}
else
newnode = newnode -> shortlink;
}
for (int y = 1; y <= length; y++)
{
if (longlinker -> longlink -> num == temp -> num)
{
longlinker -> longlink -> num = temp -> longlink -> num;
}
else
longlinker = longlinker -> shortlink;
}
}
|
|
Go4Expert Member
|
|
| 27Aug2006,11:51 | #28 |
|
Anyone?
|
|
Go4Expert Founder
|
![]() |
| 27Aug2006,16:01 | #29 |
|
This sounds quite an interesting Linked list and so thought of programming it. Here is the code for you to see.
Code: CPP
|
|
Light Poster
|
|
| 18Dec2006,17:38 | #30 |
|
Hell o I am new member of this site. I am having some problems with linked lists. I want the solution for the problem described below can you help me with the code .
We have to open a new text file and do these operations in that for eg:Name and mark of a student Add Node . Delete Node - Insert Node - Same as Add Node, but takes an additional parameter of the location in which this new node is to be inserted Sort Nodes - Display Nodes - Should display the whole list in Name1 (mark), Name 2 (mark)... format. Save Nodes - This will output the curent list and save it into the same mark.txt file Exit - Exits the Application. I need it on Wednesday |

some of the long links of the nodes that were referncing to the delete node now have ridiculous long link numbers 