Home c++ Doubly linked list

Doubly linked list

Author

Date

Category

Can you please tell me how to properly remove an element from a doubly linked list and add an element to the end of the list? See below for example code that doesn’t work as expected.

# include & lt; iostream & gt;
#include & lt; string & gt;
#include & lt; fstream & gt;
using namespace std;
int n, nom;
struct node
{
  char word [256]; // Information field.
  node * next;
  node * prev; // Address field.
};
typedef node * list;
list build_list () // Build a bidirectional list. Creation of one item.
{
  list new_list = new (node);
  cin & gt; & gt; (new_list- & gt; word);
  new_list- & gt; prev = 0;
  new_list- & gt; next = 0;
  return new_list;
}
list make_list () // Create a list.
{
  list Head, p, u;
  do
  {
    cout & lt; & lt; "\ n \ nEnter the number of entries in the list:"; cin & gt; & gt; n;
    if (n & lt; = 0) cout & lt; & lt; "There cannot be so many elements ...";
  } while (n & lt; = 0);
  if (n & gt; 0)
  {
    Head = build_list ();
    u = Head;
    for (int i = 1; i & lt; n; i ++)
    {
      p = build_list ();
      p- & gt; prev = Head;
      Head- & gt; next = p;
      Head = p;
    }
    return u;
  }
  else
    return 0;
}
void Out (list u) // Print the list.
{
  list p = u;
  if (p == 0)
  {
    cout & lt; & lt; "The list is empty ... \ n";
    return;
  }
  while (p! = 0)
  {
    cout & lt; & lt; p- & gt; word & lt; & lt; endl;
    p = p- & gt; next;
  }
}
void Delete (list & amp; Head, int nom) // Deleting the element with the given number.
{
  for (int i = 0; i & lt; n; i ++)
    if (i == nom)
    {
      Head- & gt; next = NULL;
    }
}
void addition (list & amp; Head) // Adding an item to the beginning of the list.
{
  list now;
  now = make_list ();
  now- & gt; next = Head; // The new tail looks at the old
  now- & gt; prev = NULL; // The new tail doesn't wrap around anything
  Head- & gt; prev = now; // The old tail wraps around the new one
  Head = now; // Remember the pointer to the new tail
}
void in_file (const list & amp; Head, char x []) // Writing the list to a file.
{
  ofstream u (x);
  if (x)
  {
    list p = Head;
    if (! p)
    {
      u & lt; & lt; "The list is empty ... \ n";
      return;
    }
    while (p! = 0)
    {
      u & lt; & lt; p- & gt; word & lt; & lt; endl;
      p = p- & gt; next;
    }
  }
  u.close ();
}
list inFile (ifstream & amp; f) // Restore list from file.
{
  list new_list = new (node);
  f & gt; & gt; new_list- & gt; word;
  new_list- & gt; prev = 0;
  new_list- & gt; next = 0;
  return new_list;
}
list vosstan (char u [])
{
  ifstream f (u);
  list Head = inFile (f);
  list pop = Head;
  list temp;
  while (! f.eof ())
  {
    temp = inFile (f);
    if (f.eof ())
      break;
    Head- & gt; next = temp;
    temp- & gt; prev = Head;
    Head = temp;
  }
  return pop;
}
void Delete_list (list & amp; Head) // Destroying the list.
{
  list p = Head;
  list temp;
  if (! p)
  {
    cout & lt; & lt; "The list is empty ... \ n";
    return;
  }
  while (p! = 0)
  {
    temp = p;
    p = p- & gt; next;
    delete (temp);
  }
}
int main ()
{
  setlocale (LC_ALL, "Russian");
  cout & lt; & lt; "Entries in a linear list contain a key field of type char *. \ N Generate a bidirectional list. \ N Remove the element with the given number from it, add the element to the beginning of the list." & lt; & lt; endl;
  list Head;
Head = make_list ();
  cout & lt; & lt; "Source List: \ n";
  Out (Head);
  cout & lt; & lt; "Enter the number of the element to be deleted:";
  cin & gt; & gt; nom;
  Delete (Head, nom);
  cout & lt; & lt; "List after item deletion:"; cout & lt; & lt; endl;
  Out (Head);
  cout & lt; & lt; endl;
  cout & lt; & lt; "Adding an element:"; cout & lt; & lt; endl;
  addition (Head);
  cout & lt; & lt; "List with added element:"; cout & lt; & lt; endl;
  Out (Head); cout & lt; & lt; endl;
  char x [100];
  cout & lt; & lt; "Where to write the list? (File name with extension!)"; cin & gt; & gt; x;
  in_file (Head, x); cout & lt; & lt; endl;
  cout & lt; & lt; "The list is being deleted ... \ n"; cout & lt; & lt; endl;
  Delete_list (Head);
  cout & lt; & lt; "List is empty ..." & lt; & lt; endl; cout & lt; & lt; endl;
  cout & lt; & lt; "The list is being restored ... \ n"; cout & lt; & lt; endl;
  list head = vosstan (x);
  cout & lt; & lt; "Final list:"; cout & lt; & lt; endl;
  Out (head); cout & lt; & lt; endl;
  system ("pause");
  return 0;
}

Answer 1

The removal code is very incorrect

void Delete (list & amp; Head, int nom) // Delete the element with the given number.
{
  // current node
  list current = Head;
  // looking for a position
  while (nom & gt; 0)
  {
    current = current- & gt; next;
    if (current == nullptr) throw RangeError;
    nom--;
  }
  // edit links
  if (current- & gt; prev! = nullptr) current- & gt; prev- & gt; next = current- & gt; next;
  if (current- & gt; next! = nullptr) current- & gt; next- & gt; prev = current- & gt; prev;
  // if necessary, move the head
  if (Head == curent) Head = current- & gt; next;
  // delete
  delete curent;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions