无法在C中打印链表元素

时间:2015-02-28 15:18:46

标签: c function linked-list

我正在尝试为链接列表程序创建一个打印功能。

到目前为止,这是我的代码,我设法做的唯一打印是在readfile函数内。我不知道如何使用函数来打印链表元素。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct record { // to contain several data types
  char process;
  char state;
  int  NUT;
  int  AT;
  int  Prio;
  struct reord *next;
};

typedef struct record node;
void readfile(node *newlist, node *ncurr, node *next, int quanta, int readincrement, int newincrement, int procnumb, char *fn);
node *crtnode();

main() {
  int menu = 0, quanta, readincrement, newincrement, procnumb;
  char fn[100];
  node *newlist = NULL;
  node *ncurr = NULL;
  node *next =NULL;
  char process, state;
  int NUT, AT, Prio;

  do {
    printf("Selfish RR Algorathim\n");
    printf("Enter 1 for existing file\n");
    printf("Enter 2 for manual input\n");
    printf("Enter 3 for random\n");
    printf("Enter 4 for exit\n");
    scanf("%d", &menu);

    switch (menu) {
      case 1:
        printf("insert file name");
        scanf("%s", &fn);
        readfile(newlist, ncurr, next, quanta, readincrement, newincrement, procnumb, fn);
        break;
      case 2:
        printf("boo");
        break;
      case 3:
        printf("haha");
        break;
      default:
        printf("Wrong choice again");
        break;
    }
  } while (menu != 4);
}

void readfile(node *newlist, node *ncurr, node *next, int quanta, int readincrement, int newincrement, int procnumb, char *fn) {
  FILE *myFile;
  int t;
  int j, i;

  newlist = NULL;
  myFile = fopen(fn, "r");

  // reading the values that will be final through out this process
  fscanf(myFile, "%d", &quanta);
  fscanf(myFile, "%d", &newincrement);
  fscanf(myFile, "%d", &readincrement);
  fscanf(myFile, "%d", &procnumb);
  printf("quanta = %d newinc = %d Readyinc = %d procnumb = %d \n", quanta, newincrement, readincrement, procnumb);

  newlist = crtnode(); // creating memory locations for the new list
  ncurr = newlist;

  for (i = 0; i < procnumb; i++) {
    ncurr->process = 'A' + i;
    ncurr->state = '-';
    fscanf(myFile, "%d", &ncurr->NUT);
    fscanf(myFile, "%d", &ncurr->AT);
    ncurr->Prio = '\0';

    if (i <= procnumb - 1) {
      ncurr->next = crtnode();
      ncurr = ncurr->next;
    }
  }
  ncurr = newlist;
  for (i = 0; i < procnumb; i++) {
    printf("\nnode = %p proc = %c state = %c NUT = %d AT = %d priority = %d nextnode = %p\n", ncurr, ncurr->process, ncurr->state, ncurr->NUT, ncurr->AT, ncurr->Prio, ncurr->next);
    ncurr = ncurr->next;
  }
  fclose(myFile);
}

node *crtnode() {
  return((node *)malloc(sizeof(node)));
}

0 个答案:

没有答案