Home c++ How to add items to a doubly linked list?

How to add items to a doubly linked list?

Author

Date

Category

Please help finish the program. You need to write a procedure that, given as a parameter a pointer to an element of a doubly linked list of real numbers and two numbers, add the first number in the top of the list, and the second at the end. Here is my code

# include & lt; iostream & gt;
Using Namespace STD;
struct List {
  float num;
  List * next;
  List * prev;
};
List * ls = NULL;
void fill_list () {
  List * tmp, * last = NULL;
  for (int i = 0; i & lt; 11; i ++) {
    tmp = new List;
    tmp- & gt; num = i;
    tmp- & gt; next = NULL;
    if (ls == NULL)
      ls = tmp;
    ELSE.
      last- & gt; next = tmp;
    last = tmp;
  }
}
void show () {
  for (List * tmp = ls; tmp; tmp = tmp- & gt; next)
    COUT & LT; & LT; tmp- & gt; num & lt; & lt; "";
  COUT & LT; & LT; Endl;
}
void func (float a, float b) {
  List * prev = NULL, * tmp;
  for (tmp = ls; tmp; prev = tmp, tmp = tmp- & gt; next)
  {
  }
}
INT MAIN ()
{
  float a, b;
  fill_list ();
  int sw;
  While (1)
  {
    COUT & LT; & LT; "1 - show (), 2 - func (a, b), 3 - exit" & lt; & lt; Endl;
    CIN & GT; & GT; sw;
    switch (sw) {
      Case 1:
        show ();
      Case 2:
        COUT & LT; & LT; "Input a, b" & lt; & lt; Endl;
        CIN & GT; & GT; a;
        CIN & GT; & GT; b;
        func (a, b);
      Case 3:
        Return 0;
    }
  }
  Return 0;
}

fill_list – populates the list, show – displays the list, func – should add a top of the list, b at the end of the list


Answer 1, Authority 100%

Let’s first go over your mistakes in the current implementation:

  1. When completing the doubly linked list, you will never use * prev , that is, work with him as with a simply connected.
  2. The case after you perform one of the scenarios always follow these because you do not use scripts break
  3. In your implementation, you never store the beginning and end of the list, which will result in a run on all elements of the list to insert in the right place the desired value
  4. You have never handled a scenario that list may be initially empty

Here you have attached the code, which realized that you wanted to get.

Just ahead of the question, why are we in the end we add not a separate function, and the overall function of adding an item to the list of Add – because Add will always add an element to the end.
And if you are still using C++, it is not clear why reinvent the wheel when there is a ready container list

# include & lt; stdlib.h & gt;
#Include & lt; iostream & gt;
Using Namespace STD;
struct Node // structure is a list of link
{
  int x; // The value of x will be transferred to the list
  Node * Next, * Prev; // pointer to the address of the next and previous items in the list
};
Create a class List // data type List
{
  Node * Head, * Tail; // Pointer to the start address and the end of the list
Public:
  List (): Head (NULL), Tail (NULL) {}; // Initialize addresses as empty
  ~ List (); Prototype // destructor
  void show (); // Prototype list display function
  void Add (int x); // function prototype add items to the list
  void AddFirst (int x); // add a function prototype in element to the beginning
  void func (int a, int b); // prototype of the desired function you
};
void List :: func (int a, int b) {
  AddFirst (a);
  Add (b);
}
void List :: AddFirst (int x) {
  Node * temp = new Node;
  temp- & gt; Next = NULL; // indicates that initially at the following URL is empty
  temp- & gt; Prev = NULL;
  temp- & gt; x = x;
  if (Head! = NULL) {// If the list is not empty 
TEMP- & GT; next = Head; // specify the previous first element
    HEAD- & GT; PREV = TEMP; // Previous Perry Element now indicates a new first element
    Head = Temp; // reassign the first element
  }
  else {// if the list is empty
    Head = Tail = Temp;
  }
}
List :: ~ List () // Destructor
{
  While (Head) // While at the address on the top of the list something is
  {
    Tail = Head- & gt; next; // Backup Address Next List Address
    DELETE HEAD; // Cleaning memory from the first link
    Head = tail; // Selection of the address of the beginning to the address of the next item
  }
}
Void List :: Add (Int X)
{
  Node * temp = new node; // Selecting memory for a new element of the structure
  Temp- & gt; next = null; // We indicate that it is initially an empty address
  Temp- & gt; x = x; // Write the value to the structure
  if (Head! = NULL) // if the list is not empty
  {
    Temp- & gt; prev = tail; // Indicate the address to the previous element in the acc. field
    Tail- & gt; next = temp; // Indicate the address following the tail element
    Tail = Temp; // Change the address of the tail
  }
  else // if the list is empty
  {
    TEMP- & GT; PREV = NULL; // Previous element indicates void
    Head = Tail = Temp; // Head = tail = that item that now added
  }
}
Void List :: Show ()
{
  Node * temp = new node;
  // withdraw a list from the end
  / * temp = tail; // Temporary pointer to the address of the last element
  while (temp! = null) // until empty meaning
  {
    COUT & LT; & LT; TEMP- & GT; X & LT; & LT; ""; // display the value to the screen
    TEMP = temp- & gt; prev; // Indicate that the address of the previous element is needed.
  }
  COUT & LT; & LT; "\ n"; * /
  // withdraw a list from the beginning
  Temp = Head; // Temporarily indicate the address of the first element
  While (Temp! = NULL) // until you meet an empty value
  {
    COUT & LT; & LT; TEMP- & GT; X & LT; & LT; ""; // Display every read screen
    TEMP = TEMP- & GT; Next; // Change Address to the following item
  }
  COUT & LT; & LT; "\ n";
}
INT MAIN ()
{
  LIST LST; // declare a variable whose type is a list
  INT SW;
  int a, b;
  For (int i = 0; i & lt; 11; i ++) // Analogue of our Fill_List function
    LST.ADD (I);
  While (1)
  {
    COUT & LT; & LT; "1 - show (), 2 - FUNC (A, B), 3 - EXIT" & lt; & lt; Endl;
    CIN & GT; & GT; SW;
    Switch (SW) {
      Case 1:
        lst.show ();
        Break;
      Case 2:
        COUT & LT; & LT; "INPUT A, B" & LT; & LT; Endl;
        CIN & GT; & GT; a;
        CIN & GT; & GT; b;
        LST.FUNC (A, B);
        Break;
      Case 3:
        EXIT (0);
    }
  }
  SYSTEM ("PAUSE");
  Return 0;
}

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