This commit is contained in:
iridiumR 2021-11-18 20:44:57 +08:00
parent 293e4be1e9
commit d86571bd80
3 changed files with 235 additions and 0 deletions

89
ex8/ex8.cpp Normal file
View file

@ -0,0 +1,89 @@
#include "queue.hpp"
#include <stdio.h>
#include <time.h>
#define JIE 6
class Train
{
private:
int _v[6];
Queue<int> come;
Queue<int> temp[4];
Queue<int> out;
public:
Train()
{
srand(time(NULL));
for (int i = 0; i < JIE; i++)
_v[i] = JIE - i;
for (int i = 0; i < JIE; i++)
swap(i, rand() % JIE);
printf("入队序列: \n");
for (int i = 0; i < JIE; i++)
{
come.enqueue(_v[i]);
printf("%d ", _v[i]);
}
printf("\n");
}
bool orgnize()
{
int j = 0;
while (!come.empty())
{
for (int i = 0; i < 4;)
{
if (temp[i].back() < come.front())
{
printf("将车厢%d引入缓冲轨道%d \n", come.front(), i + 1);
temp[i].enqueue(come.dequeue());
}
else
{
j++;
i++;
if (j > 20)
return false;
}
}
}
return true;
}
int Out()
{
for (int i = 1; i <= JIE;)
{
for (int j = 0; j < 4;j++)
{
if(temp[j].front()==i)
{
printf("将缓冲轨道%d中的车厢%d引入出发序列\n", j+1,temp[j].front());
out.enqueue(temp[j].dequeue());
i++;
}
}
}
return 0;
}
protected:
void swap(int a, int b)
{
int temp = _v[a];
_v[a] = _v[b];
_v[b] = temp;
}
};
int main()
{
Train T;
T.orgnize() ? T.Out() : printf("编组失败\n");
return 0;
}

107
ex8/list.hpp Normal file
View file

@ -0,0 +1,107 @@
#ifndef _LIST_
#define _LIST_
#include <iostream>
#include <string>
#define Node node *
template <class T>
class List
{
typedef struct node
{
T data;
Node pred;
Node succ;
} node;
private:
Node header = new node;
Node trailer = new node;
int _size;
protected:
Node firstInsertN(Node New);
Node lastInsertN(Node New);
Node succInsertN(Node old, Node ne);
Node predInsertN(Node old, Node ne);
void removeN(Node i);
Node searchN(int value, int n, Node p);
public:
int size()
{
return _size;
}
List();
void clear();
Node first() { return header->succ; }
Node last() { return trailer->pred; }
T firstInsert(T New);
T lastInsert(T New);
T removeLast();
Node succInsert(Node Old, T New);
Node predInsert(Node Old, T New);
int get(int i);
Node find(int i);
T remove(T i);
int operator[](int i)
{
Node p = first();
while (0 < i--)
p = p->succ;
return p->data;
}
};
template <class T>
List<T>::List()
{
_size = 0;
header->data = NULL;
header->succ = trailer;
header->pred = NULL;
trailer->data = NULL;
trailer->succ = NULL;
trailer->pred = header;
}
template <class T>
void List<T>::clear()
{
while (0 < _size)
remove(0);
}
template <class T>
T List<T>::firstInsert(T New)
{
Node temp = new node;
temp->data = New;
temp->pred = header;
temp->succ = header->succ;
header->succ = temp;
temp->succ->pred = temp;
_size++;
return New;
}
template <class T>
T List<T>::removeLast()
{
if (!_size)
return NULL;
Node last = trailer->pred;
T temp = last->data;
(last->pred)->succ = last->succ;
(last->succ)->pred = last->pred;
delete[] last;
_size--;
return temp;
}
#endif

39
ex8/queue.hpp Normal file
View file

@ -0,0 +1,39 @@
#ifndef _QUEUE_
#define _QUEUE_
#include <iostream>
#include "list.hpp"
template <class T>
class Queue
{
private:
List<T> q;
public:
int size()
{
return q.size();
}
bool empty()
{
return size() ? false : true;
}
T enqueue(T e)
{
return q.firstInsert(e);
}
T dequeue()
{
return q.removeLast();
}
T front()
{
return q.last()->data;
}
T back()
{
return q.first()->data;
}
};
#endif