R
R
Rikog2018-06-25 11:00:28
OOP
Rikog, 2018-06-25 11:00:28

How to add an element at the nth place of a doubly linked list?

I can not implement the function of adding an element to the list, each time the list is recreated and there is always 1 element
.cpp file

#include "List.h"



void List::show()
{
  Node *tmp = head;
  while (tmp != NULL)
  {
    cout << tmp->str <<endl; 
    tmp = tmp->next;
  }
}

void List::add(int x, string str2)
{
  Node *temp = new Node;
  temp->next = NULL;
  temp->str = str2;  
  temp->priority = x;

  if (head != NULL)
  {
    Node *quest = found(x);
    temp->prev = quest;
    temp->next = quest->next;
    if (quest == head)
    {
      head = temp;
    }
    if (quest == tail)
    {
      tail = temp;
    }
  }
  else
  {
    temp->prev = NULL;
    head = tail = temp;
  }
}

void List::delet()
{
  tail = tail->prev;
}

Node * List::found(int priority)
{
  if (head->next!=NULL)
  {
    if (search->priority == priority)
      return search;
    else
      return search = search->next;
  }
  else
  {
    return head;
  }
}

List::List()
{
  head = NULL;
  tail = NULL;
}


List::~List()
{
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Khayev, 2018-07-10
@hasalex

I would like to see the full code, because it is not clear how found works and what search is at each stage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question