473,383 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

doubly link list in c++

1
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 1890
Ganon11
3,652 Expert 2GB
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 Expert Mod 8TB
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 Expert 2GB
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 Expert Mod 8TB
...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
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. ...
270
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...
3
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
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...
8
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
5
by: free2cric | last post by:
Hi, how to detect head and tail in cyclic doubly link list ? Thanks, Cric
0
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...
3
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.