167k views
2 votes
For this lab assignment, we want you to develop a struct to represent the places you visit. The struct should have a variable to hold the name of the place you visited, and another variable for the date you visited. Since we want you to connect these places with each other, a pointer would also be needed in the struct. Thus, no use of arrays.

We then want you to ask a user to enter the places he/she visited. The user can enter -1 to indicate that she/he has no more data to enter.
Whenever, the user enters info about a place visited, you must:
1. Allocate memory for the new data as a struct.
2. Add this struct to the beginning of the places you visited.
3. Hold a pointer to the last visited place.
4. Each visited place must hold a pointer pointing to the place visited before, except the first visited place where it should point to NULL.
5. Write a function to print all the places you visited and the date of the visit.
6. Write another function to print all the places according to their name as if they are sorted in a dictionary, ascending. Here, you can come up with your own algorithm.

1 Answer

3 votes

Final answer:

To manage user-entered visit data, use a linked list struct in C with fields for the place name, visit date, and a next pointer. Data entry ends with '-1'. Structs are dynamically allocated, added at the beginning, and a function prints all the places. Another function sorts and prints places by name.

Step-by-step explanation:

To create a linked list structure representing places visited by a user, you would define a struct in C. This struct must contain a string (char array or char pointer) for the place name, a string or suitable structure for the date, and a pointer to the next place. Here's an example of how you could define such a struct:

typedef struct Place { char* name; char* date; struct Place* next; } Place;

As for the operations:

Here is a skeleton for the print function:

void printPlaces(Place* head) { Place* current = head; while(current != NULL) { printf("%s, visited on %s\\", current->name, current->date); current = current->next; } }

And for the sorting function, it is more complex since it requires reordering the linked list based on place names.

User Joshie
by
8.6k points