将结构体插入链表将不会显示为正确的节点



我试图创建一个将结构项添加到链表中的函数,但它似乎不能正常运行。使用如下输入:

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:%dn", 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 openedn", argv[1]);
exit(1);
}
for (dev = 0; dev < DEV_NUM; dev++){
  fgets(buffer, addrbuf, dev_in);
  token = strtok(buffer, " tn");
  token = strtok(NULL, " tn");
  in_out = strtol(token, NULL, 16);
  token = strtok(NULL, " tn");
  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;
}

下面是固定的代码:

#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 openedn", argv[1]);
    exit(1);
  }
    for (i = 0; i < DEV_NUM; i++){
      fgets(buffer, addrbuf, dev_in);
      token = strtok(buffer, " tn");
        dev = atoi(token);
      token = strtok(NULL, " tn");
      in_out = strtol(token, NULL, 16);
      token = strtok(NULL, " tn");
      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;
}

我现在将实现一个弹出和清除功能。如果有人有什么建议,那就太好了。无论如何,谢谢你指出我的错误。

相关内容

  • 没有找到相关文章

最新更新