0

So in the following code I am supposed to write a statement that declares a variable named default of type Item and initialize it with the following strings for its title, author, and year members to: "title", "author", and "1950".

#include <stdio.h>
#define SIZE 50
struct book {
    char title[SIZE], author[SIZE], year[5];
};
typedef struct book Item;
typedef struct node {
    Item item;
    struct node * next;
} Node;
typedef Node * List;
int main(void){
    Node Node1, Node2;
    List pNode = &Node2;
    return 0;
}

But I am quite new to linked lists and I can't seem to find a way to do this in one statement? Maybe I am misreading the question somehow? I feel like the answer to this is very simple but I just can't grasp even the concept of linked lists for some reason anyways.

Thanks for any help anyone!

1
  • You simply have to find a good tutorial or example and work through it carefully to understand proper list operation. You may like this Minimal Singly-Linked Non-Circular Implementation which read words into a linked list from stdin, prints the words contained in the list, and then properly frees all memory associated with the list and exits. Commented May 1, 2017 at 6:35

3 Answers 3

1

Your question has nothing to do with linked lists, and only to do with "aggregate initialization." Which you can do this way:

Item default_ = { "title", "author", "1950" };
Sign up to request clarification or add additional context in comments.

2 Comments

Except using default as a variable name instead of a label in a switch is not allowed — it is a keyword, so you can't actually write that, can you?
@JonathanLeffler: That's true of course--I'll tweak the variable name. That said, OP chose the variable name, not me! :)
0

Usually, linked list nodes are allocated dynamically. What you need is an ordered set of operations to manipulate your data while keeping it consistent. Most programs write themselves once you define your data and operations, since a program is nothing more then operations preformed on data.

  • So step one is to initialize a new list. The simplest solution is a constant initializer.

    #define LIST_INIT_VAL NULL
    
    List list = LIST_INIT_VAL; // used like this
    
  • Step two is to add a node, as I said, here we'd go the dynamic allocation route.

    bool list_push_front(List *p_list, Item const *p_item) { //stdbool.h
      Node *new = malloc(sizeof *new);
      if(!new)
        return false;
    
      new->item = *p_item;
      new->next = *p_list;
      *p_list = new;
      return true;
    }
    
    // ...
    
    list_push_front(&list, &(Item){"title", "author", "1950"}); // used like this
    

And so forth. Try to add the required list operations first, and then write your program in their terms. The next natural step is probably list_free.

Comments

0

You will need to initialize the list in reverse, and you have to use the initializer list.

Item c = { {"title", "author", "year"}, null };
Item b = { {"title", "author", "year"}, &c };
Item a = { {"title", "author", "year"}, &b };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.