Объявил указатели на начало и конец очереди, вопрос следующий, как вообще определяется что один указатель есть начало очереди, а второй ее конец?. Попробовал распечатать очередь, ничего не вышло, но ошибки при создании очереди нет.
#include <iostream>
using namespace std;
//------------------------------------------------------------------
struct Double_List {//структура данных
int Data; //информационное поле
Double_List* Next, //адресное поле
* Prior; //адресное поле
};
//создание двунаправленного списка (добавления в конец)
void Make_Double_List(int n, Double_List** Head,
Double_List* Prior) {
if (n > 0) {
(*Head) = new Double_List();
//выделяем память под новый элемент
cout << "Введите значение ";
cin >> (*Head)->Data;
//вводим значение информационного поля
(*Head)->Prior = Prior;
(*Head)->Next = NULL;//обнуление адресного поля
Make_Double_List(n - 1, &((*Head)->Next), (*Head));
}
else (*Head) = NULL;
}
//печать двунаправленного списка
void Print_Double_List(Double_List* Head) {
if (Head != NULL) {
cout << Head->Data << "\t";
Print_Double_List(Head->Next);
//переход к следующему элементу
}
else cout << "\n";
}
//вставка элемента с заданным номером в двунаправленный список
Double_List* Insert_Item_Double_List(Double_List* Head,
int Number, int DataItem) {
Number--;
Double_List* NewItem = new(Double_List);
NewItem->Data = DataItem;
NewItem->Prior = NULL;
NewItem->Next = NULL;
if (Head == NULL) {//список пуст
Head = NewItem;
}
else {//список не пуст
Double_List* Current = Head;
for (int i = 1; i < Number && Current->Next != NULL; i++)
Current = Current->Next;
if (Number == 0) {
//вставляем новый элемент на первое место
NewItem->Next = Head;
Head->Prior = NewItem;
Head = NewItem;
}
else {//вставляем новый элемент на непервое место
if (Current->Next != NULL) Current->Next->Prior = NewItem;
NewItem->Next = Current->Next;
Current->Next = NewItem;
NewItem->Prior = Current;
Current = NewItem;
}
}
return Head;
}
/*удаление элемента с заданным номером из двунаправленного списка*/
Double_List* Delete_Item_Double_List(Double_List* Head,
int Number) {
Double_List* ptr;//вспомогательный указатель
Double_List* Current = Head;
for (int i = 1; i < Number && Current != NULL; i++)
Current = Current->Next;
if (Current != NULL) {//проверка на корректность
if (Current->Prior == NULL) {//удаляем первый элемент
Head = Head->Next;
delete(Current);
if (Head != 0) Head->Prior = NULL;
Current = Head;
}
else {//удаляем непервый элемент
if (Current->Next == NULL) {
//удаляем последний элемент
Current->Prior->Next = NULL;
delete(Current);
Current = Head;
}
else {//удаляем непервый и непоследний элемент
ptr = Current->Next;
Current->Prior->Next = Current->Next;
Current->Next->Prior = Current->Prior;
delete(Current);
Current = ptr;
}
}
}
return Head;
}
//освобождение памяти, выделенной под двунаправленный список
void Delete_Double_List(Double_List* Head) {
if (Head != NULL) {
Delete_Double_List(Head->Next);
delete Head;
}
}
//-------------------------------------------------------------------
struct Queue {
Double_List* Begin;//начало очереди
Double_List* End; //конец очереди
};
Queue* My_Queue;//указатель на очередь
//создание очереди
void Make_Queue(int n, Queue* End_Queue) {
Make_Double_List(n, &(End_Queue->Begin), NULL);
Double_List* ptr; //вспомогательный указатель
ptr = End_Queue->Begin;
while (ptr->Next != NULL)
ptr = ptr->Next;
End_Queue->End = ptr;
}
//проверка пустоты двунаправленого списка
bool Empty_Double_List(Double_List* Head) {
if (Head != NULL) return false;
else return true;
}
//печать очереди
void Print_Queue(Queue* Begin_Queue) {
Print_Double_List(Begin_Queue->Begin);
}
//добавление элемента в конец очереди
void Add_Item_Queue(int NewElem, Queue* End_Queue) {
End_Queue->End = Insert_Item_Double_List(End_Queue->End,
0, NewElem)->Next;
}
//извлечение элемента из начала очереди
int Extract_Item_Queue(Queue* Begin_Queue) {
int NewElem = NULL;
if (Begin_Queue->Begin != NULL) {
NewElem = Begin_Queue->Begin->Data;
Begin_Queue->Begin = Delete_Item_Double_List(Begin_Queue->Begin, 0);
//удаляем вершину
}
return NewElem;
}
//проверка пустоты очереди
bool Empty_Queue(Queue* Begin_Queue) {
return Empty_Double_List(Begin_Queue->Begin);
}
//очистка очереди
void Clear_Queue(Queue* Begin_Queue) {
return Delete_Double_List(Begin_Queue->Begin);
}
//функция поиска первого наибольшего отрицательного элемента
int Find_Max_Negative_Element(Queue* Begin_Queue) {
int tmp;
int max = Extract_Item_Queue(Begin_Queue);
while (Begin_Queue->Begin->Data != 0) {
tmp = Extract_Item_Queue(Begin_Queue);
if (max < tmp)
{
max = tmp;
}
}
return max;
}
int main()
{
setlocale(LC_ALL, "rus");
Queue* My_Queue = new Queue();
Queue* End_Queue = new Queue();
Queue* Begin_Queue = new Queue();
Make_Queue(4, End_Queue);
Print_Queue(Begin_Queue);
system("pause");
return 0;
}