I'm trying to make make project for my programming class. All that's left is deleting elements and editing elements. Basicaly my question is how would I do it for my inner list? Or how would I delete single client from list? Would I first have to free the client's inner list if so how should I proceed to do so?

#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
struct date
{
    int day;
    int month;
    int year;
    struct date* next;
};
struct item
{
    char item_name[30];
   /* char item_state[30];
    double item_price;
    char item_status[30];
    double item_price_if_not;
    struct date *issue_date;*/
    struct item *next;
};
struct client
{
    char client_name[30];
    char client_last_name[30];
    struct item *item_data;
    struct client *next;
};
//ADD CLIENT//
void AddClient(struct client **head, char name[30], char last_name[30])
{
    if((*head) == NULL)
    {
        *head = malloc(sizeof(struct client));
        strcpy((*head)->client_name,name);
        strcpy((*head)->client_last_name,last_name);
        (*head)->next = NULL;
        (*head)->item_data = NULL;
    }
    else
{
    struct client *temp;
    temp = (*head);
    //             //
    while(temp->next)
    temp=temp->next;
    //              //
    (temp->next) = malloc(sizeof(struct client));
    strcpy(temp->next->client_name,name);
    strcpy(temp->next->client_last_name,last_name);
    temp->next->next = NULL;
    temp->next->item_data = NULL;
}
}
//ADD CLIENT END//
//////////////////
//FIND TO ADD//
struct client *FindToAdd(struct client *head, const char *named,const char *last_name)
{
    while(head != NULL)
    {
        if((strcmp(head->client_name,named) == 0 )&& (strcmp(head->client_last_name,last_name) == 0))
        {

            printf("Client found :%s %s\n",head->client_name,head->client_last_name);
            return head;

        }
        head = head->next;
    }
    return (NULL);
}
//FIND TO ADD END//
///////////////////
//ADD ITEM TO CLIENT//
void AddItemToClient1(struct client *head, char item_name[30]/*, char item_state[30], double price, char status[30], double price_if_not, int day, int month, int year*/)
{
    struct item* it = malloc(sizeof(struct item));
    strcpy(it->item_name, item_name);
    /*
    strcpy(it->item_state, item_state);
    strcpy(it->item_status, status);
    it->item_price = price;
    it->item_price_if_not = price_if_not;
    it->issue_date = malloc(sizeof(struct date));
    it->issue_date->day = day;
    it->issue_date->month = month;
    it->issue_date->year = year;
    */
    it->next = NULL;
    /*it->issue_date->next = NULL;*/
    if (head->item_data == NULL)
    {
        head->item_data = it;
    }
    else
    {
        struct item* node = head->item_data;
        while (node->next) node = node->next;
        node->next = it;
    }
}
//ADD ITEM TO CLIENT END//
//////////////////////////
// DISPLAY//
void Display(struct client *head)
    {int i=1,b=1;
    struct client *current;
    current = head;
        while(current != NULL)
        {
           printf("[%d] Client name:%s %s \n",i, current->client_name, current->client_last_name);
           struct item *CurrentItem = current->item_data;
           while(CurrentItem != NULL)
           {
             printf("-----------------------------------------------\n");
             printf("[%d] \n",b);
             printf("Item name: %s\n",CurrentItem->item_name);
             /*
             printf("Item state: %s\n",CurrentItem->item_state);
             printf("Item status: %s\n",CurrentItem->item_status);
             printf("Item price: %lf\n",CurrentItem->item_price);
             printf("Item price if debt wasnt paid: %lf\n",CurrentItem->item_price_if_not);
             printf("Issue date: %d/%d/%d\n",CurrentItem->issue_date->day,CurrentItem->issue_date->month,CurrentItem->issue_date->year);
             */
             printf("-----------------------------------------------\n");
             CurrentItem = CurrentItem->next;
             b++;
           }
           current=current->next;
           i++;
        }
    }
//DISPLAY END//
///////////////
//DELETE ITEM////
//DELETE ITEM END//
///////////////////
//DELETE WHOLE ITEM LIST LINKED TO CLIENT//
//DELETE WHOLE LIST LIST LINKED TO CLIENT END//
/////////////////////////
//ITEM IN LIST COUNT//
struct client *ItemCount(struct client *head)
{
    struct item *temp = head->item_data;
    int i=0;
    while(temp != NULL)
    {
        temp=temp->next;
        i++;
    }
    printf("List contains : %d items\n",i);
    return i;
}
int main()
{
    struct client* List = NULL;
         int choice;
         char name[30];
         char client_name[30];
         char client_named[30];
         char client_last_named[30];
          char client_last_name[30];
          char item_name[30];
          char item_state[30];
          char to_find_name[30];
          char to_find_last_name[30];
          double price;
          char status[30];
          double price_if_not;
          int day,month,year;
         scanf( "%d", &choice );
    while( choice != 8)
    {
        printf("Option 1: New person:\nOption 2: Display\nOption 3: Add item to person\n");
    switch( choice )
    {
    case 1:
     printf( "New person: \n" );
                printf("Provide client name\n");
                scanf("%s",client_name);
                printf("Provide client last name\n");
                scanf("%s",client_last_name);
                AddClient(&List,client_name,client_last_name);
                break;
    case 2:
     printf("Display\n");
                Display(List);
                break;
    case 3:
    printf("Add item to person: \n");
    struct client *temporal;
    printf("Name of desired person\n");
      scanf("%s",client_named);
    printf("Last name of desired person\n");
      scanf("%s",client_last_named);
    temporal = FindToAdd(List,client_named,client_last_named);
    printf("Item name\n");
      scanf("%s",item_name);

    AddItemToClient1(temporal,item_name);
    break;
    case 4:
     printf("Name of desired person\n");
      scanf("%s",to_find_name);
    printf("Last name of desired person\n");
      scanf("%s",to_find_last_name);
    FindToAdd(List,to_find_name,to_find_last_name);
    break;
    case 5:
    printf("Item counter\n");
        struct client *temporal1;
    printf("Name of desired person\n");
      scanf("%s",client_named);
    printf("Last name of desired person\n");
      scanf("%s",client_last_named);
    temporal1 = FindToAdd(List,client_named,client_last_named);
    ItemCount(temporal1);
    break;
    default:
            printf( "Invalid choice.\n\n" );
            break;
        }

        printf( "? " );
      scanf( "%d", &choice );
    }
}

To delete the inner list just iterate through the list and delete each node one at a time, something like this: It assumes head->item_data pointer is NOT NULL, so you should probably test for that before executing any of the code in this function.

void DeleItemList(struct client* head)
{
   struct item* save;
   struct item* it = head->item_data;
   while(it->next)
   {
      save = it;
      it = it->next;
      free(save);
   }
   head->item_data = NULL;
 }

So basicaly to delete whole list it would be like this? Seems to be working for me not sure if I'm doing it right thought. Could you aswell explain how would I edit node?

void DeleteWholeList(struct client** head)
{
   struct client *temp;
   struct client *temporal = (*head);
   struct item* save;
   struct item* it = (*head)->item_data;
   while(temporal->next)
   {
   while(it->next)
   {
      save = it;
      it = it->next;
      free(save);
   }
   temporal->item_data = NULL;
   temp = temporal;
   temporal = temporal->next;
   free(temp);
   }
     (*head) = NULL;
 }

That looks right, if the intent is to delete the entire client list.

To do edits you don't need to delete anything. Just search for the client you want then change the value of the client's properties. Of course you will have to get the new values from somewhere, most likely just prompt for them and get keyboard input, very much like when the program got the original client values.

So to change for example name and last name of client I would have to use function find and pass node pointer to function where I changedata, malloc new data and input it here? Will it overwrite or any other action is neccessary to do so?

You shouldn't have to allocate any new memory -- it's alredy there, just overwrite the fields of the pointer that FindToAdd() returns. I'd also change the name of that function to just Find() so that it better describes its purpose. Once found, just copy the new information into the client structure.

Okay writting code for edit was pretty simple got it done all thats left is how to delete single node from inner list. I understand that if I want to delete first element of inner list I should set it's first element to next one(is that right?) but when I want to delete something in middle of list I have to delete node and connect 2 other nodes , I seen few examples of how to delete nodes but I still dont understand how it should be done for node in middle

It's pretty simple, exactly like deleting the client node from the linked list. The item list is just like the client list.

If current is the node to delete, then

set prev-next = current->next
delete current

So I wrote code to delete item it seems to be working could you check if everything is right?

void DeleteItem(struct client *head,char name[30])
        {
            struct item *current = head->item_data,*prev = NULL;
            if(head==NULL)
                printf("List empty...");
            while(current!=NULL)
            {
                if(strcmp(current->item_name,name) == 0)
                {
                    if(prev == NULL)
                    {
                        head->item_data = current->next;
                    }
                    else
                    {
                        prev->next=current->next;
                    }
                    free(current);
                }
                    prev = current;
                    current = current->next;

        }
        printf("\nElement deleted \n");
            return NULL;
        }

line 18: since the node is now deleted you might as well stop looking, unless there could be more nodes with the same name.

lines 20 and 21 won't work because current has already been deleted on line 18.

current = prev->next;

So basicaly all I should change is this current = prev->next and unless I want to delete all items with given name break loop?I guess that's all I needed got some time left till deadline so could aswell try to add files to my project and learn about them a bit. Could you suggest which one would be easier to use with lists? Well overall that's all probably will need some more help with files, thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.