int main(){ struct listNode *head; printf("Elements in list are :%d\n",listLength(head)); printList(head); insertInLinkedList(&head,2,3); insertInLinkedList(&head,5,3); insertInLinkedList(&head,9,1); insertInLinkedList(&head,10,3); printf("Elements in list are :%d\n",listLength(head)); printList(head); deleteNodeFromLinkedList(&head,3); printList(head); return 0; }
/** *main() begins */ int main(int argc, char const *argv[]) { node * head = NULL; int i,n,count; char c,ans,start; system("clear"); printf("Enter number of people\n"); scanf("%d",&n); for (i = 0; i < n; ++i) { printf("Enter a letter for person (all must be distinct):\n"); scanf("\n%c",&c); insertInLinkedList(&head,c); //head passed as reference to change its value inside main from func } printf("Enter start person\n"); scanf("\n%c",&start); printf("Enter number of skip\n"); scanf("%d",&count); ans = josephus(head,start,count,n); printf("The person remaining is : %c\n",ans); return 0; }
/* ------------------------------------------------------------------------- * * Parse a CSV file containing cities. * This CSV must start with a header row and have three columns: * 1) Name: the name of the city (delimited with double quotes) * 2) Latitude: the latitude of the city in degree (floating-point value) * 3) Longitude: the longitude of the city in degree (floating-point value) * * PARAMETERS * filename A null-terminated string containing the name of the CSV file * * RETURN * cities A linked list containing the cities * ------------------------------------------------------------------------- */ static LinkedList* parseCsv(const char* filename) { // Opens the file FILE* fileObj = fopen(filename, "r"); if (fileObj == NULL) { fprintf(stderr, "Could not open file '%s'. Exiting...\n", filename); exit(EXIT_FAILURE); } LinkedList* cities = newLinkedList(); if (!cities) { fprintf(stderr, "Allocation error. Exiting...\n"); exit(EXIT_FAILURE); } // Start reading the file, line by line (N.B.: fgets() reads until next \n or EOF character) char line[LINE_SIZE]; size_t nbLine = 0; size_t nbCity = 0; char delim = ','; char stringDelim = '"'; City* city; size_t currChar, nameStart, nameEnd, nameLen, latitudeStart, longitudeStart; while (fgets(line, sizeof(line), fileObj) != NULL) { if (nbLine == 0) { nbLine++; continue; } // Skip header city = malloc(sizeof(City)); if(!city) { fprintf(stderr, "Allocation error at line %zu. Exiting...\n", nbLine); exit(EXIT_FAILURE); } city->latitude = 0.0; city->longitude = 0.0; city->name = NULL; currChar = 0; // Skip anything before the first string delimiter of the city name while (line[currChar++] != stringDelim) { } nameStart = currChar; // Find position of the end of the city name while (line[currChar] != stringDelim) { currChar++; } nameEnd = currChar, nameLen = nameEnd - nameStart; // Index one past the end of the name // City name city->name = malloc(sizeof(char) * (nameLen + 1)); if (!city->name) { fprintf(stderr, "Allocation error: city name at line %zu. Exiting...\n", nbLine); exit(EXIT_FAILURE); } memcpy(city->name, line + nameStart, sizeof(char) * nameLen); city->name[nameLen] = '\0'; // Skip until the latitude start while (line[currChar++] != delim) { } latitudeStart = currChar; // Latitude city->latitude = strtod(line + latitudeStart, NULL); // Skip until delimiter while (line[currChar++] != delim) { } longitudeStart = currChar; // Longitude city->longitude = strtod(line + longitudeStart, NULL); if(!insertInLinkedList(cities, (const void*)city)) { fprintf(stderr, "Allocation error: insertion at line %zu. Exiting...\n", nbLine); exit(EXIT_FAILURE); } nbLine++; nbCity++; } fclose(fileObj); return cities; }