Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream><pre>
#include<cstdio>
#include<cstdlib>

/*
 * Node Declaration
 */
  
 
using namespace std;
struct node
{
    int info;
    struct node *next;
    struct node *prev;
}*start;



class double_llist
{
    public:
        void create_list(int value);
        void Insrt_atFirst(int value);
        void Insrt_end(int value);
		void Insert_Afterx(int value, int position);
        void Remove_atBeginning(int value);
        void Remove_end(int value);
        void Remove_aftervaluex(int value );
		void display_allItems();
		void search();
        
        
        double_llist()
        {
            start = NULL;  
        }
};



 int main()
{
    int choice, element, position;
    double_llist dl;
    while (1)
    {
        cout<<endl<<"----------------------------"<<endl;
 cout<<endl<<"operations="" on="" doubly="" linked="" list"<<endl;
="" cout<<endl<<"----------------------------"<<endl;="" 
="" cout<<"1.create="" node"<<endl;
="" cout<<"2.insert="" the="" item="" at="" beginning"<<endl;
="" cout<<"3.insert="" end"<<endl;
="" cout<<"4.insert="" after="" value="" x"<<endl;
="" cout<<"5.remove="" from="" cout<<"6.remove="" cout<<"7.remove="" cout<<"8.display"<<endl;
="" cout<<"9.search="" for="" an="" x="" item"<<endl;
="" cout<<"10.exit"<<endl;
="" cout<<"enter="" your="" choice="" :="" ";
="" cin="">>choice;
        switch ( choice )
{
        case 1:
            cout<<"Enter the element: ";
            cin>>element;
            dl.create_list(element);
            cout<<endl;
 break;
="" case="" 2:
="" cout<<"enter="" the="" element:="" ";
="" cin="">>element;
            dl.Insrt_atFirst(element);
            cout<<endl;
 break;
="" 
="" case="" 7:
="" if="" (start="=" null)
="" {="" cout<<"list="" empty,nothing="" to="" delete"<<endl;="" }
="" cout<<"enter="" the="" element="" for="" deletion:="" ";
="" cin="">>element;
            dl.Remove_aftervaluex(element);
            cout<<endl;
 break;
="" case="" 8:
="" dl.display_allitems();
="" cout<<endl;
="" 
="" 10:
="" exit(1);
="" default:
="" cout<<"wrong="" choice"<<endl;
="" }
="" return="" 0;
}

="" *
="" *="" create="" double="" link="" list
="" void="" double_llist::create_list(int="" value)
{
="" struct="" node="" *s,="" *temp;
="" temp="new(struct" node);="" temp-="">info = value;
    temp->next = NULL;
    if (start == NULL)
    {
        temp->prev = NULL;
        start = temp;
    }
    else
    {
        s = start;
        while (s->next != NULL)
            s = s->next;
        s->next = temp;
        temp->prev = s;
    }
}
void double_llist::Insrt_atFirst(int value)
{
    if (start == NULL)
    {
        cout<<"First Create the list."<<endl;
 return;
="" }
="" struct="" node="" *temp;
="" temp="new(struct" node);
="" temp-="">prev = NULL;
    temp->info = value;
    temp->next = start;
    start->prev = temp;
    start = temp;
    cout<<"Element Inserted"<<endl;
}
void double_llist::insrt_end="" (int="" value)
{

="" if="" (start="=" null)
="" {
="" cout<<"first="" create="" the="" list."<<endl;
="" return;
="" }
="" struct="" node="" *tmp,="" *q;
="" q="start;
" 
="" tmp="new(struct" node);
="" tmp-="">info = value;
    if (q->next == NULL)
    {
        q->next = tmp;
        tmp->next = NULL;
        tmp->prev = q; 
     }
 
/*
 * Insertion of element at a particular position
 */
void double_llist::Insert_Afterx(int value, int pos)
{

    if (start == NULL)
    {
        cout<<"First Create the list."<<endl;
 return;
="" }
="" struct="" node="" *tmp,="" *q;
="" int="" i;
="" q="start;
" for="" (i="0;i" <="" pos="" -="" 1;i++)
="" {
="">next;
        if (q == NULL)
        {
            cout<<"There are less than ";
            cout<<pos<<" elements."<<endl;
="" return;
="" }
="" tmp="new(struct" node);
="" tmp-="">info = value;
    if (q->next == NULL)
    {
        q->next = tmp;
        tmp->next = NULL;
        tmp->prev = q;      
    }
    else
    {
        tmp->next = q->next;
        tmp->next->prev = tmp;
        q->next = tmp;
        tmp->prev = q;
    }
    cout<<"Element Inserted"<<endl;
}
void double_llist::remove_atbeginning(int="" value)
{
="" struct="" node="" *tmp,="" *q;
="" *first="" element="" deletion*="" 
="" if="" (start-="">info == value)
    {
        tmp = start;
        start = start->next;  
        start->prev = NULL;
        cout<<"Element Deleted"<<endl;
 free(tmp);
="" return;
="" }
="" void="" double_llist::remove_end(int="" value)
="" struct="" node="" *tmp,="" *q;
="" q="start;
" while="" (q-="">next->next != NULL)
    {   
        /*Element deleted in between*/
        if (q->next->info == value)  
        {
            tmp = q->next;
            q->next = tmp->next;
            tmp->next->prev = q;
            cout<<"Element Deleted"<<endl;
 free(tmp);
="" return;
="" }
="" q="q-">next;
    }
    void Remove_aftervaluex(int value );
   struct node *tmp, *q; 
    /*last element deleted*/
    if (q->next->info == value)    
    { 	
        tmp = q->next;
        free(tmp);
        q->next = NULL;
        cout<<"Element Deleted"<<endl;
 return;
="" }
="" cout<<"element="" "<<value<<"="" not="" found"<<endl;
}

void="" double_llist::="" search()
="" 
{="" 
struct="" node="" *ptr;="" 
int="" item,i="0,flag;" 
ptr="head;" 
if(ptr="=" null)="" 
cout<<"\nempty="" list\n";="" 
}="" 
else="" 
cout<<"\nenter="" item="" which="" you="" want="" to="" search?\n";="" 
cin="">>item; 
while (ptr!=NULL) 
{ 
if(ptr->data == item) 
{ 
cout<<"\nitem found at location %d ",i+1; 
flag=0; 
break; 
} 
else 
{ 
flag=1; 
} 
i++; 
ptr = ptr -> next; 
} 
if(flag==1) 
{ 
cout<<"\nItem not found\n"; 
} 
} 
}


 void double_llist::display_dlist()
{
    struct node *q;
    if (start == NULL)
    {
        cout<<"List empty,nothing to display"<<endl;
 return;
="" }
="" q="start;
" cout<<"the="" doubly="" link="" list="" is="" :"<<endl;
="" while="" (q="" !="NULL)
" {
="" cout<<q-="">info<<" <-> ";
        q = q->next;
    }
    cout<<"NULL"<<endl;
}
}< pre="">

What I have tried:

i tried to change the declaration <pre lang="c++">

also i think the Insrt_end is wrong and search method
Posted
Updated 25-Oct-19 22:16pm
v2

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
C++ standard library provides an implementation of the double linked list, see list - C++ Reference[^]. So there should be no reason, but exercise, to code your own.
Assuming it is an exercise, I provide you a starting point:
C++
#include <iostream>
using namespace std;

class DoubleList
{
  struct Node
  {
    int data;
    Node * next;
    Node * prev;
  };

  Node * head;
public:
  DoubleList();
  ~DoubleList();
  void push_front( int data);
  friend ostream & operator <<  (ostream & os, const DoubleList & dl);
};


void DoubleList::push_front( int data )
{
  Node * new_node = new Node;
  new_node->data = data;

  if ( ! head )
  {
    new_node->next = new_node->prev = new_node;
  }
  else
  {
    new_node->next = head;
    head->prev = new_node;

    Node * tmp_node = head;
    while ( tmp_node->next != head) { tmp_node = tmp_node->next; }
    tmp_node->next = new_node;
    new_node->prev = tmp_node;
  }
  head = new_node;
}

DoubleList::DoubleList(): head(nullptr ){}

DoubleList::~DoubleList()
{
  if  (! head) return;
  Node * tmp_node = head;
  do
  {
    Node * del_node = tmp_node;
    tmp_node = tmp_node->next;
    delete del_node;
  } while ( tmp_node != head );
}

ostream & operator <<  (ostream & os, const DoubleList & dl)
{
  DoubleList::Node * tmp_node = dl.head;
  if ( tmp_node )
  {
    do
    {
      os << tmp_node->data << ", ";
      tmp_node = tmp_node->next;
    } while ( tmp_node != dl.head );
  }
  return os;
}

int main()
{
  DoubleList dl;
  dl.push_front(1);
  dl.push_front(2);
  dl.push_front(-5);
  dl.push_front(1000);

  cout << "items { " << dl << "}" << endl;
}
 
Share this answer
 

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