how can i add a node after ith node in a doubly linkedlist?(for example after the third node)
4 2032 RedSon 5,000
Recognized Expert Expert
Why dont you try drawing a diagram of what a doubly linked list looks like. Then try to add a node to that diagram on paper and see how it is done. Then you will know.
DeMan 1,806
Top Contributor
Have you thought about how you would approach the method?
How would you find the position in the list where you would like to add the node?
What information do you need to know about different nodes to complete the operation?
Have you attempted the problem already?
iv written the code for adding a node befor a node containing a certain value(the algorithm is the same),but it seems to have problem..
here is the code: -
void insert_b4_search(int ai,int bj)
-
{ node *temp;
-
temp = head;
-
-
while(temp != NULL && temp-> num != ai)
-
{temp = temp -> nxt;
-
-
if(temp == NULL){
-
cout<<"not found"<<endl;
-
} }
-
-
if (temp == head){
-
insert_at_first(bj);//call add at first method
-
}
-
else if(temp == tail){
-
insert_to_tail(bj);
-
}
-
else { ptr = new node(bj,NULL,NULL);
-
ptr->prev = new node(bj, ptr->prev, ptr);
-
ptr->prev->prev->next = ptr->prev;
-
}
-
}
-
DeMan 1,806
Top Contributor
you seem to be creating 2 nodes?
if we are at temp, you want to create a node
thisOne to be (bj, temp->prev, temp) //I assume it's value, before after
then (temp->prev)->next = thisOne
set temp->prev = thisOne.
I'm not sure why you are storing the bj. Assuming this is the index of the node in the list, this value will need to change every insert or delete operation (so really you should go through every otem and change the value.
Rather than rely on indexes, you can step through the array counting how many nodes you pass to arrive at the nth node
(ie node->num is not a good idea)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: ashu |
last post by:
hi to all,
Please read the following ques. first.
assume that a bank maintains 2 kinds of accounts for
customers one called as
savings accounts & other as current account.
the savings account provides simple interest &
withdraw facilities but no check book facilities.
the current account provides check book facilities but
no interest.
|
by: John N. |
last post by:
Hi All,
Here I have a linked list each containing a char and is double linked.
Then I have a pointer to an item in that list which is the current
insertion point.
In this funtion, the user hits the right
and left keys to move this insertion point (cursor)
Here is the problem:
|
by: Daniel |
last post by:
I need to reverse the doubly linked list with dummy node. I think the
solution is to exchange each node pointers' next and previous address.
But what's wrong in my function?
Thanks
void reverse_list(NodePtr p)
{ NodePtr next, q=p, head=p->prev;
while (q != head)
{ next = q->next;
q->next = q->prev;
|
by: vjay |
last post by:
I want to just create a linklist.The program below goes into an endless
loop.The srange behaviour is that i can exit from the program if i create
only two nodes.After two goes into infinite loop.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
|
by: Xarky |
last post by:
Hi,
I am writing a linked list in the following way.
struct list
{
struct list *next;
char *mybuff;
};
| |
by: sudhirlko2001 |
last post by:
How to swap two nodes of doubly Linklist
|
by: zubia |
last post by:
hi
how 2 deal with the rptr n lptr in doubly linklist
|
by: adil.mohd |
last post by:
Hi All,
This is my first to this group. I have a linklist implementation. I
want to put some checks in the implementation code
so that i can detect any problem with the list and capture some data
to prove the problem.
Any suggestion or code fragments are welcome .
Thanks,
|
by: dynamo |
last post by:
this is a basic linklist,there seems to be a runtime error when i run the program(the main function) i suspect it has something to do with my del function
but i see nothing wrong can you help.Thanx
#include <iostream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
using namespace std;
|
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...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |