int main( void ) { List *list = CreateList( ); node *n; int x = 17; int y = 18; int z = 19; InsertFront( list, &x, sizeof( int ) ); InsertFront( list, &y, sizeof( int ) ); InsertFront( list, &z, sizeof( int ) ); // Loop through list. It's important to use ListBegin and ListEnd for proper looping. // Make note of what pointers Begin and End return. for(n = ListBegin( list ); n != ListEnd( list ); n = n->next) printf( "%d\n", NODE_DATA( n, int ) ); // Proper way to delete nodes from list is like so: // note -- we are not doing n = n->next within the for loop, instead // we use the return value of delete for(n = ListBegin( list ); n != ListEnd( list );) n = DeleteNode( list, n ); getchar( ); return 0; }
int main() { struct Queue *q = CreateQueue(); InsertFront(q,1); InsertFront(q,3); InsertRear(q,0); InsertRear(q,5); printf("%d\n",DeleteFront(q)->element); printf("%d\n",DeleteRear(q)->element); }
// // InsertOctree // - insert a pheremone into the tree // void InsertOctree(Octree **tree_ptr, StaticBotPtr bot) { int i; float x_min,y_min, x_max, y_max; int max = tree_ptr[0]->max_elements; for (i = 0; i < max; i++) { x_min = tree_ptr[i]->x_min; x_max = tree_ptr[i]->x_max; y_min = tree_ptr[i]->y_min; y_max = tree_ptr[i]->y_max; if ((bot->position[0] > x_min) && (bot->position[0] < x_max) && (bot->position[2] > y_min) && (bot->position[2] < y_max)) { // in the area add to list InsertFront(tree_ptr[i]->list, (StaticBotPtr)bot); return; } // end of the if } // end of the for } // end of the function
void InsertPosition(node *first, node *last, int pos) { node prev = NULL, cur = *first, temp; int i; if (pos <= 0 || pos > count + 1) { printf("\nYou entered an invalid position."); return; } temp = Create(); if (pos == 1) InsertFront(first, last, &temp); else if (pos == count + 1) InsertRear(first, last, &temp); else { for (i = 1; i < pos; ++i) { prev = cur; cur = cur->rlink; } temp->rlink = cur; temp->llink = prev; prev->rlink = temp; cur->llink = temp; } ++count; }
int main(void) { Menu menu; List list; InitList(&list); do { Data x; switch (menu = SelectMenu()) { case InsFront: x = Read("先頭に挿入", NO | NAME); InsertFront(&list, x); break; case InsRear: x = Read("末尾に挿入", NO | NAME); InsertRear(&list, x); break; case RmvFront: RemoveFront(&list); break; case RmvRear: RemoveRear(&list); break; case PrintCrnt: PrintCrntNode(&list); break; case RmvCrnt: RemoveCrnt(&list); break; case SrchNo: x = Read("探索", NO); if (SearchNode(&list, x, NoEqual) != NULL){ PrintCrntNode(&list); } break; case SrchName: x = Read("探索", NAME); if(SearchNode(&list, x, NameEqual) != NULL){ PrintCrntNode(&list); } break; case PrintAll: PrintList(&list); break; case Clear: ClearList(&list); break; } } while (menu != Term); TermList(&list); return (0); }
void InsertRear(List *list, Data x) { if (list->head == NULL){ InsertFront(list, x); }else{ Node *ptr = list->head; while (ptr->next != NULL){ ptr = ptr->next; } ptr->next = list->crnt = AllocNode(); SetNode(ptr->next, x, NULL); } }
void main() { node first = NULL, last = NULL; int i, ch; while (1) { printf("\nCIRCULAR LINKED LIST"); printf("\n1. INSERT\n2. DELETE\n3. DISPLAY\n4. EXIT"); printf("\nEnter your choice: "); scanf("%d", &i); //system("cls"); //clrscr(); switch (i) { case 1: printf("\n1. Insert at front"); printf("\n2. Insert at rear"); printf("\nEnter your choice: "); scanf("%d", &ch); switch (ch) { case 1: InsertFront(&first, &last); break; case 2: InsertRear(&first, &last); break; default: printf("\n You chose an invalid option."); } break; case 2: printf("\n1. Delete from front"); printf("\n2. Delete from rear"); printf("\nEnter your choice: "); scanf("%d", &ch); switch (ch) { case 1: DeleteFront(&first, &last); break; case 2: DeleteRear(&first, &last); break; default: printf("\n You chose an invalid option."); } break; case 3: Display(&first, &last); break; case 4: return; default: printf("\nInvalid choice. Try again."); } } }
void LinkedList::Insert(double value, unsigned int index) { if(index == 0) { InsertFront(value); }else if (index > many_nodes) { throw 9; }else{ Node *insert_ptr; unsigned int count = 0; insert_ptr = new Node; insert_ptr->data = value; insert_ptr->next = NULL; //Iterates through linked list to index value position for (cursor = head_ptr; count < index; cursor = cursor->next) { count++; precur = cursor; insert_ptr->next = cursor; } precur->next = insert_ptr; insert_ptr->next = cursor; many_nodes++; } }