473,769 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doubly link list in c++

1 New Member
Hi..Im writting a doubly link list in c++,compiled with Turbo c++, but my Add_at_first method seems not working,i couldnt find any problem in the code...
here is the code :
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. class node
  3. {  friend class linklist;
  4.   public :    int num;
  5.     node *next;
  6.     node *prev;
  7. };
  8. class linklist{
  9.     public :
  10.     void add_at_first();
  11.  
  12.     private :
  13.     node *first;
  14.     node *last;
  15. };
  16. //*******Add At First**
  17. void linklist :: add_at_first()
  18. {  
  19.    node *temp;
  20.    temp = new node();
  21.    temp ->prev=temp ->next=NULL;
  22.    cout <<"Please Enter A Number :"<< endl;
  23.    cin >> temp -> num;
  24.  
  25.    if(first ==last==NULL)// if the list is empty
  26.    {
  27.       first = last=temp;
  28.    }
  29.    else
  30.    {  
  31.       first -> prev =temp;
  32.       temp -> next = first;
  33.       first = temp;
  34.    }
  35. }
waitting for ur guidance, Thanksss..
Feb 22 '07 #1
4 1948
Ganon11
3,652 Recognized Expert Specialist
Well, in a doubly-linked list, you probably shouldn't need last - to find the last node, you code just use first->prev.

Next, if the list is not empty, you are adjusting first and temp - but you will need to change last (or, if you follow the first example, first->prev) to point forward to temp.
Feb 22 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
Well, in a doubly-linked list, you probably shouldn't need last - to find the last node, you code just use first->prev.
Only true if your list is circular as well as doubly linked.
Feb 27 '07 #3
Ganon11
3,652 Recognized Expert Specialist
Only true if your list is circular as well as doubly linked.
...right. I keep forgetting to distinguish the two.
Feb 27 '07 #4
Banfa
9,065 Recognized Expert Moderator Expert
...right. I keep forgetting to distinguish the two.
Ah, not all four legged animals are cows :D
Feb 27 '07 #5

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

Similar topics

1
2217
by: D. Beckham | last post by:
I wrote the following code to create a short doubly-link list of three strings: "one", "two", and "three". I would like to know if I set them up correctly, so that they point to one another. DLLNode class has already been created. Basically, "one" is the head node, "two" is the mid node, and "three" is the tail or the last node. I would like to know if my algorithm for setting up doubly-linked node are correct before I proceed to...
270
3990
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these things. Specially Microsoft. Here are my favorite puzzles. Don't send me emails asking for the solutions.
3
2211
by: surrealtrauma | last post by:
I want to ask what's the differences between doubly liked list and linear liked list, and also the circular doubly liked list in terms of implementation. THX
4
2932
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows traversal only in one direction, I would say that using appropriate loops/recursion, traversal in opposite direction is also possible. Then why the need for doubly linked list? --
8
4170
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
5
3231
by: free2cric | last post by:
Hi, how to detect head and tail in cyclic doubly link list ? Thanks, Cric
0
1823
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and each node in the list must contain two pointers, one pointing forward and one pointing backwards. Each node in the list will contain 3 data values: an item ID (string), a quantity (integer) and a price (float). The ID will contain only letters and...
3
2669
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
5
9716
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to implement the sparse matrices as a doubly-linked list, in which each non-zero cell is stored roughly as follows: int rownum int colnum
0
9579
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
9420
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
10035
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
9851
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
6662
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
5293
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
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
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.