链表输出
我正在做一项学校作业,几个月前才开始 C 编程。在这个作业中,我要输出“文件”占用的空间。
初始输出是正确的,但是,当我尝试打印表格时,输出变得不正确。
代码:
#define TOTAL_DISK_BLOCKS 32
#define TOTAL_DISK_INODES 8
int blockStatus[TOTAL_DISK_BLOCKS]; // free = 0
int blockList[TOTAL_DISK_BLOCKS - TOTAL_DISK_INODES]; // list of blocks of a file
struct file_table
{
char fileName[20];
int fileSize;
struct block *sb; // start block
};
struct file_table fileTable[TOTAL_DISK_BLOCKS - TOTAL_DISK_INODES];
struct block
{
int blockNumber;
struct block *next;
} * head;
int AllocateBlocks(int Size)
{
int i = 0, count = 0, inList = 0, nextBlock = 0, j = 0, dcount = 0, fails = 0;
int allocStartBlock = TOTAL_DISK_INODES;
int allocEndBlock = TOTAL_DISK_BLOCKS - 1;
// check whether sufficient free blocks are available
// some codes here
{
for (j = 0; j < Size; j++)
{
nextBlock = (rand() % (allocEndBlock - allocStartBlock + 1)) + allocStartBlock;
if (blockStatus[nextBlock] == 0)
{
// printf("\ncount:%d\n", count);
blockList[j] = nextBlock;
count += 1;
}
else
{
count = 0;
fails += 1;
// printf("\nfails:%d, block %d taken\n", fails, nextBlock);
break;
}
}
}
// remaining blocks left
dcount = 0;
for (i = 0; i < (TOTAL_DISK_BLOCKS - TOTAL_DISK_INODES); i++)
if (blockStatus[i] == 0)
dcount++;
printf("Remaining blocks: %d\n", dcount);
if (count == Size)
{
for (j = 0; j < Size; j++)
{
blockStatus[blockList[j]] = 1;
}
return 0; // success
}
else
return 1; // not successful
}
void printBlockNumbers(struct block *temp)
{
// Return if list is empty
if (head == NULL)
{
printf("List is empty.");
return;
}
temp = head;
while (temp != NULL)
{
printf("-%d", temp->blockNumber); // Print data of current node
temp = temp->next; // Move to next node
}
}
void main()
{
int i = 0, j = 0, numFiles = 0, nextBlock = 0, ret = 1;
char s[20];
struct block *temp, *newBlock;
//some print codes here...
scanf("%d", &numFiles);
for (i = 0; i < numFiles; i++)
{
printf("\nEnter the name of file #%d: ", i + 1);
scanf("%s", fileTable[i].fileName);
printf("Enter the size (kB) of file #%d: ", i + 1);
scanf("%d", &fileTable[i].fileSize);
ret = AllocateBlocks(fileTable[i].fileSize);
if (ret == 0)
{
head = (struct block *)malloc(sizeof(struct block));
head->blockNumber = blockList[0];
head->next = NULL;
temp = head;
for (j = 1; j < fileTable[i].fileSize; j++)
{
newBlock = (struct block *)malloc(sizeof(struct block));
newBlock->blockNumber = blockList[j]; // Link data field of newNode
newBlock->next = NULL; // Make sure new node points to NULL
temp->next = newBlock; // Link previous node with newNode
temp = temp->next; // Make current node as previous node
}
printf("Blocks occupied");
printBlockNumbers(fileTable[i].sb);
printf("\nFile allocation success\n");
}
else
{
printf("\nFile allocation failed\n");
}
}
// Seed the pseudo-random number generator used by rand() with the value seed
srand(1234);
printf("\nFile Allocation Table\n");
printf("%s%20s%40s\n", "FILE_NAME", "FILE_SIZE", "BLOCKS_OCCUPIED");
for (i = 0; i < numFiles; i++)
{
printf("%s%20d", fileTable[i].fileName, fileTable[i].fileSize);
printf("\t\t\t\t");
printBlockNumbers(fileTable[i].sb);
printf("\n");
}
printf("File allocation completed. Exiting.\n");
}
下面是第一个输出。
Blocks occupied-15-30-17
Blocks occupied-21-10-27-10
Blocks occupied-20-26-12-24-19
这是表输出。
File Allocation Table
FILE_NAME FILE_SIZE BLOCKS_OCCUPIED
3 3 -20-26-12-24-19
4 4 -20-26-12-24-19
5 5 -20-26-12-24-19
File allocation completed. Exiting.
I'm doing a school assignment and I've only started on C programming a few months back. In this assignment, I'm to output the space occupied by a 'file'.
The initial output is correct, however, when i try to print out a table, the output becomes incorrect.
Codes:
#define TOTAL_DISK_BLOCKS 32
#define TOTAL_DISK_INODES 8
int blockStatus[TOTAL_DISK_BLOCKS]; // free = 0
int blockList[TOTAL_DISK_BLOCKS - TOTAL_DISK_INODES]; // list of blocks of a file
struct file_table
{
char fileName[20];
int fileSize;
struct block *sb; // start block
};
struct file_table fileTable[TOTAL_DISK_BLOCKS - TOTAL_DISK_INODES];
struct block
{
int blockNumber;
struct block *next;
} * head;
int AllocateBlocks(int Size)
{
int i = 0, count = 0, inList = 0, nextBlock = 0, j = 0, dcount = 0, fails = 0;
int allocStartBlock = TOTAL_DISK_INODES;
int allocEndBlock = TOTAL_DISK_BLOCKS - 1;
// check whether sufficient free blocks are available
// some codes here
{
for (j = 0; j < Size; j++)
{
nextBlock = (rand() % (allocEndBlock - allocStartBlock + 1)) + allocStartBlock;
if (blockStatus[nextBlock] == 0)
{
// printf("\ncount:%d\n", count);
blockList[j] = nextBlock;
count += 1;
}
else
{
count = 0;
fails += 1;
// printf("\nfails:%d, block %d taken\n", fails, nextBlock);
break;
}
}
}
// remaining blocks left
dcount = 0;
for (i = 0; i < (TOTAL_DISK_BLOCKS - TOTAL_DISK_INODES); i++)
if (blockStatus[i] == 0)
dcount++;
printf("Remaining blocks: %d\n", dcount);
if (count == Size)
{
for (j = 0; j < Size; j++)
{
blockStatus[blockList[j]] = 1;
}
return 0; // success
}
else
return 1; // not successful
}
void printBlockNumbers(struct block *temp)
{
// Return if list is empty
if (head == NULL)
{
printf("List is empty.");
return;
}
temp = head;
while (temp != NULL)
{
printf("-%d", temp->blockNumber); // Print data of current node
temp = temp->next; // Move to next node
}
}
void main()
{
int i = 0, j = 0, numFiles = 0, nextBlock = 0, ret = 1;
char s[20];
struct block *temp, *newBlock;
//some print codes here...
scanf("%d", &numFiles);
for (i = 0; i < numFiles; i++)
{
printf("\nEnter the name of file #%d: ", i + 1);
scanf("%s", fileTable[i].fileName);
printf("Enter the size (kB) of file #%d: ", i + 1);
scanf("%d", &fileTable[i].fileSize);
ret = AllocateBlocks(fileTable[i].fileSize);
if (ret == 0)
{
head = (struct block *)malloc(sizeof(struct block));
head->blockNumber = blockList[0];
head->next = NULL;
temp = head;
for (j = 1; j < fileTable[i].fileSize; j++)
{
newBlock = (struct block *)malloc(sizeof(struct block));
newBlock->blockNumber = blockList[j]; // Link data field of newNode
newBlock->next = NULL; // Make sure new node points to NULL
temp->next = newBlock; // Link previous node with newNode
temp = temp->next; // Make current node as previous node
}
printf("Blocks occupied");
printBlockNumbers(fileTable[i].sb);
printf("\nFile allocation success\n");
}
else
{
printf("\nFile allocation failed\n");
}
}
// Seed the pseudo-random number generator used by rand() with the value seed
srand(1234);
printf("\nFile Allocation Table\n");
printf("%s%20s%40s\n", "FILE_NAME", "FILE_SIZE", "BLOCKS_OCCUPIED");
for (i = 0; i < numFiles; i++)
{
printf("%s%20d", fileTable[i].fileName, fileTable[i].fileSize);
printf("\t\t\t\t");
printBlockNumbers(fileTable[i].sb);
printf("\n");
}
printf("File allocation completed. Exiting.\n");
}
Below is the first output.
Blocks occupied-15-30-17
Blocks occupied-21-10-27-10
Blocks occupied-20-26-12-24-19
And this is the table output.
File Allocation Table
FILE_NAME FILE_SIZE BLOCKS_OCCUPIED
3 3 -20-26-12-24-19
4 4 -20-26-12-24-19
5 5 -20-26-12-24-19
File allocation completed. Exiting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个循环中,您每次都会打印新分配的列表,但在第二个循环中,您将打印最后分配的列表,因为您正在从列表打印,其中 tail 是全局变量
head
。在这里:
temp 的初始值并不重要。
此处:
您正在为
head
分配temp
值,因此temp->smth
指向与head-> 相同的地址;smth
因此,当您使用printBlockNumbers
打印head
时,它将打印出在此循环中分配给它的内容。您应该将
printBlockNumbers
更改为:并在每个循环中分配
head
的fileTable[i].sb
值,然后在结尾。供参考:
如果您想传递一些东西作为“引用”(即指针),以便能够更改此函数中的原始值,您可以这样做:
如果您想对指针执行相同的操作:
In first loop you're printing newly allocated list each time, but in second loop you're printing last allocated list as you're printing from list which tail is a global variable
head
.in here:
initial value of temp does not matter.
here:
you're assigning
temp
value ofhead
, sotemp->smth
points to the same address ashead->smth
so when you useprintBlockNumbers
to printhead
it will print out what was assigned to it in this loop.you should change
printBlockNumbers
to that:and also assign
fileTable[i].sb
value ofhead
in each loop and then free these lists at the end.FYI:
if you'd like to pass something to function as a "referece" (i.e. pointer) to be able to change original value in this function you do this like that:
and if you'd like to do the same to a pointer: