using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TALab
{
internal class Algorithm
{
public List<string> TakingExam(List<int> studentsList, int queueType, int
studentsNumber)
{
Queue queue = new Queue(studentsNumber); //Initialising
PointedQueue pointedQueue = new PointedQueue();
List<string> text = new List<string>();
Stopwatch stopwatch = new Stopwatch();
TimeSpan timespan = new TimeSpan();
int charNumber = 0; //Outputing the queue
foreach (int student in studentsList)
{
foreach (char ch in student.ToString())
{
charNumber++; ;
}
charNumber++; ;
}
char[] queueLineList = new char[charNumber];
int i = 0;
foreach (int student in studentsList)
{
foreach (char ch in student.ToString())
{
queueLineList[i] = ch;
i++;
}
queueLineList[i] = '\t';
i++;
}
String queueLine = new String(queueLineList);
text.Add(queueLine);
try //Do the task and count the time
{
FillingQueue();
OutputExam();
text.Add("-----Break-----");
FillingQueue();
OutputExam(true);
}
catch (Exception ex) //Stop the program if necessary
{
text.Add(ex.Message);
return text;
}
return text;
void FillingQueue()
{
stopwatch.Start();
foreach (int student in studentsList)
{
if (queueType == 1)
{
queue.Enqueue(student);
}
else
{
pointedQueue.Enqueue(student);
}
}
stopwatch.Stop();
timespan = stopwatch.Elapsed;
stopwatch.Reset();
text.Add("Time elapsed while enqueueing (ms):" +
(double)timespan.Ticks / 10000);
}
void OutputExam(bool practise = false)
{
stopwatch.Start();
string line = (practise) ? "Practical work for" : "Theoretical survey for";
int currentStudent = (queueType == 1) ? queue.Dequeue() :
pointedQueue.Dequeue();
while ((queueType == 1) ? !queue.IsEmpty() : !pointedQueue.IsEmpty())
{
int nextStudent = (queueType == 1) ? queue.Dequeue() :
pointedQueue.Dequeue();
text.Add(line + " " + currentStudent);
text.Add("Transfering part of task for " + nextStudent);
currentStudent = nextStudent;
}
text.Add(line + " " + currentStudent);
stopwatch.Stop();
timespan = stopwatch.Elapsed;
stopwatch.Reset();
text.Add("Time elapsed while dequeueing (ms):" +
(double)timespan.Ticks / 10000);
}
}
}
}
namespace TALab
{
internal class Queue
{
int[] array;
int head;
int tail;
int size;
int capacity;
public Queue(int capacity)
{
this.capacity = capacity;
this.array = new int[capacity];
this.head = 0;
this.tail = -1;
this.size = 0;
}
public void Enqueue(int item)
{
if (this.size == this.capacity)
{
throw new Exception("Queue overflow");
}
this.tail++;
if (this.tail == this.capacity)
{
this.tail = 0;
}
this.array[this.tail] = item;
this.size++;
}
public int Dequeue()
{
if (this.size == 0)
{
throw new Exception("Queue is empty");
}
int item = this.array[this.head];
this.head++;
if (this.head == this.capacity)
{
this.head = 0;
}
this.size--;
return item;
}
public bool IsEmpty()
{
return this.size == 0;
}
}
}
namespace TALab
{
internal class PointedQueue
{
class QueueItem
{
public int item;
public QueueItem next;
public QueueItem(int item)
{
this.item = item;
}
}
QueueItem head;
QueueItem tail;
public void Enqueue(int item)
{
QueueItem newItem = new QueueItem(item);
if (this.tail == null)
{
this.head = newItem;
}
else
{
this.tail.next = newItem;
}
this.tail = newItem;
}
public int Dequeue()
{
if (this.head == null)
{
throw new Exception("Queue is empty");
}
int item = this.head.item;
head = head.next;
if (head == null)
{
tail = null;
}
return item;
}
public bool IsEmpty()
{
return head == null;
}
}
}