copy arraylistdemo for linked list
This commit is contained in:
90
c_os/user/demo/LinkedListDemo.cc
Normal file
90
c_os/user/demo/LinkedListDemo.cc
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#include "user/demo/LinkedListDemo.h"
|
||||||
|
|
||||||
|
void LinkedListDemo::run() {
|
||||||
|
kout.lock();
|
||||||
|
kout.clear();
|
||||||
|
kout << "Initial list size: " << dec << this->list.size() << endl;
|
||||||
|
|
||||||
|
kout << "Adding elements in order" << endl;
|
||||||
|
for (unsigned int i = 0; i < 5; ++i) {
|
||||||
|
this->list.insert(i);
|
||||||
|
}
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
kout << "Removing all elements from the front" << endl;
|
||||||
|
for (unsigned int i = 0; i < 5; ++i) {
|
||||||
|
this->list.remove_first();
|
||||||
|
}
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
kout << "Adding elements in order with realloc" << endl;
|
||||||
|
for (unsigned int i = 0; i < 10; ++i) {
|
||||||
|
kout << "Add " << dec << i << endl;
|
||||||
|
this->list.insert(i);
|
||||||
|
}
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
kout << "Removing all elements from the back" << endl;
|
||||||
|
for (unsigned int i = 0; i < 10; ++i) {
|
||||||
|
this->list.remove_last();
|
||||||
|
}
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 5; ++i) {
|
||||||
|
this->list.insert(i);
|
||||||
|
}
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
kout << "Adding inside the list (at idx 0, 2, 5)" << endl;
|
||||||
|
this->list.insert_at(10, 0);
|
||||||
|
this->list.insert_at(10, 2);
|
||||||
|
this->list.insert_at(10, 5);
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
kout << "Removing inside the list (at idx 0, 2, 5)" << endl;
|
||||||
|
this->list.remove_at(0);
|
||||||
|
this->list.remove_at(2);
|
||||||
|
this->list.remove_at(5);
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 5; ++i) {
|
||||||
|
this->list.remove_first();
|
||||||
|
}
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
kout << "Mirror scheduling behavior" << endl;
|
||||||
|
|
||||||
|
// These are the threads
|
||||||
|
int active = 0; // Idle thread
|
||||||
|
this->list.insert(1);
|
||||||
|
this->list.insert(2);
|
||||||
|
this->list.insert(3);
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
kout << "Starting..." << endl;
|
||||||
|
for (unsigned int n = 0; n < 10000000; ++n) {
|
||||||
|
this->list.insert(active);
|
||||||
|
active = list.remove_first();
|
||||||
|
|
||||||
|
if (this->list.size() != 3) {
|
||||||
|
kout << "ERROR: Thread went missing" << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n < 5) {
|
||||||
|
this->list.print(kout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
kout << "Finished." << endl;
|
||||||
|
|
||||||
|
this->list.print(kout);
|
||||||
|
|
||||||
|
kout.unlock();
|
||||||
|
scheduler.exit();
|
||||||
|
}
|
||||||
22
c_os/user/demo/LinkedListDemo.h
Normal file
22
c_os/user/demo/LinkedListDemo.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef __LinkedListDemo_include__
|
||||||
|
#define __LinkedListDemo_include__
|
||||||
|
|
||||||
|
#include "kernel/Globals.h"
|
||||||
|
#include "kernel/threads/Thread.h"
|
||||||
|
#include "user/lib/LinkedList.h"
|
||||||
|
|
||||||
|
class LinkedListDemo : public Thread {
|
||||||
|
private:
|
||||||
|
LinkedListDemo(const LinkedListDemo& copy) = delete;
|
||||||
|
|
||||||
|
LinkedList<int> list;
|
||||||
|
|
||||||
|
public:
|
||||||
|
LinkedListDemo() {
|
||||||
|
kout << "Initialized LinkedListDemo" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void run() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user