void XMLNodeList::setElementAtPosition(double index, const XMLElement & elem) { if (size == 0) { insertAtEnd(elem); prevNode = parent->children; prev = 1; } else if (index < 1) { insertAtBeginning(elem); } else if (index > size) { insertAtEnd(elem); } else if ((int)index == index) { replaceAtIndex((int)index, elem); } else { insertAtIndex((int)index, elem); } }
void sum(node *n1, node *n2){ node *temp1 = n1, *temp2 = n2; int output,carry = 0,rem; while(temp1&&temp2){ output = temp1->data + temp2->data + carry; rem = output%10; carry = output/10; res = insertAtEnd(res,rem); temp1=temp1->next; temp2=temp2->next; } for(;temp1;temp1=temp1->next){ rem = (temp1->data + carry)%10; carry = (temp1->data +carry)/10; res = insertAtEnd(res,rem); } for(;temp2;temp2=temp2->next){ rem = (temp2->data + carry)%10; carry = (temp2->data +carry)/10; res = insertAtEnd(res,rem); } if(carry) res = insertAtEnd(res,carry); }
void saveContext(node *p){ int executedTime,n; //unload running process //modify the state of the process - task status change, //update time left if(p->TCB.taskState == 0x01) {//Running p->TCB.taskState = 0x02; //Blocked p->TCB.taskDuration = 0; p->TCB.remainingTaskDuration = p->TCB.taskDuration - executedTime; //place it in appropriate position in READY queue insertAtEnd(p,n); //insert this task at the end of READY queue sortList(p); //sort the queue according to task durations //place the process in waiting queue having process pend on a signal //create a wait queue with insertion options //wait for a signal /***** code *****/ } }
/* Stop the process in the given cell (do not verify if the cell is indeed in * the runningList), i.e.: remove it from the running. The process won't be * running until it start again */ void stop(struct cell * processCell) { void* process = processCell->element; removeCell(&runningList, processCell); insertAtEnd(&stoppedList, process); yield(); /* Yield in case the processus have the processor */ }
int main(void) { struct item *start = NULL; int choice, number, n; do { printf("1. Insert element into a linked list\n"); printf("2. Find nth element in the linked list\n"); printf("Enter choice\n"); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the number to be inserted\n"); scanf("%d", &number); start = insertAtEnd(start, number); break; case 2: printf("Enter the value of n\n"); scanf("%d", &n); number = getNth(start, n); printf("The %dth element in the linked list is %d\n", n, number); break; case 3: break; default: printf("Invalid choice\n"); } }while(choice != 3); return 0; }
List<DataType> & List<DataType>::operator=(const List<DataType> &list) { // check for self-assignment if (this == &list) return(*this); // delete old list ListItem<DataType> *pos; for (pos = first; pos != NULL; ) { ListItem<DataType> *save = pos->next; delete pos; pos = save; } count = 0; first = last = NULL; // copy new list for (pos = list.first; pos != NULL; pos = pos->next) { // item counts updated by insert function insertAtEnd(pos->data); } return(*this); }
int List<DataType>::insertNth(int n, const DataType &data) { // invariant MustBeTrue((first != NULL || last == NULL) && (first == NULL || last != NULL)); // check for out of range if (n < 1) return(NOMATCH); // find location to insert new tuple if (isEmpty()) { // add at the front return(insertAtFront(data)); } else if (n > count) { // add at the end return(insertAtEnd(data)); } // search for correct place to insert new item int i; ListItem<DataType> *pos; for (i = 1, pos = first; pos != NULL && i < n; pos = pos->next, i++) ; // check which case if (pos != NULL && i == n) { // allocate a new item ListItem<DataType> *pitem = new ListItem<DataType>(data); if (pitem == NULL) return(NOTOK); // update links pitem->previous = pos->previous; pitem->next = pos; pos->previous = pitem; if (pitem->previous != NULL) { // previous link must point to new link pitem->previous->next = pitem; } else { // at beginning of list first = pitem; } // increment the counter count++; return(OK); } else return(NOMATCH); }
List<DataType>::List(const List<DataType> &list): count(0), first(NULL), last(NULL) { for (ListItem<DataType> *pos = list.first; pos != NULL; pos = pos->next, count++) { insertAtEnd(pos->data); } }
int main(void){ int val; char ch; do{ scanf("%d%c",&val,&ch); num1 = insertAtEnd(num1,val); //display(num1); }while(ch!='\n'); do{ scanf("%d%c",&val,&ch); num2 = insertAtEnd(num2,val); }while(ch!='\n'); sum(num1,num2); display(res); return 0; }
int main() { struct Node*start=NULL; start=insertAtEnd(start,makeNode(5)); start=insertAtEnd(start,makeNode(3)); start=insertAtEnd(start,makeNode(7)); start=insertAtEnd(start,makeNode(4)); start=insertAtEnd(start,makeNode(9)); start=insertAtEnd(start,makeNode(2)); start=insertAtEnd(start,makeNode(1)); start=insertAtEnd(start,makeNode(8)); start=insertAtEnd(start,makeNode(6)); printList(start); return 0; }
struct cell * createEmptyProcess(void) { struct processDescriptor * process = kalloc(sizeof(struct processDescriptor)); process->ppid = choosePPID(); pidCounter++; process->pid = pidCounter; insertAtEnd(&stoppedList, process); return getIndex(&stoppedList, 0); }
List<DataType>::List(const List<DataType> &list): count(0), first(NULL), last(NULL) { for (ListItem<DataType> *pos = list.first; pos != NULL; pos = pos->next) { // item count is updated by insert function insertAtEnd(pos->data); } }
int main(void) { int data; char ch; do{ scanf("%d%c",&data,&ch); insertAtEnd(data); }while(ch!='\n'); display(); return 0; }
void main() { node *head,*tail; createList(&head); insertAtBeginning(&head,4); insertAtBeginning(&head,3); insertAtBeginning(&head,2); insertAtBeginning(&head,5); insertAtBeginning(&head,6); insertAtBeginning(&head,10); insertAtEnd(&head,1); insertInBetween(&head,9,2); traverseList(head); printf("\n"); reverseTraversal(head); reverseList(&head); traverseList(head); deleteEnd(&head); traverseList(head); deleteBeginning(&head); traverseList(head); deleteAfter(&head,2); traverseList(head); deleteList(&head); traverseList(head); // tail= getTail(head); // printf("\nTail= %d",tail->info); }
int validSuccessor(tQueue *a, tQueue *f, tNodeQueue *p, tNodeQueue *m, int heuristica) { m->elem->g = p->elem->g + 1; printf("%d\n",a); if ((!(memberOf(m, a)) || (!(memberOf(m, f))))) { insertAtEnd(m, a); calcH(p, m, heuristica); } else { if (memberOfWithGMin(m, a)) { insertAtEnd(m, a); calcH(p, m, heuristica); } else { if (memberOfWithGMin(m, f)) { removeElem(m, f); insertAtEnd(m, a); calcH(p, m, heuristica); } } } }
int List<DataType>::insertOrderedUnique(const DataType &data) { // invariant MustBeTrue((first != NULL || last == NULL) && (first == NULL || last != NULL)); // search for correct place to insert new item ListItem<DataType> *pos; for (pos = first; pos != NULL && pos->data < data ; pos = pos->next) ; // check which case if (pos == NULL) { // insert at end of list return(insertAtEnd(data)); } else if (pos->data == data) { // we found it, overwrite current data pos->data = data; count++; } else { // allocate a new item ListItem<DataType> *pitem = new ListItem<DataType>(data); if (pitem == NULL) return(NOTOK); // updata links pitem->previous = pos->previous; pitem->next = pos; pos->previous = pitem; if (pitem->previous != NULL) { // previous link must point to new link pitem->previous->next = pitem; } else { // at beginning of list first = pitem; } count++; } // all done return(OK); }
int main() { struct Node*start=NULL; start=insertAtEnd(start,makeNode(1)); start=insertAtEnd(start,makeNode(2)); start=insertAtEnd(start,makeNode(3)); start=insertAtEnd(start,makeNode(4)); start=insertAtEnd(start,makeNode(5)); start=insertAtEnd(start,makeNode(6)); start=insertAtEnd(start,makeNode(7)); start=insertAtEnd(start,makeNode(8)); start=insertAtEnd(start,makeNode(9)); printf("The original LinkedList is : \n"); printList(start); start=reverseListAdvanced(start,1,4); printf("The reverse of the original Linked List is : \n"); printList(start); return 0; }
int insertNode(List *list,int index,void* data){ Node *head,*newNode; int i; if(index <= -1 || index > list->length) return 0; list->length++; head = list->head; for (i = 0; i < index; ++i){ if(head->next != NULL) head = head->next; } newNode = malloc(sizeof(Node)); newNode->next = NULL; newNode->data = data; if(1 == insertAtFront(newNode,list,index,head)) return 1; if(1 == insertAtEnd(newNode,list,index,head)) return 1; insertAtMiddle(newNode, head); return 1; }
int main(){ std::array<int, 8> values{1,2, 4, 5, 2, 1, 6, 3}; Node* firstNode = new Node(values[0]); for(int i = 1; i < values.size(); i++) { insertAtEnd(firstNode, values[i]); } printValues(firstNode); removeDups(firstNode); printValues(firstNode); delete firstNode; }
void SinglyLinkedList::createDummyList(){ int nodes,data; cout<<"Enter the number of nodes"<<endl; cin>>nodes; head=NULL; while(nodes){ cout<<"Enter data"<<endl; cin>>data; insertAtEnd(data); nodes--; } //Point the last node to head to create a loop SingleNode *tmp=head; if(tmp){ while(tmp->next){ tmp=tmp->next; } tmp->next=head; } }
int main(){ int x = 21; struct list *head = NULL; insertAtEnd(&head, x); insertAtEnd(&head, ++x); insertAtEnd(&head, ++x); insertAtEnd(&head, ++x); insertAtEnd(&head, ++x); insertAtEnd(&head, ++x); displayList(head); deleteList(&head); displayList(head); }
/* create new process and returns the cell containing the process */ struct cell * createProcess(void (*f)(void), void* baseAddress) { void* area = get2M(); LOG("Creating process at base address : "); LOG_INT((int)area); LOG_CONT("\n"); /* Prepare initial processState*/ struct processDescriptor * process = kalloc(sizeof(struct processDescriptor)); process->processState.pc = f; process->processState.sp = area + 2*SPACE - 4; /* When exiting, auto-delete process */ process->processState.lr = &deleteProcess; process->ppid = choosePPID(); pidCounter++; process->pid = pidCounter; process->map.baseAddress = area; process->baseAddress = baseAddress; printProcess(process); insertAtEnd(&stoppedList, process); return getIndex(&stoppedList, 0)->previous; }
int aStar(tNodeQueue *root, int heuristica, int *answer) { tQueue *a = malloc(sizeof(tQueue)); tQueue *f = malloc(sizeof(tQueue)); *answer = -1; printf("%d\n",a); initialize(a); initialize(f); insertAtEnd(a, root); while (!isEmpty(a)) { tNodeQueue *v = removeMin(a); insertAtEnd(f, v); if (isFinalState(v->elem->matrix)) { *answer = v->elem->f; return 1; } else { tNodeQueue *top = malloc(sizeof (tNodeQueue)); tNodeBoard *topBoard = malloc(sizeof (tNodeBoard)); top->elem = topBoard; tNodeQueue *right = malloc(sizeof (tNodeQueue)); tNodeBoard *rightBoard = malloc(sizeof (tNodeBoard)); right->elem = rightBoard; tNodeQueue *bottom = malloc(sizeof (tNodeQueue)); tNodeBoard *bottomBoard = malloc(sizeof (tNodeBoard)); bottom->elem = bottomBoard; tNodeQueue *left = malloc(sizeof (tNodeQueue)); tNodeBoard *leftBoard = malloc(sizeof (tNodeBoard)); left->elem = leftBoard; int i; int j; findZeroPosition(v->elem->matrix, &i, &j); if (moveTop(v->elem->matrix, top->elem->matrix, i, j)) { validSuccessor(a, f, v, top, heuristica); } else { free(topBoard); free(top); } if (moveRight(v->elem->matrix, right->elem->matrix, i, j)) { validSuccessor(a, f, v, right, heuristica); } else { free(rightBoard); free(right); } if (moveBottom(v->elem->matrix, bottom->elem->matrix, i, j)) { validSuccessor(a, f, v, bottom, heuristica); } else { free(bottomBoard); free(bottom); } if (moveLeft(v->elem->matrix, left->elem->matrix, i, j)) { validSuccessor(a, f, v, left, heuristica); } else { free(leftBoard); free(left); } } } if (isEmpty(a)) { return 0; } }
/* Start the process in the given cell (do not verify if the cell is indeed in * the stoppedList), i.e.: add it to the running list. The process may not * run immediately*/ void start(struct cell * processCell) { void* process = processCell->element; removeCell(&stoppedList, processCell); insertAtEnd(&runningList, process); }
void str::append(const char* pString) { insertAtEnd(pString); }
int main () { FreeList *FL_START = NULL, *FL_END = NULL; char tempBuffer[80]; char *inputCommand, *tempBuffer2; unsigned int inputArgument; // Number of bytes requested by the user printf ("********** IMPORTANT **********\n"); printf ("This program implements a Linked List to store the allocated and de-allocated memory.. ..\n"); printf ("The size of LinkedList Structure == %lu (bytes)\n\n", sizeof (FreeList)); printf ("***** Made By: GURSIMRAN SINGH __ 2014041 *****\n\n"); printf ("Enter one of the following (without quotes) to operate\n"); printf ("\t\"malloc\" <int>\t\tTo allocate <int> bytes\n"); printf ("\t\"free\" <int>\t\tTo free the entry at <int> in the list\n"); printf ("\t\"printlist\"\t\tTo print the FreeList\n"); printf ("\t\"exit\"\t\t\tTo quit\n\n"); while (1) { fgets (tempBuffer, 80, stdin); tempBuffer [strlen (tempBuffer) -1] = '\0'; inputCommand = strtok (tempBuffer, " "); tempBuffer2 = strtok (NULL, " "); if (tempBuffer2 != NULL) inputArgument = atoi (tempBuffer2); else inputArgument = -1; // printf ("%s %d\n", inputCommand, inputArgument); if (!strcmp (inputCommand, "malloc")) { if (inputArgument > 0) insertAtEnd (&FL_START, &FL_END, inputArgument); continue; } else if (!strcmp (inputCommand, "free")) { deleteFromPosition (&FL_START, &FL_END, inputArgument); continue; } else if (!strcmp (inputCommand, "printlist")) { if (inputArgument == -1) printFreeList (FL_START, FL_END); continue; } else if (!strcmp (inputCommand, "exit")) { _exit_ (&FL_START, &FL_END); } else { printf ("INVALID_COMMAND: Enter one of the following (without quotes)\n"); printf ("\t\"malloc\" <int>\t\tTo allocate <int> bytes\n"); printf ("\t\"free\" <int>\t\tTo free the entry at <int> in the list\n"); printf ("\t\"printlist\"\t\tTo print the FreeList\n\n"); } } return 0; }
int main(){ int x = 21; struct list *head = NULL; head = insertAtEnd(head, x); head = insertAtEnd(head, x); head = insertAtEnd(head, x); head = insertAtEnd(head, ++x); head = insertAtEnd(head, ++x); head = insertAtEnd(head, ++x); head = insertAtEnd(head, ++x); head = insertAtEnd(head, ++x); head = insertAtEnd(head, ++x); head = insertAtEnd(head, ++x); head = insertAtEnd(head, ++x); displayList(head); //delete by position printf("Deleted Data : %d\n", deleteNodeAtPos(head, 3)); displayList(head); //delete by value (all the nodes containing this value will be deleted) head = deleteNodesContaining(head, 25); head = deleteNodesContaining(head, 21); displayList(head); }