473,779 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in adding a node in doubly linklist.

3 New Member
how can i add a node after ith node in a doubly linkedlist?(for example after the third node)
Feb 27 '07 #1
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.
Feb 27 '07 #2
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?
Feb 27 '07 #3
paeez
3 New Member
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:
Expand|Select|Wrap|Line Numbers
  1.  void insert_b4_search(int ai,int bj)
  2.     {     node *temp;
  3.         temp = head;
  4.  
  5.     while(temp != NULL && temp-> num != ai)
  6.         {temp = temp -> nxt;
  7.  
  8.         if(temp == NULL){
  9.             cout<<"not found"<<endl;
  10.             }    }
  11.  
  12.     if (temp == head){
  13.         insert_at_first(bj);//call add at first method
  14.             }
  15.     else if(temp == tail){
  16.         insert_to_tail(bj);
  17.                 }
  18.     else {     ptr = new node(bj,NULL,NULL);
  19.         ptr->prev = new node(bj, ptr->prev, ptr);
  20.         ptr->prev->prev->next = ptr->prev;
  21.             }
  22.         }
  23.  
Feb 27 '07 #4
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)
Feb 27 '07 #5

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

Similar topics

13
2784
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.
5
6062
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:
5
5506
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;
7
3140
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;
57
4303
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
8
4170
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
2
2095
by: zubia | last post by:
hi how 2 deal with the rptr n lptr in doubly linklist
3
1343
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,
2
2106
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;
0
9632
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
10302
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
10136
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 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...
0
9925
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...
0
8958
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, 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...
1
7478
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
6723
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
5372
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...
2
3631
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.