if (tmp == NULL)
{
return 0;
}
int count = 0;
while (tmp)
{
tmp = tmp->next;
count++;
}
return count;
}
Node* search(List head, const int id)
{
code
if (head->next == NULL)
{
return NULL;
}
Node *tmp = head -> next;
while (tmp)
{
if (tmp->d.id == id)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
errors:
cc fileallocation.c -std=c99
In file included from fileallocation.c:8:0:
LinkedList.h:1:1: error: unknown type name ‘Block’
typedef Block Data;
^
LinkedList.h:11:24: error: unknown type name ‘Block’
extern void init_block(Block *const);
^
LinkedList.h: In function ‘createEmptyList’:
LinkedList.h:17:5: warning: implicit declaration of function ‘init_block’ [-Wimplicit-function-declaration]
init_block(&(head -> d));
^
LinkedList.h: In function ‘display’:
LinkedList.h:119:53: error: request for member ‘id’ in something not a structure or union
printf(" BID : %-2d\tStatus: %d\n\n", tmp->d.id, tmp->d.status);
^
LinkedList.h:119:64: error: request for member ‘status’ in something not a structure or union
printf(" BID : %-2d\tStatus: %d\n\n", tmp->d.id, tmp->d.status);
^
LinkedList.h: In function ‘search’:
LinkedList.h:154:19: error: request for member ‘id’ in something not a structure or union
if (tmp->d.id == id)
^
fileallocation.c: At top level:
fileallocation.c:34:6: warning: conflicting types for ‘init_block’ [enabled by default]
void init_block(Block *const);
^
In file included from fileallocation.c:8:0:
LinkedList.h:17:5: note: previous implicit declaration of ‘init_block’ was here
init_block(&(head -> d));
^
fileallocation.c: In function ‘contiguous’:
fileallocation.c:81:9: error: incompatible type for argument 2 of ‘insertLast’
insertLast(list, b);
^
In file included from fileallocation.c:8:0:
LinkedList.h:24:6: note: expected ‘Data’ but argument is of type ‘Block’
void insertLast(List head, const Data d)
^
fileallocation.c:99:13: warning: implicit declaration of function ‘random’ [-Wimplicit-function-declaration]
id = random() % num_blk;
^
fileallocation.c:101:23: error: request for member ‘status’ in something not a structure or union
if (ptr->d.status != FREE)
^
fileallocation.c:109:27: error: request for member ‘id’ in something not a structure or union
start = ptr->d.id;
^
fileallocation.c:115:27: error: request for member ‘status’ in something not a structure or union
if (tmp->d.status == FREE)
^
fileallocation.c:138:27: error: request for member ‘status’ in something not a structure or union
tmp->d.status = 1;
^
fileallocation.c: In function ‘linked’:
fileallocation.c:181:9: error: incompatible type for argument 2 of ‘insertLast’
insertLast(list, b);
^
In file included from fileallocation.c:8:0:
LinkedList.h:24:6: note: expected ‘Data’ but argument is of type ‘Block’
void insertLast(List head, const Data d)
^
fileallocation.c:205:23: error: request for member ‘status’ in something not a structure or union
if (ptr->d.status != FREE)
^
fileallocation.c:210:21: error: request for member ‘status’ in something not a structure or union
ptr -> d.status = 1;
^
fileallocation.c:222:25: error: request for member ‘status’ in something not a structure or union
ptr -> d.status = FREE;
^
fileallocation.c:237:24: error: request for member ‘next_file_blk’ in something not a structure or union
left->d.next_file_blk = &(right->d);
^
fileallocation.c:238:24: error: request for member ‘status’ in something not a structure or union
left->d.status = 1;
^
fileallocation.c:240:21: error: request for member ‘next_file_blk’ in something not a structure or union
right->d.next_file_blk = NULL;
^
fileallocation.c:265:24: warning: initialization from incompatible pointer type [enabled by default]
Block *b = &(ptr->d);
^
fileallocation.c: In function ‘indexed’:
fileallocation.c:288:9: error: incompatible type for argument 2 of ‘insertLast’
insertLast(list, b);
^
In file included from fileallocation.c:8:0:
LinkedList.h:24:6: note: expected ‘Data’ but argument is of type ‘Block’
void insertLast(List head, const Data d)
^
fileallocation.c:314:23: error: request for member ‘status’ in something not a structure or union
if (ptr->d.status == FREE)
^
[os-2200050049@os 26Mar24]nanofileallocation.c[os−2200050049@os26Mar24] [os-2200050049@os 26Mar24]$ cc fileallocation.c -std=c99
In file included from fileallocation.c:8:0:
LinkedList.h:1:1: error: unknown type name ‘Block’
typedef Block Data;
^
LinkedList.h:11:24: error: unknown type name ‘Block’
extern void init_block(Block *const);
^
LinkedList.h: In function ‘createEmptyList’:
LinkedList.h:17:5: warning: implicit declaration of function ‘init_block’ [-Wimplicit-function-declaration]
init_block(&(head -> d));
^
LinkedList.h: In function ‘display’:
LinkedList.h:119:53: error: request for member ‘id’ in something not a structure or union
printf(" BID : %-2d\tStatus: %d\n\n", tmp->d.id, tmp->d.status);
^
LinkedList.h:119:64: error: request for member ‘status’ in something not a structure or union
printf(" BID : %-2d\tStatus: %d\n\n", tmp->d.id, tmp->d.status);
^
LinkedList.h: In function ‘search’:
LinkedList.h:154:19: error: request for member ‘id’ in something not a structure or union
if (tmp->d.id == id)
^
fileallocation.c: At top level:
fileallocation.c:34:6: warning: conflicting types for ‘init_block’ [enabled by default]
void init_block(Block *const);
^
In file included from fileallocation.c:8:0:
LinkedList.h:17:5: note: previous implicit declaration of ‘init_block’ was here
init_block(&(head -> d));
^
fileallocation.c: In function ‘contiguous’:
fileallocation.c:81:9: error: incompatible type for argument 2 of ‘insertLast’
insertLast(list, b);
^
In file included from fileallocation.c:8:0:
LinkedList.h:24:6: note: expected ‘Data’ but argument is of type ‘Block’
void insertLast(List head, const Data d)
^
fileallocation.c:99:13: warning: implicit declaration of function ‘random’ [-Wimplicit-function-declaration]
id = random() % num_blk;
^
fileallocation.c:101:23: error: request for member ‘status’ in something not a structure or union
if (ptr->d.status != FREE)
^
fileallocation.c:109:27: error: request for member ‘id’ in something not a structure or union
start = ptr->d.id;
^
fileallocation.c:115:27: error: request for member ‘status’ in something not a structure or union
if (tmp->d.status == FREE)
^
fileallocation.c:138:27: error: request for member ‘status’ in something not a structure or union
tmp->d.status = 1;
^
fileallocation.c: In function ‘linked’:
fileallocation.c:181:9: error: incompatible type for argument 2 of ‘insertLast’
insertLast(list, b);
^
In file included from fileallocation.c:8:0:
LinkedList.h:24:6: note: expected ‘Data’ but argument is of type ‘Block’
void insertLast(List head, const Data d)
^
fileallocation.c:205:23: error: request for member ‘status’ in something not a structure or union
if (ptr->d.status != FREE)
^
fileallocation.c:210:21: error: request for member ‘status’ in something not a structure or union
ptr -> d.status = 1;
^
fileallocation.c:222:25: error: request for member ‘status’ in something not a structure or union
ptr -> d.status = FREE;
^
fileallocation.c:237:24: error: request for member ‘next_file_blk’ in something not a structure or union
left->d.next_file_blk = &(right->d);
^
fileallocation.c:238:24: error: request for member ‘status’ in something not a structure or union
left->d.status = 1;
^
fileallocation.c:240:21: error: request for member ‘next_file_blk’ in something not a structure or union
right->d.next_file_blk = NULL;
^
fileallocation.c:265:24: warning: initialization from incompatible pointer type [enabled by default]
Block *b = &(ptr->d);
^
fileallocation.c: In function ‘indexed’:
fileallocation.c:288:9: error: incompatible type for argument 2 of ‘insertLast’
insertLast(list, b);
^
In file included from fileallocation.c:8:0:
LinkedList.h:24:6: note: expected ‘Data’ but argument is of type ‘Block’
void insertLast(List head, const Data d)
^
fileallocation.c:314:23: error: request for member ‘status’ in something not a structure or union
if (ptr->d.status == FREE)
^
fixthe code
The code you've provided contains several issues, primarily stemming from attempting to use a Block type in LinkedList.h before it's been defined. Additionally, there are issues with misuse of the type Data in fileallocation.c. Let's address these problems step by step to correct the code and get it compiling and running correctly.
Firstly, the primary issue in your code comes from the order and way types are declared and used across fileallocation.c and LinkedList.h.
Fix 1: Ensure Block is defined before LinkedList.h is included.
Since LinkedList.h uses Block as a type for Data, you need to define Block before including LinkedList.h. Thus, place the Block struct definition above the #include "LinkedList.h" directive in fileallocation.c.
Fix 2: Modify LinkedList.h to accept generic data by using void* or consider integrating the structure definition into fileallocation.c.
For simplicity, here's how you can adjust your LinkedList.h to deal with generic data pointers which will allow it to be more flexible. This avoids the immediate error but might require adjustments in how you handle data within the list.
LinkedList.h:
c
typedef struct Node {
void* data; // Changed from Data d;
struct Node* next;
} Node;
typedef Node* List;
... // Functions declarations remain largely the same but will require adjustments due to the change from Data to void*.
Fix 3: Adapt code in fileallocation.c to work with the changes made in LinkedList.h.
When you are using void* for data in LinkedList.h, you need to adjust how data is inserted into the list and accessed thereafter in fileallocation.c. Any operation on Node's data will now require casting to the appropriate type before usage.
For example, the insertLast function would look something like this:
c
void insertLast(List head, void* d)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = d; // Note: d is now a void pointer
newNode->next = NULL;
Node* tmp = head;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = newNode;
}
And when accessing data from a Node, you'll have to cast it back to the appropriate type, like so:
c
Node* node = search(list, id);
Block* blockData = (Block*)node->data; // Assuming you're searching for a Block type
This approach will require several changes throughout fileallocation.c to cast data to and from void* appropriately.
Given the extent of the changes needed, these fixes offer a conceptual path forward rather than a direct, drop-in solution. The key is to fix the forward declaration issue by properly structuring the code so that all types are defined before they are used, and adapting LinkedList.h to handle generic data through void* pointers, which then requires casting data types when they are used. This approach introduces some complexity but adds flexibility to your linked list implementation.
Given the issues encountered and the request for a comprehensive solution, I'll provide a simplified and corrected version of your code integrating the main ideas but focusing on getting a compilable and logically consistent structure. This solution will address the primary issues in a more straightforward manner than introducing void* pointers for generic list handling due to the complexity that generic data handling would add. Instead, we'll ensure the Block structure is defined before being used by LinkedList.h and simplify the usage.
Step 1: Define Structures Properly
fileallocation.c (partial):
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX 100
#define FREE 0
typedef struct Block
{
int id;
unsigned status : 1;
struct Block *next_file_blk;
} Block;
// Ensure LinkedList.h is included after the Block definition.
#include "LinkedList.h"
// Further implementations...
Step 2: Adjust LinkedList Implementation
LinkedList.h (modified for direct integration with Block structure):
c
#include <stdlib.h>
#include <stdio.h>
typedef struct Node
{
Block d; // Directly use Block as this list specifically handles Blocks.
struct Node *next;
} Node;
typedef Node *List;
List createEmptyList();
void insertLast(List head, const Block d);
Node* search(List head, const int id);
// And more function declarations as needed...
Step 3: Simplify the fileallocation.c File
fileallocation.c (continued):
Considering the length and complexity of your original code, instead of pasting the entirety of it here, I'll note key adjustments below that should be made following the newly defined structures and simplified linked list:
Ensure the init_block function is defined before including LinkedList.h or directly within fileallocation.c if it's only used there.
Use Block directly in your list operations. Correct the insertLast usage and any other list operations to properly handle Block without type mismatches.
Adapt the file operations (contiguous, linked, indexed) to correctly work with the adjusted List and Node structure.
Consider removing extern void init_block(Block *const); from "LinkedList.h" if it's unnecessarily complicating the code structure, as initialization functionalities are typically handled within the main file or their specific module.
Simplified File Allocation Operations
The detailed implementation of functions like contiguous, linked, and indexed in fileallocation.c would still follow the logic you've defined but will now work correctly with the list. Since redoing those function implementations in full would be quite extensive and isn't the focus of the fix, make sure that when you access or modify Block structures within these functions, you are directly working with Block instances or pointers and not through a type mismatch.
Conclusion
This provided fix focuses on the structural issues in your code related to type definitions and the order of includes. The operations within the file allocation functions would follow the same logic but adapted to the corrected structure. Due to space and focus, detailing every corrected line of your rather extensive original code isn't practical here. However, this guide gives a solid foundation on how to correctly organize your code and address the type-related issues you've encountered.