//inserts a Partial Path into a list in it's sorted position (sorted by costtonode/f-cost (depending on algorithm)) Insertion sort for each insertion should be O(N). void insertSorted(LinkedList* list, Solution* sol) { LinkedListNode* node = list->head; LinkedListNode* replace; //spot to replace int foundspot = 0; while ((node != NULL) && (!foundspot)) { if ( ((Solution*)(node->data))->cost > sol->cost) { foundspot = 1; replace = node->prev; //if it's the smallest node if (replace == NULL) insertFirst(list,sol); else { //if it is in the middle somewhere LinkedListNode *newLLN = (LinkedListNode*)malloc(sizeof(LinkedListNode)); newLLN->data = (void*)sol; newLLN->next = replace->next; newLLN->prev = replace; replace->next = newLLN; newLLN->next->prev = newLLN; (list->len)++; } } node = node->next; } //if it is the largest node if (!foundspot) insertLast(list, sol); }
int enqueue(list* listPtr,int priority,void* data){ Node* nodePtr = calloc(sizeof(Node),1); Node *temp=calloc(sizeof(Node),1); Node *temp2=calloc(sizeof(Node),1); if(listPtr->length == 0){ return createFirstNode(nodePtr,listPtr,data,priority); } else{ createNode(nodePtr,data,priority); temp = listPtr->head; if(temp->priority > nodePtr->priority) return insertFirst(nodePtr,temp,listPtr); while(temp->next!=NULL) temp= temp->next; if(temp->priority < nodePtr->priority) return insertLast(temp,nodePtr,listPtr); temp = listPtr->head; while(temp->priority < nodePtr->priority){ temp2=temp; temp=temp->next; } temp2->next = nodePtr; nodePtr->next=temp; listPtr->length +=1; return 1; }; }
int main(int argc, char const *argv[]) { List tegut; initializeList(&tegut); std::cout << "Ausgabe first: " << tegut.first << std::endl; std::cout << "Ausgabe last: " << tegut.last << std::endl; for (int i = 0; i < 3; i++) { insert0(&tegut, insertArtikel()); // std::cout << "Ausgabe first: " << tegut.first << std::endl; // std::cout << "Ausgabe last: " << tegut.last << std::endl; } insertLast(&tegut, insertArtikel()); insertPos(&tegut, insertArtikel(), 2); outputList(&tegut); std::cout << "Nach remove0:" << std::endl; remove0(&tegut); outputList(&tegut); std::cout << "Nach removePos:" << std::endl; removePos(&tegut, 1); outputList(&tegut); return 0; }
void insVLast(List &L, Peserta PNew){ address P=alokasi(PNew); if(alokasiSukses(P)) { insertLast(L,P); } }
void insertAtPosition(node **head, int data, int pos) { int len = size(head); if (pos <= 0 || pos > (len + 1)) { printf("Position is not Valid\n"); return; } if(pos == 1) { insertFirst(head,data); } else if(pos == (len +1)) { insertLast(head, data); } else { // execution reach in this location if position is between 2 and len node *newNode = createNode(data); node *curr = (*head)->next; node *prev = *head; int i = 2; // start from 2nd node while(i != pos) { i ++; prev = curr; curr = curr->next; } newNode->next = curr; prev->next = newNode; } // size++; }
void insertAtRank (int rank, const Object& element)// insert at given rank { if (rank == size()) // no checkRank if last insertLast(element); else { checkRank(rank); insertBefore( atRank(rank), element ); } }
int main(void) { initializeList(); while(1) { printf("1. Insert new item. 2. Delete item. 3. Search item. \n"); printf("4. Insert Last. 5. Delete Last. 6. Print forward. \n"); printf("7. Print backward. 8. exit.\n"); int ch; scanf("%d",&ch); if(ch==1) { int item; scanf("%d", &item); insertFirst(item); } else if(ch==2) { int item = deleteLast(); if(item!=NULL_VALUE) printf("Deleted: %d\n", item); } else if(ch==3) { int item; scanf("%d", &item); struct listNode * res = searchItem(item); if(res!=0) printf("Found.\n"); else printf("Not found.\n"); } else if(ch==4) { int item; scanf("%d",&item); insertLast(item); } else if(ch==5) { deleteLast(); } else if(ch==6) { printListForward(); } else if(ch==7) { printListBackward(); } else if(ch==8) { freeall(list); break; } } }
int DList_insertData(DList* list , int index , void* data){ Node* node = calloc(1,sizeof(node)); if (index > list->length) return 0; if (index == 0) insertFirst(list,node); else if(index == list->length) insertLast(list,node); else insertMiddle(list, index, node); node->data = data; list->length++; return 1; };
int main(){ struct mhs *p = createMhs("1901466641","Jeremy", 100); struct mhs *q = createMhs("048320845","Adam", 99); struct mhs *r = createMhs("789103615","Testing", 45); struct mhs *s = createMhs("489456106","DeadPoll", 79); struct mhs *t = createMhs("081557892","Polling", 50); insertLast(p); insertLast(q); insertLast(s); deleteMiddle(s); struct mhs *temp; temp = searchByNim("1901466641"); printf("%s %d\n",temp->nama,temp->nilai); print(); free(p); free(q); getchar(); return 0; }
//finds the GraphNode in the list, and if it doesn't exist, creates one with that name GraphNode* findNode(char* name, LinkedList* list) { LinkedListNode* currnode = list->head; GraphNode* graphnode = nodeSearch(name, list); //if the node can't be found, allocate a node for it if (graphnode == NULL) { graphnode = makeGraphNode(name); insertLast(list, graphnode); } return graphnode; }
// int size = 0; int main() { //Initialize Crcular linked list with NULL head node *head = NULL; int data = 10; // printf("Enter: "); // scanf("%d",&data); insertLast(&head, data); insertFirst(&head, 5); print(&head); printf("Size: %d\n", size(&head)); printf("Insert at position 1\n"); insertAtPosition(&head, 1,1); print(&head); printf("Size: %d\n", size(&head)); printf("Insert at position 4\n"); insertAtPosition(&head, 15,4); print(&head); printf("Size: %d\n", size(&head)); deleteFirst(&head); print(&head); printf("Size: %d\n", size(&head)); deleteLast(&head); print(&head); printf("Size: %d\n", size(&head)); deleteAtPosition(&head,2); print(&head); printf("Size: %d\n", size(&head)); deleteAtPosition(&head,1); print(&head); printf("Size: %d\n", size(&head)); printf("New Circular linked list\n"); node *head1 = NULL; createList(&head1); print(&head1); printf("Enter data want to search: "); scanf("%d", &data); search(&head1,data); printf("Sort a circular linked list\n"); sort(&head1); print(&head1); }
// copy constructor DoublyLinkedList::DoublyLinkedList(DoublyLinkedList& dll) { // Initialize the list header.next = &trailer; trailer.prev = &header; if (!dll.isEmpty()){ DListNode* node; node=dll.getFirst(); while (node!=dll.getAfterLast()){ insertLast(node->getElem());//insert new element node=node->getNext();//set node to next node } } }
void insertAfter(List &L, address P, address Prec) { if(Prec == Last(L)) { insertLast(L,P); } else { Next(P) = Next(Prec); Prev(P) = Prec; Next(Prec) = P; Prev(next(Prec)) = P; } }
int main(void) { initializeList(); while(1) { printf("1. Insert new item. 2. Delete item. 3. Search item. \n"); printf("4. Insert last. 5. Print. 6.exit. \n"); int ch; scanf("%d",&ch); if(ch==1) { int item; scanf("%d", &item); insertItem(item); } else if(ch==2) { int item; scanf("%d", &item); deleteItem(item); } else if(ch==3) { int item; scanf("%d", &item); struct listNode * res = searchItem(item); if(res!=0) printf("Found.\n"); else printf("Not found.\n"); } else if(ch==4) { int item; scanf("%d", &item); insertLast(item); } else if(ch==5) { printList(); } else if(ch==6) { break; } } }
//adds each node to the other's connectedNodes list int addvert(GraphNode* a, GraphNode* b,int weight) { LinkedListNode* tmpnode = a->connectedNodes->head; //see if node b is in a's list of connected node (if so we exit) while (tmpnode != NULL) { if (!strcmp(((GraphNode*)(((ConnectedNode*)(tmpnode->data))->node))->name, b->name)) return 0; tmpnode = tmpnode->next; } ConnectedNode* conNode = (ConnectedNode*)malloc(sizeof(ConnectedNode)); conNode->node = b; conNode->weight = weight; insertLast(a->connectedNodes, (void*)conNode); (a->connectedNodes->len)++; addvert(b,a,weight); static int x = -1; return 1; }
void main(int argc, char * argv[]) { int i, test, vyska; float vaha, vek; char * jmeno; FILE *jm_soub; tClovek searchTest={0,0,0,"Matej Kocour"}, nacti[5]; tUkDbClovek db=NULL, tmpUk; if (argc >=2) { if( (jm_soub = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Soubor %s se nepodarilo otevrit.\n", argv[1]); printf("Soubor %s se nepodarilo otevrit.\n", argv[1]); exit(1); } else { jm_soub = fopen(argv[1], "r"); for(i=0; i<5; i++) { fscanf(jm_soub, "%i %.1f %.1f %s\n", &nacti[i].vyska, &nacti[i].vaha, &nacti[i].vek, &nacti[i].jmeno); } fclose(jm_soub); printf("\nNacteni informaci ze souboru dokonceno"); } db=insertFirst(db, nacti[0]); db=insertFirst(db, nacti[1]); db=insertLast(db, nacti[2]); db=insert(db, nacti[3]); db=insert(db, nacti[4]); printf("\nInformace ulozeny do seznamu"); tmpUk=search(db, nacti[2]); if (tmpUk!=NULL && tmpUk->clovek.jmeno!=NULL) printf("Found: %s\n", tmpUk->clovek.jmeno); tmpUk=search(db, searchTest); if (tmpUk!=NULL && tmpUk->clovek.jmeno!=NULL) printf("Found: %s\n", tmpUk->clovek.jmeno); db=deleteLast(db); db=deleteAny(db); while ((db=deleteFirst(db))!=NULL); /* smazani celeho seznamu */ printf("\nSeznam je smazan"); } else printf("parametr \"jmeno souboru\" nebyl zadan!\n"); }
// assignment operator DoublyLinkedList& DoublyLinkedList::operator=(DoublyLinkedList& dll) { DListNode *prev_node, *node = header.next; while (node != &trailer) { prev_node = node; node = node->next; delete prev_node; } header.next = &trailer; trailer.prev = &header; // Copy from dll if (!dll.isEmpty()){ node=dll.getFirst(); while (node!=dll.getAfterLast()){ insertLast(node->getElem());//insert new element node=node->getNext();//set node to next node } } }
// Insert the new cell just below the lowest boundary cell void ZOrderedCells::insertCell(Cell * cell) { // Get boundary cells CellSet boundary = cell->boundary(); // Insert at appropriate position if(boundary.size() == 0) { // Insert last insertLast(cell); } else { // Insert before boundary Iterator it = begin(); while(it!=end() && !boundary.contains(*it)) ++it; list_.insert(it,cell); } }
/* * Add an available move for the player to the list of moves. * - Move is expected to be valid in terms of piece type constraints (e.g: a peon can only move to 3 possible squares). * - Additional validation will be done in this function (moves that result in a check status for the current player are * illegal). * --> If the move is legal, it is added to the list of possibleMoves. Otherwise nothing happens. * Input: * board ~ The chess game board. * possibleMoves ~ A list of possible moves by the current player, we aggregate it as we check * possible eat / position change moves. * isMovesForBlackPlayer ~ True if current player is black. False if white. * startPos ~ Where the piece is currently located. * targetX, targetY ~ Coordinates of where the piece will move to. * kingPos ~ Current position of the current player's king (following the execution of the move). */ bool addPossibleMove(char board[BOARD_SIZE][BOARD_SIZE], LinkedList* possibleMoves, bool isMovesForBlackPlayer, Position* startPos, int targetX, int targetY, Position* kingPos) { // Check if the move doesn't cause the current player a check. If it does, we don't count it. if (!isValidMove(board, isMovesForBlackPlayer, startPos, targetX, targetY, kingPos)) return false; Position targetPos = { targetX, targetY }; Move* newMove = createMove(startPos, &targetPos); if (g_memError) return false; insertLast(possibleMoves, newMove); if (g_memError) { deleteMove((void*)newMove); return false; } return true; }
void CircleList< TYPENODE >::insertSort( Node<TYPENODE> node ){ if ( isEmpty( ) ){ head = new Node< TYPENODE >( node ); head->next = head; } else if ( head->next->info.getPlaque( ).compare( node.info.getPlaque( ) ) > 0 ){ insertBegin( node ); } else if ( head->info.getPlaque( ).compare( node.info.getPlaque( ) ) < 0 ){ insertLast( node ); } else{ Node< TYPENODE > *newNode = new Node< TYPENODE >( node ); Node< TYPENODE > *actNode = head->next; Node< TYPENODE > *antNode = head; while( node.info.getPlaque( ).compare( actNode->info.getPlaque() ) > 0 ){ antNode = actNode; actNode = actNode->next; } antNode->next = newNode; newNode->next = actNode; } }
void LinkedTokenList::add(shared_ptr<Token> token) { insertLast(token); }
int main() { int pil; infotype x, y; address P, Q; list L; createList(&L); while(pil != 10) { cout<<"1. Insert first"<<endl; cout<<"2. Insert after"<<endl; cout<<"3. Insert last"<<endl; cout<<"4. Delete first"<<endl; cout<<"5. Delete after"<<endl; cout<<"6. Delete last"<<endl; cout<<"7. View list"<<endl; cout<<"8. Search element"<<endl; cout<<"9. Search sentinel"<<endl; cout<<"Pilih: "; cin>>pil; switch(pil) { case 1: cout<<"ID : "; cin>>x.id; cout<<"Nama : "; cin>>x.nama; cout<<"Jabatan : "; cin>>x.jabatan; cout<<"Departemen : "; cin>>x.departemen; cout<<"Gaji : "; cin>>x.gaji; P = allocate(x); insertFirst(&L, P); cout<<endl; cout<<"Insert complete."; break; case 2: cout<<"ID : "; cin>>x.id; cout<<"Nama : "; cin>>x.nama; cout<<"Jabatan : "; cin>>x.jabatan; cout<<"Departemen : "; cin>>x.departemen; cout<<"Gaji : "; cin>>x.gaji; cout<<"Dimasukan setelah ID: "; cin>>y.id; if(searchElement(&L, y) != Nil) { insertAfter(&L, allocate(x), allocate(y)); cout<<endl; cout<<"Insert complete."; } else { cout<<"Data tidak ada."; } break; case 3: cout<<"ID : "; cin>>x.id; cout<<"Nama : "; cin>>x.nama; cout<<"Jabatan : "; cin>>x.jabatan; cout<<"Departemen : "; cin>>x.departemen; cout<<"Gaji : "; cin>>x.gaji; insertLast(&L, allocate(x)); cout<<endl; cout<<"Insert complete."; break; case 4: cout<<"ID : "; cin>>x.id; deleteFirst(&L, allocate(x)); cout<<endl; cout<<"Delete complete."; break; case 5: cout<<"ID: "; cin>>x.id; P = allocate(x); cout<<"Dimasukan setelah ID: "; cin>>x.id; Q = allocate(x); deleteAfter(&L, P, Q); cout<<endl; cout<<"Delete complete."; break; case 6: cout<<"ID: "; cin>>x.id; deleteFirst(&L, allocate(x)); cout<<endl; cout<<"Delete complete."; break; case 7: viewList(L); break; case 8: cout<<"ID: "; cin>>x.id; P = searchElement(&L, x); if(P != Nil) { cout<<"ID = "<<info(P).id<<endl; cout<<"Nama = "<<info(P).nama<<endl; cout<<"Jabatan = "<<info(P).jabatan<<endl; cout<<"Departemen = "<<info(P).departemen<<endl; cout<<"Gaji = "<<info(P).gaji<<endl; } else { cout<<"Data tidak ditemukan."; } break; case 9: cout<<"ID: "; cin>>x.id; P = searchSentinel(&L, x); if(P != Nil) { cout<<"ID = "<<info(P).id<<endl; cout<<"Nama = "<<info(P).nama<<endl; cout<<"Jabatan = "<<info(P).jabatan<<endl; cout<<"Departemen = "<<info(P).departemen<<endl; cout<<"Gaji = "<<info(P).gaji<<endl; } break; } } return 0; }
int main(void) { initializeList(); while(1) { printf("1. Insert new item. 2. Delete item. 3. Search item. \n"); printf("4. Insert new item at last of list. 5. Insert new item after old item in list.\n"); printf("6. Delete first item in list 7. Delete last item in list.\n"); printf("8. Print. 9. exit.\n"); int ch; scanf("%d",&ch); if(ch==1) { int item; scanf("%d", &item); insertItem(item); } else if(ch==2) { int item; scanf("%d", &item); deleteItem(item); } else if(ch==3) { int item; scanf("%d", &item); struct listNode * res = searchItem(item); if(res!=0) printf("Found.\n"); else printf("Not found.\n"); } else if(ch==4) { int item; scanf("%d",&item); insertLast(item); } else if(ch==5) { int oldItem,newItem; printf("Old item: "); scanf("%d",&oldItem); printf("New item: "); scanf("%d",&newItem); insertAfter(oldItem,newItem); } else if(ch==6) { deleteFirst(); } else if(ch==7) { deleteLast(); } else if(ch==8) { printList(); } else if(ch==9) { break; } } }
/** * Acts as a withdraw process */ void withdraw(int request) { int semid = get_semid((key_t)SEMAPHORE_KEY); int shmid = get_shmid((key_t)SEMAPHORE_KEY); struct shared_variable_struct *shared_variables = shmat(shmid, 0, 0); // wait(mutex) printf("PID: %d - Someone is waiting on mutex to withdraw $%d.\n", getpid(), request); semaphore_wait(semid, SEMAPHORE_MUTEX); printf("PID: %d - Withdrawer:%d has passed mutex.\n", getpid(), request); // if (wcount = 0 and balance > withdraw) if (shared_variables->wcount == 0 && shared_variables->balance >= request) { // {balance = balance - withdraw; signal(mutex)} printf("PID: %d - A withdrawal of $%d was made!\n", getpid(), request); shared_variables->balance = shared_variables->balance - request; printf("PID: %d - Withdrawer:%d is signaling mutex.\n", getpid(), request); print_memory(shared_variables); semaphore_signal(semid, SEMAPHORE_MUTEX); } else { // wcount = wcount + 1; shared_variables->wcount = shared_variables->wcount + 1; // AddEndOfList(LIST, withdraw); printf("PID: %d - Withdrawer:%d is added to queue.\n", getpid(), request); insertLast(shared_variables->list, request); if (shared_variables->list->head == NULL) { printf("Totally null\n"); } // signal(mutex); printf("PID: %d - Withdrawer:%d is signaling mutex.\n", getpid(),request); semaphore_signal(semid, SEMAPHORE_MUTEX); print_memory(shared_variables); // wait(wlist); printf("PID: %d - Withdrawer is waiting to withdraw.\n", getpid()); semaphore_wait(semid, SEMAPHORE_WLIST); printf("PID: %d - Withdrawer is now ready to withdraw.\n", getpid()); // balance = balance - FirstRequestAmount(LIST); shared_variables->balance = shared_variables->balance - getFirstRequestAmount(shared_variables->list); //printf("%d\n", shared_variables->balance); // DeleteFirstRequest(LIST); removeFirst(shared_variables->list); // wcount = wcount - 1; shared_variables->wcount = shared_variables->wcount - 1; // if (wcount > 1 and (FirstRequestAmount(LIST)) < balance)) signal(wlist) if (shared_variables->wcount > 0 && getFirstRequestAmount(shared_variables->list) < shared_variables->balance) { printf("PID: %d - Withdrawer is signaling the next withdrawer.\n", getpid()); print_memory(shared_variables); semaphore_signal(semid, SEMAPHORE_WLIST); } // else signal(mutex)} else { printf("PID: %d - Withdrawer is signaling mutex.\n", getpid()); print_memory(shared_variables); semaphore_signal(semid, SEMAPHORE_MUTEX); } } if (shmdt(shared_variables) == -1) { perror("shmdt failed during a withdraw"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }