⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dllist.cc

📁 程序使用的是在nachos体系架构下完成双向链表的构建
💻 CC
字号:
#include "copyright.h"#include "dllist.h"#include "system.h"extern int mode;//--------------------------------------------------------------------i// ListElement::ListElement// 	Initialize a list element, so it can be added somewhere on a list.////	"itemPtr" is the item to be put on the list.  It can be a pointer//		to anything.//	"sortKey" is the priority of the item, if any.//----------------------------------------------------------------------DLListElement::DLListElement(void *itemPtr, int sortKey){     item = itemPtr;     key = sortKey;     next = NULL;	// assume we'll put it at the end of the list      prev = NULL;}//----------------------------------------------------------------------// List::List//	Initialize a list, empty to start with.//	Elements can now be added to the list.//----------------------------------------------------------------------DLList::DLList(){     first = last = NULL; }//----------------------------------------------------------------------// List::~List//	Prepare a list for deallocation.  If the list still contains any //	ListElements, de-allocate them.  However, note that we do *not*//	de-allocate the "items" on the list -- this module allocates//	and de-allocates the ListElements to keep track of each item,//	but a given item may be on multiple lists, so we can't//	de-allocate them here.//----------------------------------------------------------------------DLList::~DLList(){    int*element;     while (Remove(element) != NULL)	;	 // delete all the list elements}//----------------------------------------------------------------------// List::Append//      Append an "item" to the end of the list.//      //	Allocate a ListElement to keep track of the item.//      If the list is empty, then this will be the only element.//	Otherwise, put it at the end.////	"item" is the thing to put on the list, it can be a pointer to //		anything.//----------------------------------------------------------------------voidDLList::Append(void *item){    DLListElement *element = new DLListElement(item, 0);    if (IsEmpty()) {		// list is empty	first = element;	last = element;    } else {			// else put it after last	last->next = element;        element->prev = last;        element->next = NULL;        last = element;    }}//----------------------------------------------------------------------// List::Prepend//      Put an "item" on the front of the list.//      //	Allocate a ListElement to keep track of the item.//      If the list is empty, then this will be the only element.//	Otherwise, put it at the beginning./////	"item" is the thing to put on the list, it can be a pointer to //		anything.//----------------------------------------------------------------------voidDLList::Prepend(void *item){    DLListElement *element = new DLListElement(item, 0);    if (IsEmpty()) {		// list is empty	first = element;	last = element;    } else {			// else put it before first        first->prev = element;      	element->next = first;        element->prev = NULL;	first = element;    }}//----------------------------------------------------------------------// List::Remove//      Remove the first "item" from the front of the list.// // Returns://	Pointer to removed item, NULL if nothing on the list.//----------------------------------------------------------------------void *DLList::Remove(int *keyPtr){    return SortedRemove(keyPtr);  // Same as SortedRemove, but ignore the key}//----------------------------------------------------------------------// List::Mapcar//	Apply a function to each item on the list, by walking through  //	the list, one element at a time.////	Unlike LISP, this mapcar does not return anything!////	"func" is the procedure to apply to each element of the list.//----------------------------------------------------------------------/*voidDlList::Mapcar(VoidFunctionPtr func){    for (DlListElement *ptr = first; ptr != NULL; ptr = ptr->next) {       DEBUG('l', "In mapcar, about to invoke %x(%x)\n", func, ptr->item);       (*func)((int)ptr->item);    }}*///----------------------------------------------------------------------// List::IsEmpty//      Returns TRUE if the list is empty (has no items).//----------------------------------------------------------------------boolDLList::IsEmpty() {     if (first == NULL)        return TRUE;    else	return FALSE; }//----------------------------------------------------------------------// List::SortedInsert//      Insert an "item" into a list, so that the list elements are//	sorted in increasing order by "sortKey".//      //	Allocate a ListElement to keep track of the item.//      If the list is empty, then this will be the only element.//	Otherwise, walk through the list, one element at a time,//	to find where the new item should be placed.////	"item" is the thing to put on the list, it can be a pointer to //		anything.//	"sortKey" is the priority of the item.//----------------------------------------------------------------------voidDLList::SortedInsert(void *item, int sortKey){    DLListElement *element = new DLListElement(item, sortKey);    DLListElement *ptr;		// keep track    if (IsEmpty()) {	// if list is empty, put        first = element;        last = element;    } else if (sortKey < first->key) {			// item goes on front of list        first->prev = element;	element->next = first;        element->prev = NULL;	first = element;    } else {		// look for first elt in list bigger than item        for (ptr = first; ptr->next != NULL; ptr = ptr->next) {            if (sortKey < ptr->next->key) {    if(mode==2)                currentThread->Yield();                element->prev = ptr;                      ptr->next->prev = element;		element->next = ptr->next;    if(mode==3)               currentThread->Yield();	        ptr->next = element;		return;	    }	}        element->prev = last;	last->next = element;		// item goes at end of list	last = element;        element->next = NULL;    }}//----------------------------------------------------------------------// List::SortedRemove//      Remove the first "item" from the front of a sorted list.// // Returns://	Pointer to removed item, NULL if nothing on the list.//	Sets *keyPtr to the priority value of the removed item//	(this is needed by interrupt.cc, for instance).////	"keyPtr" is a pointer to the location in which to store the //		priority of the removed item.//----------------------------------------------------------------------void *DLList::SortedRemove(int *keyPtr){    DLListElement *element = first;    void *thing;    if (IsEmpty()) 	return NULL;    thing = first->item;    if(mode==1){    printf("point to  the first item\n");    currentThread->Yield();    printf("turn to the other thread\n");    printf("try to remove the first item\n");   }    if (first == last) {	// list had one item, now has none         first = NULL;	last = NULL;    } else {        first = element->next;        first->prev = NULL;    }    if (keyPtr != NULL)        *keyPtr = element->key;    delete element;    return thing;}voidDLList::Print(){ DLListElement *p; printf("items in the dllist:"); for(p=first;p!=NULL;p=p->next)   printf("%d ",p->key); printf("\n");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -