Thread: Declaring head and tail for doubly linked list

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    11

    Declaring head and tail for doubly linked list

    I'm new to C and creating a program that manages two different collection structures - an array structure and a doubly linked list. I'm creating functions that will manage these structures as well as the movie data they store.


    I'm a little stumped on how to define the linked list in my header file. I'm asked to create the ListType and NodeType data types, which define a doubly linked list, with both a head and a tail.


    The list must store the next id to be assigned to a movie added to the list and must not not use dummy nodes; every node must correspond to a movie structure. I think I have the general nodes down like the data, previous, and next nodes. But I'm a little confused as to what's supposed to go into the ListType vs NodeType structures. I'm not sure what the ListType structure would be used for other than storing the nextId integer. The NodeType structure is more obvious to me, as it probably declares each Node in the linked list.


    Am I supposed to declare data members for the head and tail directly in the NodeType/ListType structures or should they be declared somewhere else?


    In my task, it says my program will begin by declaring and initializing the collections that we will be using in this program. I am to declare two instances of a linked list structure, as defined in the defs.h file; each list will be associated with a person or fictional character etc. calvinMovies and hobbesMovies.


    I am to initialize the two linked lists as follows:


    (a) initialize each list’s head, tail, and initial next identifier value


    (b) add movies to both lists by calling the initAllMovies() function that is provided


    Since the initAllMovies() takes in two ListType parameters, I assumed I declared calvinMovies and hobbesMovies correctly. I'm just confused on to how I would initialize the head and tail. At first I created two NodeType data structures, but I wasn't sure if the head and tail should already be declared in one of the data structures, leading to the two other examples I wrote below.


    I'm just a little confused on how to get started and would appreciate some help or a push in the right direction. The functions for my linked list will also go in the main.c file, but before creating them I need to figure out how to set up the basics correctly.


    defs.h

    Code:
        typedef struct{
            int id;
            char *title;
            int year;
        } MovieType;
    
    
        typedef struct{
            int nextId; 
        } ListType;
    
    
        typedef struct Node {
          MovieType *data;
          struct Node *prev;
          struct Node *next;
        } NodeType; 
    
    
        typedef struct {
          int size;
          MovieType **movies;
        } ArrayType;
    
    
        enum SortType{
            BY_TITLE,
            BY_YEAR
        };
    main.c

    Code:
        #include "defs.h"
    
    
        int main(){
            
            NodeType *calvinMovies;
            NodeType *hobbesMovies; 
            
        /*
            NodeType *head = NULL;
            NodeType* tail = NULL;
            
            NodeType.head = NULL;
            NodeType.tail = NULL;
            
            ListType.head = NULL;
            ListType.tail = NULL;
        */
            initAllMovies(calvinMovies, hobbesMovies);
            return 0;
            
        }
    init.c

    Code:
        #include "defs.h"
        oid initAllMovies(ListType *list1, ListType *list2)
        {
          MovieType *m;
    
    
          initMovie("E.T. The Extra-Terrestrial", 1982, &m);
          addMovieToList(BY_TITLE, list1, m);
          initMovie("Metropolis", 1927, &m);
          addMovieToList(BY_TITLE, list1, m);
        }
    
    

    I uploaded the complete assignment through images. I'm not expecting any help past the red line, but I included the rest of the assignment for guidance if that helps. I'm just confused on how to declare the main components of the linked list. I'm not sure what ListType should be used for.
    Attached Images Attached Images Declaring head and tail for doubly linked list-screenshot-2-png Declaring head and tail for doubly linked list-screenshot-4-png Declaring head and tail for doubly linked list-screenshot-3-png 
    Last edited by eagerissac; 11-08-2020 at 01:29 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,097
    The head and tail go in ListType.
    Code:
        typedef struct Node {
          MovieType *data;
          struct Node *prev;
          struct Node *next;
        } NodeType; 
     
        typedef struct{
            Node *head;
            Node *tail;
            int nextId;
        } ListType;
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tail pointer crases program in doubly linked list
    By wesdom in forum C++ Programming
    Replies: 8
    Last Post: 11-12-2014, 12:54 PM
  2. doubly linked list tail pointer in struct
    By bazzano in forum C Programming
    Replies: 15
    Last Post: 06-11-2007, 12:31 PM
  3. loosing tail in doubly linked list
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 05-06-2007, 11:46 PM
  4. Another List Q - Seg fault on head-tail list
    By JimpsEd in forum C Programming
    Replies: 11
    Last Post: 05-10-2006, 12:53 AM
  5. Cant head insert on doubly linked list
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 09-18-2005, 11:31 AM

Tags for this Thread