指针编写不正确

时间:2016-04-03 01:26:54

标签: c pointers linked-list

我这里有这个代码。我想知道为什么parseCommand不写入main函数中的“队列”。难道我做错了什么?我假设有3种不同类型的队列,parseCommand以“enqueue,1,Class-section,name”的格式返回一个命令,它应该存储在链表中,具体取决于它应该的队列号。是

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hw08-header.h"    /*  note header  */

typedef struct node{
    int classNumber;
    char *className;
    struct node *next;
} Queue;

void parseCommand(char *cmd, Queue **queue);

int main(int argc, char* argv[])
{
    int i = 0;
    char *value = "go";
    int key;
    Queue *queues = calloc(3, sizeof(Queue));

    (queues)->next = NULL;
    (queues + (sizeof(Queue) * 1))->next = NULL;
    (queues + (sizeof(Queue) * 2))->next = NULL;

    if(argc != 2)
    {
        printf("  usage:  program_name positive_integer\n");
        printf("example:  ./example-hw08  123\n");
        exit(1);
    }

    /**************  begin REQUIRED  **************/
    /*  put before logic.  DO NOT PUT IN A LOOP */
    key = atoi(argv[1]);
    initialize(key);
    /**************   end REQUIRED   **************/

    while(strcmp(value, "stop") != 0)
    {
        value = getString();
        parseCommand(value, &queues);
    }

    printf("%d\n", (queues + 1)->classNumber);
}

void parseCommand(char *cmd, Queue **queue) {
  char *verb, *class, *name, *className;
  int queueNumber, classNumber;
  Queue *tempQueue = *queue, *node, *p;

  verb = strtok(cmd, ",");

  if(strcmp(verb, "stop") != 0) {
    if(strcmp(verb, "enqueue") == 0) {
      queueNumber = atoi(strtok(NULL, ","));
      class = strtok(NULL, ",");
      name = strtok(NULL, ",");
      className = strtok(class, " ");
      classNumber = atoi(strtok(NULL, " "));

      printf("%s enters line %d to register for %s %d\n", name, queueNumber, className, classNumber);

      node = malloc(sizeof(queue));

      node->classNumber = classNumber;
      node->next = NULL;

      p = tempQueue;

      while(p->next != NULL) {
        p = p->next;
      }

      p->next = node;
      p = node;

      *queue = tempQueue;
    }
  }
}

0 个答案:

没有答案