我正在尝试创建一个将结构条目添加到链表中的函数,但它似乎没有正常运行。使用这样的输入:
0 1 0
1 1 0
2 1 0
3 1 0
4 1 0
5 1 0
6 1 0
7 0 0
8 0 FF
9 0 FF
10 0 FF
11 0 FF
12 0 FF
13 0 FF
14 0 FF
15 0 FF
我得到了输出:
Printing linked list...
dev:0
dev:0
dev:0
dev:0
dev:0
dev:0
dev:35
dev:46
dev:48
dev:61
dev:165
dev:200
dev:240
dev:254
dev:255
我不确定这是什么原因。我期待看到我的设备(1-15)根据他们的数量排名。此模块将添加到我的模拟器中,以用作待处理设备中断的队列,因此需要根据其优先级排列我的设备。
此外,我不确定如何检索其余属性,例如in_out和time。那些不属于我的名单吗?
为什么我的输出只有一个16位整数列表?
这是我的整个模块。任何提示或指示都会很棒。谢谢!
#include <stdio.h>
#include <stdlib.h>
#define DEV_NUM 15
#define addrbuf 64
#define TRUE 1
#define FALSE 0
typedef struct entry{
unsigned char input_output;
unsigned char out_active;
unsigned short time;
unsigned char dev;
} entrystruct;
struct node{
int n;
struct node *prev;
struct node *next;
struct entry data;
}*head;
struct node* getnewnode(struct entry linked_dev){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = linked_dev;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}
void insert(struct entry linked_dev){
struct node* newnode = getnewnode(linked_dev);
struct node* temp = head;
if(head == NULL){
head = newnode;
return;
}
if(head->data.dev > linked_dev.dev){
head->prev = newnode;
newnode->next = head;
head = newnode;
return;
}
while (temp->next && temp->next->data.dev < linked_dev.dev) {
temp = temp->next;
}
newnode->next = temp->next;
if(temp->next){
temp->next->prev=newnode;
}
temp->next = newnode;
newnode->prev = temp;
}
void print(){ //Function purely for testing purposes
struct node* temp = head;
while (temp != NULL){
printf("dev:%d\n", temp->data.dev);
temp = temp->next;
}
}
int main (int argc, char* argv[]){
int dev = 0;
char filecontent, file_name[15];
FILE* dev_in;
entrystruct entry[DEV_NUM];
unsigned char* buffer;
unsigned char* token;
unsigned in_out, output_time;
buffer = malloc(addrbuf);
if (argc != 2){
printf("To operate on Linux enter:\n");
printf("./<linkedlist> <devicefilename.in>\n");
exit(1);
}
if ((dev_in = fopen(argv[1], "r")) == NULL){
printf("Device input file %s could not be opened\n", argv[1]);
exit(1);
}
for (dev = 0; dev < DEV_NUM; dev++){
fgets(buffer, addrbuf, dev_in);
token = strtok(buffer, " \t\n");
token = strtok(NULL, " \t\n");
in_out = strtol(token, NULL, 16);
token = strtok(NULL, " \t\n");
output_time = strtol(token, NULL, 16);
entry[dev].input_output = in_out;
entry[dev].time = output_time;
entry[dev].out_active = FALSE;
insert(entry[dev]);
}
printf("Printing linked list...\n");
print();
fclose(dev_in);
return 0;
}
答案 0 :(得分:1)
这是固定代码:
#include <stdio.h>
#include <stdlib.h>
#define DEV_NUM 16
#define addrbuf 64
#define TRUE 1
#define FALSE 0
typedef struct entry{
unsigned char input_output;
unsigned short out_active;
unsigned short occur_time;
unsigned short dev;
} entrystruct;
struct node{
int n;
struct node *prev;
struct node *next;
struct entry data;
}*head;
struct node* getnewnode(struct entry linked_dev){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = linked_dev;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}
void insert(struct entry linked_dev){
struct node* newnode = getnewnode(linked_dev);
struct node* temp = head;
if(head == NULL){
head = newnode;
return;
}
if(head->data.dev > linked_dev.dev){
head->prev = newnode;
newnode->next = head;
head = newnode;
return;
}
while ((temp->next) && (temp->next->data.dev < linked_dev.dev)) {
temp = temp->next;
}
newnode->next = temp->next;
if(temp->next){
temp->next->prev = newnode;
}
temp->next = newnode;
newnode->prev = temp;
}
void print(){ //Function purely for testing purposes
struct node* temp = head;
while (temp != NULL){
printf("dev:%2d input_output: %2d out_active: %2d occur_time: %2x \n",
temp->data.dev, temp->data.input_output, temp->data.out_active, temp->data.occur_time);
temp = temp->next;
}
}
/*Testing mainline*/
int main (int argc, char* argv[]){
FILE* dev_in;
int i = 0;
entrystruct entry[DEV_NUM];
unsigned char* buffer;
unsigned char* token;
unsigned short dev;
unsigned in_out, output_time;
buffer = malloc(addrbuf);
if (argc != 2){
printf("To operate on Linux enter:\n");
printf("./<linkedlist> <devicefilename.in>\n");
exit(1);
}
if ((dev_in = fopen(argv[1], "r")) == NULL){
printf("Device input file %s could not be opened\n", argv[1]);
exit(1);
}
for (i = 0; i < DEV_NUM; i++){
fgets(buffer, addrbuf, dev_in);
token = strtok(buffer, " \t\n");
dev = atoi(token);
token = strtok(NULL, " \t\n");
in_out = strtol(token, NULL, 16);
token = strtok(NULL, " \t\n");
output_time = strtol(token, NULL, 16);
entry[dev].dev = dev;
entry[dev].input_output = in_out;
entry[dev].occur_time = output_time;
entry[dev].out_active = FALSE;
insert(entry[dev]);
}
printf("Printing linked list...\n");
print();
fclose(dev_in);
return 0;
}
我现在将实现一个pop和clear函数。如果有人有任何指针会很棒。无论如何,感谢你指出我的错误。