int main(int argc, char *argv[]) { struct node *list; int i; /* 4 <-> 3 <-> 2 <-> 1 <-> 0 */ for (i = 0; i < 5; i++) add_as_head(&list, i); /* 4 <-> 3 <-> 2 <-> 1 <-> 0 <-> 99 */ add_as_tail(&list, 99); /* 4 <-> 3 <-> 2 <-> 1 <-> 0 */ delete_node_by_value(&list, 99); /* 3 <-> 2 <-> 1 <-> 0 */ delete_head(&list); /* 3 <-> 1 <-> 0 */ delete_node(list->next); /* 3 <-> 1 */ delete_tail(&list); disp_list(list); free_list(&list); return EXIT_SUCCESS; }
int main() { int ile,i,var; char command[16]; scanf("%u ", &ile); for(i= 0; i< ile;i++){ scanf("%s %u", &command, &var); if(strcmp(&command,"insert")==0){ append(var); }else if(strcmp(&command,"delete_head")==0){ delete_head(); }else if(strcmp(&command,"delete_tail")==0){ delete_tail(); }else if(strcmp(&command,"print")==0){ show(); }else if(strcmp(&command,"count")==0){ count(); }else if(strcmp(&command,"insert_head")==0){ add_new_head(var); } } return 0; }
//移动 void move(snake_body *snake) { //添头 add_head(snake,snake->direction); //去尾 delete_tail(snake); }
int move_snake(STRU_B(*p)[N], char drection) { int i, j, NH = 0; for (i = 0; i < M; i++) for (j = 0; j < N; j++) { if (strcmp(p[i][j].part, bNONE) == 0 || strcmp(p[i][j].part, sBODY) == 0); else if (strcmp(p[i][j].part, sHEAD) == 0 && NH == 0) { p[i][j].drection = drection; switch (drection) { case UP:if (strcmp(p[i - 1][j].part, bBORDER) == 0) { lose(p); return 0; } if (strcmp(p[i - 1][j].part, bFOOD) == 0) add_food(p); else delete_tail(p); p[i - 1][j] = p[i][j]; break; case DOWN:if (strcmp(p[i + 1][j].part, bBORDER) == 0) { lose(p); return 0; } if (strcmp(p[i + 1][j].part, bFOOD) == 0) add_food(p); else delete_tail(p); p[i + 1][j] = p[i][j]; break; case LEFT:if (strcmp(p[i][j - 1].part, bBORDER) == 0) { lose(p); return 0; } if (strcmp(p[i][j - 1].part, bFOOD) == 0) add_food(p); else delete_tail(p); p[i][j - 1] = p[i][j]; break; case RIGHT:if (strcmp(p[i][j + 1].part, bBORDER) == 0) { lose(p); return 0; } if (strcmp(p[i][j + 1].part, bFOOD) == 0) add_food(p); else delete_tail(p); p[i][j + 1] = p[i][j]; break; } strcpy(p[i][j].part, sBODY); NH++; } } return 1; }
void snake::move_snake() { if(direction!=0) { if(direction==UP){node n('*',head->Pos_x,head->Pos_y-1); insert_body(n);} else if(direction==DOWN){node n2('*',head->Pos_x,head->Pos_y+1); insert_body(n2);} else if(direction==LEFT){node n3('*',head->Pos_x-2,head->Pos_y); insert_body(n3);} else if(direction==RIGHT){node n4('*',head->Pos_x+2,head->Pos_y); insert_body(n4);} if(head->Pos_x!=apple_x||head->Pos_y!=apple_y)//判断是否吃苹果 delete_tail(); else creat_food(); show_food(); } }
struct node *delete_node(struct dl_list *list, struct node *to_die){ if(!to_die->next){ //data == list->tail return delete_tail(list); } else if(!to_die->prev){ //data == list->head return delete_head(list); } else{ to_die->prev->next = to_die->next; to_die->next->prev = to_die->prev; --(list->size); to_die->next = to_die->prev = NULL; return to_die; } }
//delete tail if input is larger than list size //delete the element immediately after the zth node struct node *delete_at(struct dl_list *list, int z){ if(is_empty(list)){ //nothing to delete return NULL; } else if(z == 0){ return delete_head(list); } else if(z >= list->size){ return delete_tail(list); } else{ struct node *temp = list->head; while(z-- > 0){ temp = temp->next; } return delete_node(list, temp); } }
void delete_all(struct dl_list *list, const void *key, comp eq){ //nothing to delete if(is_empty(list)){ return; } //list not empty struct node *cur; struct node *temp; for(cur = list->head; cur != NULL; ){ //Key found if(eq(cur->data, key)){ //cur is head if(!cur->prev){ destroy_node(delete_head(list)); cur = list->head; } //cur is tail else if(!cur->next){ destroy_node(delete_tail(list)); cur = NULL; } else{ cur->prev->next = cur->next; cur->next->prev = cur->prev; temp = cur; cur = cur->next; destroy_node(temp); --(list->size); } } //key not found else{ cur = cur->next; } } }
CandidateList::~CandidateList(){ while (!empty()) { delete_tail(); } }
void init_menu_delete(AList_WS* list) { for (int i = 0; i < 4; ++i) printf("%s\n", MAIN_MENU[i]); printf("\ta. Delete data at Head\n"); printf("\tb. Delete data at Tail\n"); printf("\tc. Delete data at an Index\n"); printf("\td. Delete first occurrence of a given data\n"); printf("\te. Delete first occurrence of a given data\n"); printf("\tf. Go Back\n"); for (int i = 4; i < 8; ++i) printf("%s\n", MAIN_MENU[i]); printf("\vPLease enter option: "); char* input = (char*)(malloc(100)); memset(input, '-', 100); scanf("%s", input); char ch = input[0]; if (isdigit(input[0]) && isdigit(input[1])) ch = 'X'; switch (ch) { case '1': case 'a': case 'H': { delete_head(list); break; } case '2': case 'b': case 'T': { delete_tail(list); break; } case '3': case 'c': case 'I': { printf("Please enter the index"); size_t pos; scanf("%lu", pos); delete_data_at(list, pos); break; } case '4': case 'd': case 'D': { printf("Please enter the value you want to delete: "); double data; scanf("%lf", &data); delete_data(list, data); break; } case '5': case 'e': case 'A': { printf("Please enter the value you want to delete: "); double data; scanf("%lf", &data); do { delete_data(list, data); }while (find(list,data) != NOT_FOUND); break; } case '6': case 'f': case 'B': break; default: printf("Please enter a valid choice.\n\n"); init_menu_insert(list); } }