forked from alanachtenberg/CSCE-410
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
83 lines (73 loc) · 2.16 KB
/
Copy pathtest.cpp
File metadata and controls
83 lines (73 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdlib.h>
#include <stdio.h>
struct Thread{
int thread_id;
long long dummy_array[8096];
};
//simple node class for implementation of linked list and ready queue
class Node{
private:
Thread* thread;
Node* next;
public:
Node(){
thread=NULL;
next=NULL;
}
Node(Thread* t){
thread=t;
next=NULL;
}
//appends thread to end of ready queue
//recursive implementation allows append to be called on any node in list with the same result
void enqueue(Thread* t){//will attempt to set next to new Node(thread), if next!= null will recursively call append on next
if (thread==NULL)//case where list is empty
thread=t;
else
if (next==NULL)// check if next exists
next=new Node(t);//if not set next
else
next->enqueue(t);//else call push on next
}
Thread* dequeue(){//returns and removes thread at the beginning of the list
if (thread==NULL)//case where list is empty
return NULL;
if (next!=NULL){
Thread* popped_t=thread;// save oridinal thread
thread=next->thread;//set thread to the next thread
Node* consumed=next;
next=next->next;//set next to the next of the new head
delete consumed;// clean up old node
return popped_t; //return the original thread
}
Thread* temp=thread;//temp pointer to return
thread=NULL;//set thread to null so that push works correctly
return temp;//case where there is only one item in list, return thread
}
};
int main(){
Node list;
while(1){
for (int i=0;i<10;++i){
Thread* thread= new Thread;
thread->thread_id=i;
list.push(thread);
}
for (int i=0;i<5;++i){
Thread* t=list.pop();
delete t;
// printf("%d\n",t->thread_id);
}
for (int i=0;i<5;++i){
Thread* thread= new Thread;
thread->thread_id=i;
list.push(thread);
}
for (int i=0;i<10;++i){
Thread* t=list.pop();
printf("%d\n",t->thread_id);
delete t;
}
}
return 0;
}