Jump to content

Manipulation of program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
hsmith1976

hsmith1976

    Newbie

  • Members
  • Pip
  • 4 posts
I have a program that I have written that now I have to rewrite. Instead of using a dynamic array I now have to use linked lists. Below are the instructions that I have been given.

     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");

         

}

     



#2
dragon_kcn

dragon_kcn

    Newbie

  • Members
  • PipPip
  • 21 posts
Hi.
I've read your massage and found some mistakes.

1. you didn't declare the struct correctly.
2. There is a name confliction, I mean a function name and a local variable name. pls find it out.
3. The while state in the main function would not work.
4. There are some fatal errors in your guest_create function. pls find out what those are.
5. Also there are many bugs in your code. So I can't point all of them.
Please watch carefully your code one line by one line.
Here is my advice.
You are distinguishing the head pointer of the linkd list from the member of one node.

Good luck.
Pls tell me again, I'm willing to help you.:thumbup1:
Cholnam, Kim./

#3
hsmith1976

hsmith1976

    Newbie

  • Members
  • Pip
  • 4 posts
how do I declare the structure correctly.

#4
dragon_kcn

dragon_kcn

    Newbie

  • Members
  • PipPip
  • 21 posts
This is what you declared.
struct [B]guest[/B]{

       int age;

       char lastname;

       struct [B]node[/B] *next;

};

Where does the item 'node' come from?
You should write it as 'guest' like this.
struct [B]guest[/B]{

       int age;

       char lastname;

       struct [B]guest[/B] *next;

};

Anyway you should match the two bold words.
Good luck.
Cholnam, Kim./

#5
ifuturz

ifuturz

    Newbie

  • Members
  • Pip
  • 3 posts
The structure correctly.