- struct guest* guest_create( char* lastname, int age, );
o allocates a new struct guest data structure which address you will return
o Set the lastname field to a newly allocated copy of the string pointed to by
the lastname parameter
o Set the age field to the value in the age parameter
o Set the next pointer to NULL
void guestlist_deallocate (struct guest* p);
o Deallocates all the elements of the struct guest linked list pointed to by p
struct guest * guestlist_readall (int number);
o This function will read the name and age of number guests from the user
o For each guest, create a new struct guest data structure, assign appropriate
value to its fields (the age value and a copy of the lastname typed by the
user), and link it to the end of a linked list of all struct guest read so far.
o You will return a pointer to the first struct guest of the linked list
o Use the guest_create function to help you write this one
void guestlist_display(struct guest* p);
o Display information about each guest (name and age) on the screen
The code I am trying to manipulate is as follows and everything in this program works just fine.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct guest {
int age;
char lastname[30];
};
struct guest* guestlist_allocate(int number);
void guestlist_deallocate(struct guest* p, int number);
void guestlist_readall(struct guest* p, int number);
void guestlist_display(struct guest* p, int number);
int main()
{
int n = 0;
struct guest* guestlist;
int number;
struct guest* p;
printf("How many guests do you need to enter? ");
scanf("%d", &n);
guestlist = guestlist_allocate(n);
while(n <= 0)
{
printf("Error you entered a negative number!");
break;
}
printf("Reading data for %d guests\n", n);
guestlist_readall(guestlist, n);
printf("Displaying data for %d guests\n", n);
guestlist_display(guestlist, n);
guestlist_deallocate(guestlist, n);
system("PAUSE");
return 0;
}
struct guest* guestlist_allocate(int number)
{
char lastname;
int age = 0;
char buffer[30];
struct guest *guestlist;
guestlist = malloc (sizeof(struct guest) * number);
return guestlist;
}
void guestlist_deallocate(struct guest* p, int number)
{
int i;
struct guest *guestlist;
for (i = 0; i < number; i++)
free(p);
}
void guestlist_readall(struct guest* p, int number)
{
char lastname;
int age=0;
int i;
char buffer[30];
int name_length;
for( i = 0; i < number; i++)
{
printf("\tGuest #%d:\n ", i);
printf("\t\tEnter lastname: ");
scanf("%29s", buffer);
name_length = strlen(buffer);
strcpy(p[i].lastname, buffer);
printf("\t\tEnter age: ");
scanf("%ld", &p[i].age);
}
}
void guestlist_display(struct guest* p, int number)
{
int i;
for(i = 0; i < number; i++)
printf("\tGuest #%d: lastname = %s\tage = %ld\n", i, p[i].lastname, p[i].age);
}
Below is what I have come up with so far on the manipulation of the code. This code does not compile and I am not really sure if I am even on the right track. Any assistance that I can get would be greatly appreciated. I DO NOT just want the answers I want some direction on how to do what I am suppose to do.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct guest{
int age;
char lastname;
struct node *next;
};
struct guest* guest_create(char* lastname, int age);
void guestlist_deallocate(struct guest* p);
struct guest * guestlist_readall(int number);
void guestlist_display(struct guest* p);
int main()
{
int n = 0;
struct guest* guestlist;
int number;
struct guest* p;
printf("How many guests do you need to enter? ");
scanf("%d", &n);
struct guest *guest_create = NULL;
while(n <= 0)
{
printf("Error you entered a negative number!");
break;
}
printf("Reading data for %d guests\n", n);
guestlist_readall(number);
printf("Displaying data for %d guests\n", n);
guestlist_display(p);
guestlist_deallocate(p);
system("PAUSE");
return 0;
}
struct guest* guest_create(char* lastname, int age)
{
struct guest *tmp;
if(lastname == NULL)
{
lastname = (struct guest*)malloc(sizeof(struct guest));
if(lastname == NULL)
{
printf("Error! memory is not available\n");
exit(0);
}
lastname-> age = age;
lastname-> next = lastname;
}
else
{
tmp = lastname;
while (tmp-> next != lastname)
tmp = tmp -> next;
tmp -> next = (struct guest *)malloc(sizeof(struct guest));
if(tmp -> next == NULL)
{
printf("Error! memory is not available\n");
exit(0);
}
tmp = tmp->next;
tmp -> age = age;
tmp -> next = lastname;
}
return lastname;
}
void guestlist_deallocate(struct guest* p)
{
struct guest *current, *tmp;
current = lastname->next;
lastname->next = NULL;
while(current != NULL)
{
tmp = current -> next;
free(current);
current = tmp;
}
}
struct guest * guestlist_readall(int number)
{
struct guest {
int age;
struct node *next;
};
char lastname;
int age=0;
int i;
char buffer[30];
int name_length;
for( i = 0; i < number; i++)
{
printf("\tGuest #%d:\n ", i);
printf("\t\tEnter lastname: ");
scanf("%29s", buffer);
name_length = strlen(buffer);
strcpy(lastname, buffer);
printf("\t\tEnter age: ");
scanf("%ld", &age);
}
}
void guestlist_display(struct guest* p)
{
struct guest *current
current = lastname;
if(current != NULL)
{
do
{
printf("%d\t",current->age);
current = current->lastname;
}while(current != lastname);
printf("\n");
}
else
printf("The list is empty\n");
}


Sign In
Create Account

Back to top









