R
R
Rikog2018-06-25 08:42:29
OOP
Rikog, 2018-06-25 08:42:29

Lists and queues, their implementation?

According to the assignment, you need to implement a priority queue in the form of a list. There was a problem displaying items. header file

#include <iostream>
#include <string>

using namespace std;

struct Node
{
    string str;
    Node *next;
    Node *prev;
    int priority;
};

class List
{
    Node *head;
    Node *tail;
    Node *temp;
public:
    void show();
    void add(int x, string str);
    void delet();
    Node *found(int priority);
    List();
    ~List();
};

.cpp file
#include "List.h"



void List::show()//в этой функции постоянно вылетает исключение
{
  Node *tmp = head;
  while (tmp!=NULL)
  {
    cout << tmp->str<<endl;
    if (tmp->next == NULL)
    {
      break;
    }
    else
    {
      tmp = tmp->next;
    }
  }
}

void List::add(int x, string str2)
{
  Node *temp = new Node;
  temp->priority = x;
  temp->str = str2;
  if (head != NULL)
  {
    temp->prev = tail;
    tail->next = temp;
    tail = temp;
  }
  else
  {
    temp->prev = NULL;
    head = tail = temp;
  }
}

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

//Node * List::found(int priority)
//{
//	if (temp->priority == priority)
//		return temp;
//	else
//		return temp = temp->next;
//}

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


List::~List()
{
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-06-25
@res2001

Zero out temp->next in the add() function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question