// We have not tested this function yet for part a. Value* apply(Value* function, Value* actualArgs){ //printf("printing actual arguments: "); //printValue(actualArgs); //printf("\n"); if (!function){ return actualArgs; }else if (function->type == primitiveType){ return function->primitiveValue(actualArgs); }else if (function->type == closureType){ List *formalArgs = function->closureValue->args; //eval(formalArgs->head, function->closureValue->parent); Environment *frame = createFrame(function->closureValue->parent); /* Bind formalArgs to actualArgs in frame here. */ Value *curArg = formalArgs->head; Value *curValue = actualArgs; while (curArg && curValue){ assert(getFirst(curArg)->type ==symbolType); insertItem(frame->bindings->tableValue, getFirst(curArg)->symbolValue, getFirst(curValue)); curArg = getTail(curArg); curValue = getTail(curValue); } if (curArg || curValue){ printf("Wrong number of parameters for the procedure.\n"); // destroyFrame(frame); return NULL; } return eval(function->closureValue->body, frame); }else{ printf("Unknown identifier!"); return NULL; } }
// This function evaluates define by creating bindings. It always returns NULL. Value* evalDefine(Value* args, Environment* env){ //printf("I'm in the evalDefine\n"); if (args == NULL||args->cons->cdr== NULL){ printf("syntax error: missing components here\n"); return NULL; } assert(args->type == cellType); //check if there are more than 2 values after define if (listLength(args) > 2){ printf("syntax error: multiple expressions after identifier\n"); return NULL; } //check if the variable is valid if (variableCheck(getFirst(args)) < 1){ if (variableCheck(getFirst(args)) < 0){ printf("bad syntax\n"); return NULL; } else{ printf("cannot change constant variable\n"); return NULL; } }else{ // now it has correct number of arguments and the identifier is valid. // eval the second argument and put (1st, 2nd) as key-value pair in the hash table. assert(env!=NULL); assert(env->bindings->type == tableType); assert(args->type==cellType); assert(getFirst(args)!=NULL); assert(getFirst(args)->type == symbolType); if (getFirst(getTail(args)) && getFirst(getTail(args))->type == symbolType){ Value *value = eval(envLookup(getFirst(getTail(args))->symbolValue, env), env); if (value){ insertItem(env->bindings->tableValue, (getFirst(args))->symbolValue, value); }else{ printf("syntax error: unknown identifier\n"); return NULL; } }else{ assert(getFirst(args)!=NULL); assert(getFirst(args)->type==symbolType); assert(env->bindings->tableValue!=NULL); assert(getFirst(args)->symbolValue); //printf("printing out value"); //printValue(eval(getTail(args),env)); //printf("\n"); insertItem(env->bindings->tableValue, getFirst(args)->symbolValue, eval(getFirst(getTail(args)), env)); } return NULL; } }
boolean RingBuffer::read(uint8_t page[], size_t size) { uint16_t head = getHead(); uint16_t tail = getTail(); uint16_t pageSize = getPageSize(); boolean retVal = false; if (getFlipMarker() == 0) { if (tail < head) { readPage(tail,pageSize,page,size); retVal = true; } } else { if (tail + pageSize > bootSectorStart) { tail = startData; setTail(tail); setFlipMarker(0x00); if (tail < head) { readPage(tail,pageSize,page,size); retVal = true; } } else { readPage(tail,pageSize,page,size); retVal = true; } } return retVal; }
boolean RingBuffer::write(uint8_t page[], size_t size) { uint16_t head = getHead(); uint16_t tail = getTail(); uint16_t pageSize = getPageSize(); boolean retVal = false; // check, if we wrapped if (getFlipMarker() == 0) { if (head + pageSize > bootSectorStart) { // we reached the end of the memory, go to start and flip marker head = startData; setHead(head); setFlipMarker(0xFF); if (head + pageSize < tail) { // there is some space, write the page writePage(head,pageSize,page,size); retVal = true; } } else { // there is some space, write the page writePage(head,pageSize,page,size); retVal = true; } } else { if (head < tail) { // we have some sapace left writePage(head,pageSize,page,size); retVal = true; } } return retVal; }
// here the sorting happens exclusive of the end node // returns the head node after sorting the linked list struct node *quickSortRecur(struct node *head, struct node *end) { // base condition if (!head || head == end) return head; node *newHead = NULL, *newEnd = NULL; // Partition the list, newHead and newEnd will be updated // by the partition function struct node *pivot = partition(head, end, &newHead, &newEnd); // If pivot is the smallest element - no need to recur for // the left part. if (newHead != pivot) { // Set the node before the pivot node as NULL struct node *tmp = newHead; while (tmp->next != pivot) tmp = tmp->next; tmp->next = NULL; // Recur for the list before pivot newHead = quickSortRecur(newHead, tmp); // Change next of last node of the left half to pivot tmp = getTail(newHead); tmp->next = pivot; } // Recur for the list after the pivot element pivot->next = quickSortRecur(pivot->next, newEnd); return newHead; }
void parser(char * argv[], Node * head, Hash ** hashTable, int size) { char line[MAXLINELENGTH]; FILE * infile; char * returnValue; char * firstName; char * lastName; Node * tail; Node * node; char * phoneNumber; infile = fopen(argv[1], "r"); if (infile == NULL) { printf("error opening file \n"); exit(0); } returnValue = fgets(line, MAXLINELENGTH, infile); while (returnValue != NULL) { firstName = strtok(line, ","); lastName = strtok(NULL, ","); phoneNumber = strtok(NULL, "\n"); node = addFront(head, phoneNumber, firstName, lastName); addToTable(node, hashTable, size); returnValue = fgets(line, MAXLINELENGTH, infile); } tail = getTail(head); //quickSort(head -> next, tail); fclose(infile); }
int main() { // the list's name is "nodolista" insertHead(&nodolista.next, 1); insertTail(&nodolista.next, 6); insertTail(&nodolista.next, 3); insertTail(&nodolista.next, 8); insertTail(&nodolista.next, 4); insertTail(&nodolista.next, 7); insertTail(&nodolista.next, 5); insertTail(&nodolista.next, 9); insertTail(&nodolista.next, 2); // end //printList printList(nodolista.next); //getHead printf("\nHead:\t[ (%d) %d ]\n", getHead(nodolista.next)->index, getHead(nodolista.next)->info); //getTail printf("Tail:\t[ (%d) %d ]\n", getTail(nodolista.next)->index, getTail(nodolista.next)->info); // getPrev int k = 0; printf("Insert an index to return the prev (starting from 0 to %d): ", getIndex(getTail(nodolista.next))); scanf("%d", &k); printf("The prev of value %d is: %d\n", getValue(nodolista.next, k)->info, \ (getPrev(nodolista.next, k) ? getPrev(nodolista.next, k)->info : 0)); // search k = 0; printf("Insert a value to search into the list: "); scanf("%d", &k); printf("The search went: %s\n", (search(nodolista.next, k) ? "true" : "false")); // delete printf("Insert an item in the list to delete: "); scanf("%d", &k); delete(nodolista.next, k); printList(nodolista.next); // inserction sort inserctionSort(nodolista.next); printList(nodolista.next); return 0; } //end main
node<T>* getTail(node<T>* n, int &length) { length++; if ( !n->next ) return n; return getTail(n->next, length); }
int reaction(queue_t * self){ if (compare(self)==1){ for(int i = getHead(self); i < getTail(self)-1;i++) queue_remove(self); queue_print(self); } return 0; }
/** * @Synopsis append a node after the tail node * * @Param n - the node to append * @Param poly - the polynomial list */ void appendAfterTail(PolyNode *n, Polynomial *poly) { PolyNode *t = getTail(poly); if( t==NULL ) /* an empty polynomial */ poly->head = n; else t->next = n; /* append to the tail */ n->next = NULL; }
boolean RingBuffer::containsData() { if (getHead() != getTail()) { return true; } // they are equal, we have to check, if we have wrapped if (getFlipMarker() != 0) { return true; } return false; }
//Using linked list to implement undo. void undo(Hole board[], int *gameNum) { if (len(lHead) == 1) { printf("Sorry, you cannot undo past the beginning of the game. Please retry move.\n"); return; } deleteLastNode(&lHead); State* n = getTail(lHead); copyBoard(board, n->data); displayBoard(board); *gameNum -= 1; }
void SList::insertTail(int inNodeContents) { SafePointer<SLNode> newNode = new SLNode(inNodeContents); if(mHead == 0) { mHead = newNode; } else { SLNode* const tailNode = getTail(); tailNode->setNextNode(newNode); } newNode.release(); ++mSize; }
Node * Reverse(Node *head) { Node *newhead = getTail(head); auto cursor = newhead; while(cursor != NULL) { auto tmp = cursor->prev; cursor->prev = cursor->next; cursor->next = tmp; cursor = tmp; } return newhead; }
// remove the last thing in the list, typically the close parenthesis. // If the list is empty or only has one element, nothing will happen. void removeLast(Value* value){ if (!value || value->type!=cellType){ return; } Value *previous = NULL; Value *current = value; Value *next = getTail(value); while (next){ previous = current; current = next; next = getTail(next); } if (!previous){ return; }else{ assert(current->type == cellType); free(current->cons->car); free(current->cons); free(current); previous->cons->cdr = NULL; } }
/* Quote function works well. */ Value* evalQuote(Value* args){ if (getFirst(args) && getFirst(args)->type==closeType){ printf("quote: bad syntax (wrong number of parts) in: (quote)"); return NULL; // since there is one argument list and one close parenthese, the listLength should return zero. }else if (listLength(args) != 1){ printf("quote: bad syntax (wrong number of parts) in: (quote "); printValue(args); printf("\n"); return NULL; } else{ assert(getFirst(getTail(args))!=NULL); assert(getFirst(getTail(args))->type==closeType); Value *toRemove = getTail(args); free(toRemove->cons->car); free(toRemove->cons); free(toRemove); args->cons->cdr = NULL; return args; } }
float TrackData_op::getDuration() { if (isOrphan()) return FrameDur; if ( ! (isHead())) return (StartTime); if (EndTime == 0.0f) { TrackData_op* trk = getTail(); EndTime = trk->getTime() + FrameDur; } return (EndTime - StartTime); }
static void testGetTail(void) { test(getTail("/a/b/c/filename.ext"), "filename.ext"); test(getTail("A:a\\b\\c\\filename.ext"), "filename.ext"); test(getTail("a/b"), "b"); test(getTail("a"), "a"); test(getTail("/a"), "a"); test(getTail("/"), ""); }
static oop intern(char *cstr) { oop list= nil; for (list= symbols; is(Pair, list); list= getTail(list)) { oop sym= getHead(list); if (!strcmp(cstr, get(sym, Symbol,bits))) return sym; } oop sym= nil; GC_PROTECT(sym); sym= newSymbol(cstr); symbols= newPair(sym, symbols); GC_UNPROTECT(sym); return sym; }
// This function evaluates if statement. Value* evalIf(Value* args, Environment* env){ // args = evalEach(args,env); int count = listLength(args); //printf("count = %d \n",count); // printValue(getTail(args)); // printValue(getTail(getTail(args))); if (count < 2) { printf("syntax error: too few arguments in if statement\n"); return NULL; } if ( count > 3) { printf("syntax error: too many arguments in if statement\n"); return NULL; } Value *evalTest = eval(getFirst(args), env); // Value *tempArgs; // printf("result: %d", evalTest->type); if (evalTest && evalTest->type == booleanType && !(evalTest->boolValue)) { // if evalTest is false, then return eval(alternate) // if no alternate, just returns NULL if (count == 3) { return eval(getFirst(getTail(getTail(args))), env); } else return NULL; // DRracket doesn't return a '(), it returns nothing (NULL) } else { // else return eval(consequence) return eval(getFirst(getTail(args)), env); // return eval(alternate) } }
/** bewegt den Wurm von Start nach Destiny * @brief Worm::move * @param start * @param destiny */ bool Worm::move(const WormPart& start, const WormPart& destiny){ int x = start.getX() - destiny.getX(); int y = start.getY() - destiny.getY(); bool Upper(false); bool moveHead(false); if(x > 1) return false; if(y > 1) return false; if(y < -1) return false; if(x < -1) return false; if(getHead() == start){ moveHead = true; Upper = getHead().getPosX() %2; } else if(getTail() == start){ moveHead = false; Upper = getTail().getPosX() %2; } else{ return false; } if(((x==0) && (y==-1)) || ((x==0) && (y==1)) || ((x==-1) && ((Upper && ((y==0) || (y==1))) || ((!Upper) && ((y==-1) || (y==0))))) || ((x==1) && ((Upper && ((y==0) || (y==1))) || ((!Upper) && ((y==-1) || (y==0)))))){ if(moveHead){ posHead++; posHead %= length; wormparts[posHead] = destiny; } else{ wormparts[posHead] = destiny; if(--posHead < 0) { posHead = length-1; } } calcHash(); return true; } return false; }
/*3 Insert a node at the end of the list */ void insertAtEnd(node **head,int value) { node *ptr,*loc; ptr=(node *)malloc(sizeof(node)); //printf("\n%u-->",ptr); ptr->info=value; ptr->next=NULL; if(*head==NULL) *head=ptr; else { loc=getTail(*head); loc->next=ptr; } }
boolean RingBuffer::isFull() { uint16_t head = getHead(); uint16_t tail = getTail(); uint16_t pageSize = getPageSize(); boolean retVal = false; if (getFlipMarker() == 0) { if (head + pageSize > bootSectorStart && startData + pageSize >= tail) { retVal = false; } } else{ retVal = head >= tail; } return retVal; }
node<T>* getIntersectingNode(node<T>* a, node<T>* b) { int length_a = 0; int length_b = 0; node<T>* tail_a = getTail(a, length_a); node<T>* tail_b = getTail(b, length_b); if ( tail_a != tail_b ) return NULL; while ( length_a > length_b ) { a = a->next; length_a--; } while ( length_b > length_a ) { b = b->next; length_b--; } return getIntersection(a, b); }
// Append new element 'new_data' at list's tail bool append(lnode *head_ptr, int const new_data) { bool flag = false; lnode *last = NULL; if(NULL == head_ptr) { // printf("---list doesn't be created---\n"); return flag; } lnode *newnode = newNode(new_data); last = getTail(head_ptr); newnode->pnext = last->pnext; // At this time, last has pointed to head node, so last->node == head_ptr last->pnext = newnode; head_ptr->data++; // change list's count return flag; }
/** * Trim free blocks from the end of the expanded heap. * * Reduces the size of the heap memory region to the end of the last allocated block. * Returns the new size of the heap. */ uint32_t MEMAdjustExpHeap(ExpandedHeap *heap) { ScopedSpinLock lock(&heap->lock); // Find the last free block auto lastFree = getTail(heap->freeBlockList); if (!lastFree) { return heap->size; } // Erase the last free block heap->size -= lastFree->size; eraseBlock(heap->freeBlockList, lastFree); return heap->size; }
std::string iurlstream::getUrlFilename(std::string url) const { std::string filename = url; // strip query string, anchor from URL int questionmark = stringIndexOf(url, "?"); if (questionmark >= 0) { filename = filename.substr(0, questionmark); } int hash = stringIndexOf(url, "#"); if (hash >= 0) { filename = filename.substr(0, hash); } filename = getTail(filename); if (filename.empty()) { filename = "index.tmp"; // for / urls like http://google.com/ } return filename; }
const int EvaFTAgentPacket::parseHeader(unsigned char *buf) { if(getTag() != QQ_FILE_AGENT_PACKET_TAG || getTail() != QQ_FILE_AGENT_PACKET_TAIL) return -1; int pos = 0; m_Version = EvaUtil::read16(buf + pos); pos+=2; unsigned short tmp2 = EvaUtil::read16(buf + pos); pos+=2; if(tmp2 != getBodyLength()) return -1; m_Command = EvaUtil::read16(buf + pos); pos+=2; m_Sequence = EvaUtil::read16(buf + pos); pos+=2; m_Id = EvaUtil::read32(buf + pos); pos+=4; pos+=8; // 8 unknown bytes if(m_Command != QQ_FILE_AGENT_CMD_CREATE){ m_Session = EvaUtil::read32(buf + pos); pos+=4; } return pos; }
int mymain(void) { unsigned int code=0; unsigned char head=0, tail=0; unsigned char scanCode=0, asciiCode=0; disableKey(); if(firstTime==1) { initHeadTail(); initTab(); firstTime=0; } scanCode=getScanCode(); asciiCode=lookupAscii(scanCode); if(asciiCode==0) { /* not printable */ if(scanCode==0x1D) { /* check if ctrl is pressed or not */ isCtrlDown=1; } else if(scanCode==0x9d) { isCtrlDown=0; } } if(scanCode>=0x3b && scanCode<=0x3e && isCtrlDown==1) { setActiveShell((scanCode-0x3b)+1); } code= scanCode; code<<=8; code|=asciiCode; head=getHead(); tail=getTail(); if(!((tail+2)==head || (head==0x1e && tail==0x3c)) ) { /* not full */ setKeyCode(code); if(tail==0x3C) { setTail(0x1e); } else { setTail(tail+2); } } enableKey(); eoi(ss,sp); }
ListNode* rotateRight(ListNode *head, int k) { if (head == nullptr || head->next == nullptr) return head; int size = 0; ListNode *tailNode = getTail(head,size); tailNode->next = head; int count = 1; int offset = size - k%size; while (count < offset) { head = head->next; count++; } ListNode *result = head->next; head->next = nullptr; return result; }