简体   繁体   中英

Doubly Linked Circular List problem solving

I am trying to understand the answer to this question.

We want to store a collection of 100 StudentMark objects in a list. A student mark object contains only a student id number, which takes a single word (4 bytes) and a mark, which takes another word.

We choose a Doubly Linked Circular List as our implementation of a list ADT. All pointers also take one word of memory. StudentMark objects are not stored directly in the linked list nodes but are allocated elsewhere in memory and only a pointer to each StudentMark object is stored in each linked list node.

How many words of memory are required to store all the linked list nodes and all the StudentMark objects?

Do not include the header node that points to the first node in the linked list in your count.

Doubly Linked Circular List :

在此处输入图像描述

I assumed the answer was 400

Reasoning:

  • 2 words of memory for every object as each object has a sutdentID & a single word which take 1 word of memory each, so, 2*100 = 200
  • 2 words of memory for each object, as each object has two pointers coming out from it so, 2*100 = 200
  • 200 + 200 = 400

But apparently the answer is 500

The keyphrase in the description is this one:

StudentMark objects are not stored directly in the linked list nodes but are allocated elsewhere in memory and only a pointer to each StudentMark object is stored in each linked list node.

The word used by that pointer (marked in bold in the above quote) was missing in your calculation.

A more detailed picture of the linked list structure would be like this one:

           ┌──────────────────────────────────────────────┐        
  ┌────────│─────────────────────────────────────┐        │
  │        ▼                                     ▼        │
  │ ┌─────────┐   ┌─────────┐   ┌─────────┐   ┌─────────┐ │
  └─── prev   │◄──── prev   │◄──── prev   │◄──── prev   │ │
    │  next ─────►│  next ─────►│  next ─────►│  next ────┘  
    │  data   │   │  data   │   │  data   │   │  data   │
    └───│─────┘   └───│─────┘   └───│─────┘   └───│─────┘
        │             │             │             │
        ▼             ▼             ▼             ▼
    ┌─────────┐   ┌─────────┐   ┌─────────┐   ┌─────────┐
    │  ID     │   │  ID     │   │  ID     │   │  ID     │
    |  mark   |   |  mark   |   |  mark   |   |  mark   |
    └─────────┘   └─────────┘   └─────────┘   └─────────┘

In each "column" in this diagram there are 5 words, so if the linked list has 100 entries, the total number of words is 500.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM