Click here to Skip to main content
15,917,321 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include<iostream>
#include<cstring>
using namespace std;

struct node
{
    int a;
    node *previous;
    node *next;
};

 struct node *head=NULL;
 struct node *tail=NULL;

int insert_in_first()
{
    node *tmp=new node;
    cout<<"\ninput a.\n";
    cin>>tmp->a;
        if(head==NULL)
        {
            tmp->next=NULL;
            tmp->previous=NULL;
            head=tmp;
            tail=head;
        }
        else
        {
            tmp->next=head;
            tmp->previous=NULL;
            head->previous=tmp;
            head=tmp;
        }
        return 0;
}

int traverse_form_head()
{
    node *tmp=new node;
    tmp=head;
    if (tmp==NULL)
    {
        cout<<"\nnothing for traverse\n";
        return 0;
    }
    while(tmp!=NULL)
    {
        cout<<"a="<<tmp->a<<"\n";
        tmp=tmp->next;
    }
    return 0;
}


int traverse_form_tail()
{
    node *tmp=new node;
    tmp=tail;
    if (tmp==NULL)
    {
        cout<<"\nnothing for traverse\n";
        return 0;
    }
    while(tmp!=NULL)
    {
        cout<<"a="<<tmp->a<<"\n";
        tmp=tmp->previous;
    }
    return 0;
}

int insert_at_last()
{
    node *tmp=new node;
    node *tmp1=new node;
    tmp=tail;
    if (tmp==NULL)
    {
        cout<<"\nthere is no element\n";
        return 0;
    }
    else
    {
        cout<<"\nimput a\n";
        cin>>tmp1->a;
        tmp1->next=NULL;
        tmp1->previous=tmp;
        tmp->next=tmp1;
        tmp=tmp1;
    }
    return 0;
}


int main()
{
    int i;
    do
    {
        cout<<"\nimput 1 for first insert\ninput 2 for traverse from head\ninput 3 for traverse from tail\ninput 4 for last insert\ninput 9 for break\n";
        cin>>i;
        switch(i)
        {
            case 1:
            insert_in_first();
            break;
            case 2:
            traverse_form_head();
            break;
            case 3:
            traverse_form_tail();
            break;
            case 4:
            insert_at_last();
            break;

            case 9:
            break;

        }
    }
    while (i!=9);
    return 0;
}

I input 3 node which first 2 node is for first insert and last node is for last insert .then i show traverse form head and it was right but when i show traverse form tail there was a missing of last node. why?
Posted
Updated 16-Aug-13 9:57am
v3
Comments
Sergey Alexandrovich Kryukov 16-Aug-13 16:03pm    
Did you try to use the debugger before asking for help? It would make it quite clear.
—SA

You seem to be under the impression that you have to allocate a new object with "new" to initialize a pointer:
C++
int insert_at_last()
{
    node *tmp=new node; // <------ this is wrong
    node *tmp1=new node;
    tmp=tail;

You just wanted to write:
C++
int insert_at_last()
{
    node *tmp=tail;
    node *tmp1=new node;

But even that is unneccessary. You have already a tail pointer. Why assign it to a tmp pointer?

The next lines of code are also wrong:
C++
if (tmp==NULL)
{
    cout<<"\nthere is no element\n";
    return 0;
}

If tmp (which points to the tail node in that case) is NULL the list is empy. Why can't you insert a node into an empty queue.

My suggestion: Compare the function insert_at_first and insert_at_last. They should be totally symmetrical. All else in your double linked list is symmetrical, so should the code for insert at the begin and end of the list be.
 
Share this answer
 
Comments
Rumy Hasan 16-Aug-13 16:49pm    
thanks
nv3 16-Aug-13 17:44pm    
You are welcome.
pasztorpisti 16-Aug-13 17:07pm    
5.
nv3 16-Aug-13 17:44pm    
Thank you!
If you don't know what debugging means and how to use a debugger then start learning about about it with highest priority. If you are working on windows then use Visual Studio[^] as your IDE (the linked express version is free) - its the best C++ dev env on Earth in my opinion with a superb code editor and debugger integrated together. If you are on linux then I recommend using Code::Blocks[^] as your IDE - this one is a very good, free, opensource, multiplatform software supporting many compilers on many platforms (including gcc).

Install one of the previous IDEs and search for a debugging tutorial for the IDE of your choice. In order to be able to choose a good tutorial I describe in a nutshell the most useful things involved in debugging:
You use the debugger by marking some of the code lines with so called "breakpoints" before starting your program. If the execution of your program runs on one of these breakpoints or a fatal error happens (crash) then the debugger suspends the executions of your program and allows you to inspect/modify the state of your program/variables.
The most important views when checking out things:
- Call stack: shows the stack of function calls you are executing on the current thread
- watch/autos view/tooltip: helps you to read/modify the value of global/local/member variables

When you are done with checking out the state of your program you can issue the following operations:
- continue program execution until another breakpoint is hit.
- you can tell the debugger to execute a "single line of code" by entering functions (step in) or without entering them (step over)

Other hints:
- A good debugger allows you to pause program execution at any time but this is not very useful, usually you want to stop only at specific points marked with breakpoints.
- In a good debugger you are allowed to place breakpoints anytime but this is usually (and practically) done before starting the debugged application or when the application is suspended because of hitting a breakpoint.

Many people are lazy to discover the unknown like the usage of a debugger. Its a pity that I have used neither an IDE nor a debugger in the first 1-2 years of my (assembly) programming journey (because I didn't know that such thing exists). Using an IDE and a debugger shortens your learning curve radically, helps in understanding whats goin on inside a running program/process, and you don't have to go to forums to ask people to debug out the reason of your simple/obvious crashes. Finding the reason for a crash if often impossible without a debugger while the same crash is often very-very easy to catch using a debugger.

Your next steps to take: Install and setup/befriend one of the IDEs I've mentioned and then learn how to debug in it.
 
Share this answer
 
Comments
nv3 16-Aug-13 17:48pm    
How true! 5. In my first programming years we still had to read hex dumps and we would have given an arm and leg to get hands on one of the symbolic debuggers we have today.
pasztorpisti 16-Aug-13 17:56pm    
Thank you! It was fun coding even without assembler/debugger just by writing byte code. Especially because I wasn't in haste because of the needs of businessmen....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900