int main(int argc, char* argv[]) { printf("Let us formulate the problem statement to understand the deletion process. Given a key, delete the first occurrence of this key in linked list.\nTo delete a node from linked list, we need to do following steps.\n1) Find previous node of the node to be deleted.\n2) Changed next of previous node.\n3) Free memory for the node to be deleted.\nSince every node of linked list is dynamically allocated using malloc() in C, we need to call free() for freeing memory allocated for the node to be deleted.\n"); //struct node *head = NULL; //head = (struct node*)malloc(sizeof(_node)); _node *head = NULL; insert_element(&head,1); insert_element(&head,2); display_list(head); // printf(" number of nodes = %d"); insert_element(&head,7); insert_element(&head,8); insert_element(&head,9); insert_element(&head,12); insert_element(&head,111); display_list(head); //delete_node(&head,111); display_list(head); insert_element(&head,13); insert_element(&head,15); //head->next->next->next->next->next->next = head->next->next; int give_answer = detect_loop(head); //delete_node(&head,1); //display_list(head); //delete_node(&head,12); //display_list(head); //delete_node(&head,8); //display_list(head); return 0; }
void quick_sort (int l[], int left, int right) { int i, pivot; /* do nothing if array contains fewer than two elements */ if(left >= right) return; /* partition and move pivot element to v[0] */ swap(l, left, (left+right)/2); display_list(l, SIZE); pivot = left; printf("Pivot: %d\n", l[pivot]); /*scan all items from pivot+1 */ for (i=left+1; i<=right; i++) if (l[i] < l[left]) { printf("Compared for Swap: %d %d\n", l[i], l[left]); swap (l, ++pivot, i); display_list(l, SIZE); } swap(l, left, pivot); display_list(l, SIZE); printf("Recursion Starts\n"); quick_sort (l, left, pivot-1); quick_sort (l, pivot+1, right); }
/* * Main method that runs operations - doesn't return values * Reads in 3 lines: Operations, first numeric string, second numeric string * Usage: reads in from stdin non-negative numeric strings to do addition or subtraction operations */ int main( void ){ size_t op_size = 4; // max add or size_t line_size = 80; // Temporary size char *op, *first, *second, *first_cpy, *second_cpy, *result; // Cant allocate space if( !(op = calloc(op_size, sizeof(char))) || !(first = calloc(line_size, sizeof(char))) || !(second = calloc(line_size, sizeof(char)))){ fprintf(stderr, "Error: Allocating memory\n"); return 1; } getline(&op, &op_size, stdin); // Gets the operator if((strcmp(op, "add\n") == 0) || (strcmp(op, "sub\n") == 0)){ // Not accepted string getline(&first, &line_size, stdin); // reads in first numeric string first_cpy = input(first); free(first); // already have copy now if(first_cpy == NULL){ free(op); return 1; } getline(&second, &line_size, stdin); // reads in second numeric string second_cpy = input(second); free(second); // already have copy now if(second_cpy == NULL){ free(op); free(first_cpy); return 1; } int result_size = (strlen(first_cpy) >= strlen(second_cpy)) ? (strlen(first_cpy)) : strlen(second_cpy); if(!(result = calloc(result_size + 1, sizeof(char)))){ // 1 more in case has carry or negative fprintf(stderr, "Error: Creating memory\n"); return 1; } if(strcmp(op, "add\n") == 0){ // Doing add operation int add_status = 0; if((add_status = add(first_cpy, second_cpy, result)) == 1){ printf("1"); display_list(result); }else if(add_status == 0){ display_list(result);} }else{ // subtraction int sub_status = 0; if((sub_status = sub(first_cpy, second_cpy, result)) == 1){ printf("-"); display_list(result); }else if(sub_status == 0){ display_list(result); } } }else{ // Invalid op operation fprintf(stderr, "Error: 1st line not equal to 'add' or 'sub'\n"); free(op); free(first); free(second); return 1; } free(op); free(first_cpy); free(second_cpy); free(result); return 0; }
/** * Remove a timer from the list of active timers. * * @param timerToRemove pointer on the timer to remove * * WARNING: timerToRemove MUST NOT be null (rem: static function ) * */ static void remove_timer (T_TIMER_LIST_ELT* timerToRemove) { T_TIMER_LIST_ELT* removePoint; #ifdef __DEBUG_OS_ABSTRACTION_TIMER _log ("\nINFO : remove_timer - start: removing 0x%x to expire at %d (now = %d)", (uint32_t) timerToRemove, timerToRemove->desc.expiration, _GET_TICK()); display_list(); #endif if ( NULL != g_CurrentTimerHead ) { removePoint = g_CurrentTimerHead; while ( NULL != removePoint ) { if ( timerToRemove == removePoint ) { if ( NULL != timerToRemove->next ) { timerToRemove->next->prev = timerToRemove->prev ; } if ( NULL != timerToRemove->prev ) { timerToRemove->prev->next = timerToRemove->next ; } } removePoint = removePoint->next ; } if ( timerToRemove == g_CurrentTimerHead ) { g_CurrentTimerHead = g_CurrentTimerHead->next; if (NULL != g_CurrentTimerHead) { g_CurrentTimerHead->prev = NULL; } } } else { #ifdef __DEBUG_OS_ABSTRACTION_TIMER _log ("\nERROR : remove_timer : list of active timer is empty "); #endif panic (E_OS_ERR); } /* clean-up links */ timerToRemove->prev = NULL; timerToRemove->next = NULL; timerToRemove->desc.status = E_TIMER_READY; #ifdef __DEBUG_OS_ABSTRACTION_TIMER _log ("\nINFO : remove_timer - end "); display_list(); #endif }
int main(void) { struct NODE *llist = (struct NODE*)malloc(sizeof(struct NODE)), *sentinal = (struct NODE*)malloc(sizeof(struct NODE)); int number; for(number = 1; number < 150; number++){ append_node(llist, number); } display_list(llist); sentinal->next = llist; delete_node(sentinal, 50); display_list(llist); return number; }
//---------main------------------- int main(){ char end; int operation; do{ display_menu(); scanf("%d",&operation); switch(operation){ case 1 : display_list(); break; case 2 : push(); break; case 3 : pop(); break; default : printf("Error : Wronge number entered.\n"); break; } printf("\n\nDo you want to end program? Enter 'y' or 'n' : "); end=getche(); printf("\n\n"); }while(end!='y'); }
int main(int argc, char **argv) { Node *head = NULL; char **filenames = init_filenames(); char arg; char *listfile = "index"; char *namefile = "filenames"; while ((arg = getopt(argc,argv,"i:n:")) > 0){ switch(arg){ case 'i': listfile = optarg; break; case 'n': namefile = optarg; break; default: fprintf(stderr, "Bad arguments for printindex\n"); exit(1); } } read_list(listfile, namefile, &head, filenames); display_list(head, filenames); return 0; }
int insert_after(struct node *newNode) { display_list(); printf("\nEnter the no. of element after which you want to insert.\n"); int n; scanf("%d",&n); struct node *temp; temp=head.next; int r=1; while(temp!=NULL) { if(r!=n) { r++; temp=temp->next; continue; } else { if(temp->next==NULL) { temp->next=newNode; newNode->prev=temp; break; } else{ newNode->next=temp->next; newNode->prev=temp; temp->next->prev=newNode; temp->next=newNode; break; } } } }
int main(int ac, char **av) { char buffer[2048]; struct termios t; char read_char[5]; t_l *l; *read_char = 0; l = NULL; if (tgetent(buffer, getenv("TERM")) < 1) return (-1); term_no_canon(t); tputs(tgetstr("ti", NULL), 1, ft_int_putchar); tputs(tgetstr("vi", NULL), 1, ft_int_putchar); if ((l = (t_l *)malloc(sizeof(t_l))) == NULL) return (-1); ft_init_list(l); display_list(l, ac, av); tputs(tgetstr("ks", NULL), 1, ft_int_putchar); read_keys(read_char); tputs(tgetstr("ke", NULL), 1, ft_int_putchar); tputs(tgetstr("ve", NULL), 1, ft_int_putchar); tputs(tgetstr("te", NULL), 1, ft_int_putchar); term_canon(t); return (0); }
/* * Main method that runs all the iterations * Reads sequence of integers and displays the counts of the value occurance * Returns 0 is no errors, 1 if run into input, or memory issues */ int main( void ){ int status, count; while((status = scanf("%d",&count)) != EOF){ if(status == 0){ // Initial count is a non-int fprintf(stderr, "Didn't read any input\n"); return 1; } if(count < 1){ fprintf(stderr, "Count must be positive\n"); return 1; } int i = 0; struct occurance *values = NULL; for(i = 0; i < count; i++){ // Assigns all the values int value; if((status = scanf("%d", &value) == 0) || status == EOF){ fprintf(stderr, "Invalid input or too few input\n"); free_list(values); return 1; } values = add(values, value); // adds & updates linked list } display_list(values); // Displays occurances free_list(values); // free in memory } return 0; }
int main() { int choice=0; while(1) { printf("\n1.Insert new element\n2.Delete an element\n3.Display linked list\n4.Reverse display\n0.Exit\n"); scanf("%d",&choice); switch(choice) { case 0:return 0; case 1: { create_node(); break; } case 2: { delete_node(); break; } case 3: { display_list(); break; } case 4: { reverse_display(); break; } default:printf("\nWrong choice selected.Try again\n"); } } }
int ask_position(struct node *newNode) { printf("\nThe linked list is "); int check=display_list(); if(check==1) { printf("\nThe inserted element becomes the first element\n"); head.next=newNode; } else { printf("\nEnter a choice\n1.Insert after an element\n2.Insert before an element\n"); int choice; scanf("%d",&choice); switch(choice) { case 1: { insert_after(newNode); break; } case 2: { insert_before(newNode); break; } default:printf("\nWrong choice.New element not inserted.\n"); } } }
int control_size(t_elem *l) { char *termtype; int lin; termtype = getenv("TERM"); if (!termtype) ft_env_error(); tgetent(NULL, termtype); tputs(tgetstr("cl", NULL), 1, int_char); lin = tgetnum("li"); l->len = ft_list_len(l); if (l->len > lin - 1) { tputs(tgetstr("cl", NULL), 1, int_char); ft_putendl_red("Please Resize window"); return (0); } else { tputs(tgetstr("cl", NULL), 1, int_char); display_list(l); } return (1); }
node *remove_member(node *head) { char ch[20]; node *ptr=NULL,*prev=head; printf("\t\t\tHere you can remove existing member\n\n"); printf("Name of all members (for reference) :-\n\n"); display_list(head,0); printf("\n"); while(ptr==NULL) { printf("Enter name of member : "); scanf(" %[^\n]",ch); ptr=find_user_node(head,ch); if(ptr==NULL) printf("User not found !! Try Again !!\n\n"); } per_tot-=ptr->per; while(prev->next!=ptr) prev=prev->next; if(ptr==head) head=head->next; else prev->next=ptr->next; free(ptr); return head; }
void main(){ int m=2,n=3; struct node *t1=create_node(2); head=t1; t1->next=create_node(5); t1->next->next=create_node(7); t1->next->next->next=create_node(9); t1->next->next->next->next=create_node(11); t1->next->next->next->next->next=create_node(13); t1->next->next->next->next->next->next=create_node(15); display_list(); alter_list(m,n); display_list(); }
void insertion_sort (int l[], int size) { int i, index; for (i=1; i<size; i++) { index = i; printf("index=%d ", index); /* Keep pushing the index element to the left */ while (index > 0 && (l[index] < l[index-1])) { swap(l, index, index-1); display_list(l, SIZE); index--; } printf("List after pass: "); display_list(l, SIZE); } }
int main(void) { int num = 0; int input = 1; int retval = 0; //struct NODE *llist; miNodo *llist; llist = (struct NODE *)malloc(sizeof(struct NODE)); llist->number = 0; llist->next = NULL; while(input != 0) { printf("\n-- Menu Selection --\n"); printf("0) Quit\n"); printf("1) Insert\n"); printf("2) Delete\n"); printf("3) Search\n"); printf("4) Display\n"); scanf("%d", &input); switch(input) { case 0: default: printf("Goodbye ...\n"); input = 0; break; case 1: printf("Your choice: `Insertion'\n"); printf("Enter the value which should be inserted: "); scanf("%d", &num); append_node(llist, num); break; case 2: printf("Your choice: `Deletion'\n"); printf("Enter the value which should be deleted: "); scanf("%d", &num); delete_node(llist, num); break; case 3: printf("Your choice: `Search'\n"); printf("Enter the value you want to find: "); scanf("%d", &num); if((retval = search_value(llist, num)) == -1) printf("Value `%d' not found\n", num); else printf("Value `%d' located at position `%d'\n", num, retval); break; case 4: printf("You choice: `Display'\n"); display_list(llist); break; } /* switch */ } /* while */ free(llist); return(0); }
int display_qvariant(char *buf) { char *orig_buf=buf; uint32_t type = *((uint32_t*)buf); type=ntohl(type); buf+=4; char null=*buf; buf++; if(null) { //Nothing to do } switch(type) { case 1: buf+=display_bool(buf); break; case 2: case 3: buf+=display_int(buf, type); break; case 7: //UTF16 byte buf+=display_short(buf); break; case 8: buf+=display_map(buf); break; case 9: buf+=display_list(buf); break; case 10: buf+=display_string(buf); break; case 11: buf+=display_stringlist(buf); break; case 12: buf+=display_bytearray(buf); break; case 15: buf+=display_time(buf); break; case 16: buf+=display_date(buf); break; case 127: //User type ! buf+=display_usertype(buf); break; case 133: buf+=display_short(buf); break; default: printf("Unknown QVariant type: %d\n", type); exit(-1); break; }; return buf-orig_buf; }
static void display_lambda(struct StXLambda *lambda, StObject port) { St_WriteCString("(lambda ", port); if (lambda->dotted) { size_t len = ST_VECTOR_LENGTH(lambda->vars); if (len == 1) { St_Display(ST_VECTOR_DATA(lambda->vars)[0], port); } else { St_WriteCString("(", port); for (size_t i = 0; i < len - 1; i++) { St_Display(ST_VECTOR_DATA(lambda->vars)[i], port); St_WriteCString(" ", port); } St_WriteCString(". ", port); St_Display(ST_VECTOR_DATA(lambda->vars)[len], port); St_WriteCString(")", port); } } else { St_WriteCString("(", port); display_list(lambda->vars, port); St_WriteCString(")", port); } size_t len = ST_VECTOR_LENGTH(lambda->defvars); if (len != 0) { St_WriteCString(" ", port); } for (size_t i = 0; i < len; i++) { St_WriteCString("(define ", port); St_Display(ST_VECTOR_DATA(lambda->defvars)[i], port); St_WriteCString(" ", port); St_Display(ST_VECTOR_DATA(lambda->defvals)[i], port); St_WriteCString(")", port); if (i < len - 1) { St_WriteCString(" ", port); } } if (ST_VECTOR_LENGTH(lambda->body) != 0) { St_WriteCString(" ", port); } display_body(lambda->body, port); St_WriteCString(")", port); }
void on_split_clicked(GtkButton * button, gpointer user_data) { gint w, h; view_mode = SPLIT_VIEW; gtk_widget_show(tree1_w); gtk_window_get_default_size(GTK_WINDOW(main_wnd), &w, &h); gtk_paned_set_position(GTK_PANED(hpaned), w / 2); if (tree2) gtk_tree_store_clear(tree2); display_list(); }
int apply_flags(t_file *file, int *apply, char *path) { if (apply[3] == 1) display_flag_d(&file, apply, path); if (apply[1] == 1) my_rev_list(&file); if (apply[0] == 1) display_detailed_list(&file); else display_list(&file); }
/** * Handles changing a tire. * @param reason INVALID_TARGET if there's no wheel in the selected square, * LACK_TOOLS if the player is missing a tool. */ void veh_interact::do_tirechange(task_reason reason) { werase( w_msg ); int msg_width = getmaxx(w_msg); switch( reason ) { case INVALID_TARGET: mvwprintz(w_msg, 0, 1, c_ltred, _("There is no wheel to change here.")); wrefresh (w_msg); return; case LACK_TOOLS: fold_and_print(w_msg, 0, 1, msg_width - 2, c_ltgray, _("To change a wheel you need a <color_%1$s>wrench</color> and a <color_%2$s>jack</color>."), has_wrench ? "ltgreen" : "red", has_jack ? "ltgreen" : "red"); wrefresh (w_msg); return; } mvwprintz(w_mode, 0, 1, c_ltgray, _("Choose wheel to use as replacement:")); wrefresh (w_mode); int pos = 0; while (true) { sel_vpart_info = &(wheel_types[pos]); bool is_wheel = sel_vpart_info->has_flag("WHEEL"); display_list (pos, wheel_types); itype_id itm = sel_vpart_info->item; bool has_comps = crafting_inv.has_amount(itm, 1); bool has_tools = has_jack && has_wrench; werase (w_msg); wrefresh (w_msg); char ch = input(); // See keypress.h int dx, dy; get_direction (dx, dy, ch); if ((ch == '\n' || ch == ' ') && has_comps && has_tools && is_wheel) { sel_cmd = 'c'; return; } else { if (ch == KEY_ESCAPE || ch == 'q' ) { werase (w_list); wrefresh (w_list); werase (w_msg); break; } } if (dy == -1 || dy == 1) { pos += dy; if (pos < 0) { pos = wheel_types.size() - 1; } else if (pos >= wheel_types.size()) { pos = 0; } } } }
void ListView::list (List *list) { if (f_the_list != NULL) f_the_list-> RemoveDependent ((notify_handler_t) &ListView::list_changed, List::APPENDED); f_the_list = list; f_the_list->AddDependent ((notify_handler_t) &ListView::list_changed, List::APPENDED); display_list(); }
void ListView::display() { if (f_shell == NULL) { create_ui_objects(); display_list(); } f_shell->Popup(); f_shell->DeIconify(); }
int main(int argc, char **argv) { Node *head = NULL; char **filenames = init_filenames(); char ch; char *indexfile = "index"; char *namefile = "filenames"; while((ch = getopt(argc, argv, "i:n:")) != -1) { switch (ch) { case 'i': indexfile = optarg; break; case 'n': namefile = optarg; break; default: fprintf(stderr, "Usage: indexfile [-i FILE] [-n FILE ] FILE...\n"); exit(1); } } while(optind < argc) { FILE *fname; if((fname = fopen(argv[optind], "r")) == NULL) { perror("Name file"); exit(1); } char line[MAXLINE]; char splitBy[] = " \t\n"; char *token; char *cleaned_token; while ((fgets(line, MAXLINE, fname)) != NULL){ token = strtok(line, splitBy); while (token != NULL) { cleaned_token = clean_word(token); //only add_word if not empty string if (strcmp(cleaned_token, "") != 0) { head = add_word(head, filenames, cleaned_token, argv[optind]); } token = strtok(NULL, splitBy);} } optind++; } write_list(namefile, indexfile, head, filenames); display_list(head, filenames); return 0; }
void display_structure(liste linked_list){ int i,n; if(linked_list.structure == TYPE_STRUCT_CL2LT){ n = linked_list.nClauses; } else{ n = linked_list.nLitteraux; } for(i=0;i<n;i++){ display_list(linked_list,i); } }
int main () { list l = create_list(); prepend (&l, (void*)2); append (&l, (void*)17); insert_in_list (&l, 3, (void*)1); insert_in_list (&l, 2, (void*)14); display_list(l); printf ("\n"); display_list_reverse(l); printf ("\n"); delete_from_list (&l,2); display_list(l); printf ("\n"); display_list_reverse(l); printf ("\n"); set (l, 3, (void*)5); display_list(l); printf ("\n"); display_list_reverse(l); return 0; }
void edit_member_info(node *head) { char ch[20],option; node *ptr=NULL; printf("Name and personal amount of all members (for reference) :-\n\n"); display_list(head,1); printf("\n"); while(ptr==NULL) { printf("Enter name of member : "); scanf(" %[^\n]",ch); ptr=find_user_node(head,ch); if(ptr==NULL) printf("User not found !! Try Again !!\n\n"); } while(1) { CLS; printf("\t\t\tHere you can edit member info\n\n"); printf("Choose Option :-\n\n"); printf("1 - Edit Name\n2 - Edit Personal Amount\n\tBackspace - Edit Menu\n"); option = getch(); CLS; switch(option) { case '1': printf("\t\t\tHere you can edit member name\n\n"); printf("Old Name : %s\n",ptr->name); printf("Enter New Name : "); scanf(" %[^\n]",ptr->name); printf("\nName Changed !!"); hold_screen(); break; case '2': printf("\t\t\tHere you can edit member personal expenditure amount\n\n"); per_tot-=ptr->per; printf("Old personal expenditure of %s : %f\n",ptr->name,ptr->per); printf("Enter new personal expenditure of %s : ",ptr->name); scanf(" %f",&ptr->per); per_tot+=ptr->per; grp_tot=bill_amt-per_tot+entry-sub_grp_total;//Set New Group Total printf("\nPersonal expenditure amount changed !!"); hold_screen(); break; case 8: return; } } }
void selection_sort(int l[], int size) { int i, j, min; for (i=0; i<size-1; i++) { min = i; for (j=i+1; j<size; j++) if (l[j] < l[min]) min = j; swap(l, i, min); printf("List after pass: "); display_list(l, SIZE); } }
static void toggle_sym_value(struct menu *menu) { if (!menu->sym) return; sym_toggle_tristate_value(menu->sym); if (view_mode == FULL_VIEW) update_tree(&rootmenu, NULL); else if (view_mode == SPLIT_VIEW) { update_tree(browsed, NULL); display_list(); } else if (view_mode == SINGLE_VIEW) display_tree_part(); //fixme: keep exp/coll }