C程序使用结构发生了什么?/无法正常工作

时间:2015-05-05 18:28:39

标签: c struct

我正在努力修复过去几周我一直在努力的一段代码。这应该做的几乎是生成结构的链接列表。目前它没有产生任何东西,但我仍在研究代码。我可以使用任何关于如何正确实现insert_ordered的建议。谢谢!

#include <stdlib.h> //for malloc and rand
#include <stdio.h>

struct PCB
{
    struct PCB *Next_PCB ;
    int PID ;
} ;

struct PCB *ptr, *tmp ;
void insert_ordered (struct PCB *Head, struct PCB *Add) ;
void print_list(struct PCB *Head) ;

int main()
{
    int num_structs, i;

    ptr = (struct PCB *) malloc (sizeof (struct PCB)) ;
    ptr->Next_PCB = NULL;
    ptr->PID = rand()%20;

    num_structs = 10 + (rand() % 10) ;
    for ( i = 0 ; i < num_structs ; i++)
    {tmp = (struct PCB *) malloc (sizeof(struct PCB)) ;
        tmp->PID = rand() % 20 ;
        tmp->Next_PCB = NULL ;

    insert_ordered(ptr, tmp);
    }
    print_list(ptr) ;
    return EXIT_SUCCESS;
    }



void insert_ordered (struct PCB *Head, struct PCB *Add)
{
    struct PCB* first;
    if ((Head == NULL) || ((Head)->PID >= Add->PID)){
            Add->Next_PCB= Head;
            Head = Add;
        }
    else{
            first = Head;
    }
    while ((first->Next_PCB != NULL) && (first->Next_PCB->PID < Add->PID))
    {
        first = first->Next_PCB;
    }
    Add->Next_PCB = first->Next_PCB;
    first->Next_PCB = Add;
}


void print_list(struct PCB *Head)
{
    tmp=(struct PCB *) malloc(sizeof(struct PCB));
    tmp=Head;

    while (tmp != NULL) 
    {

        printf("%d\n", tmp->PID);
        tmp=tmp->Next_PCB;

    }
    exit(EXIT_SUCCESS);
}

此代码现在将编译,但我目前没有从该文件输出。

2 个答案:

答案 0 :(得分:2)

简而言之,结构是(在非常简短的外行人的术语中):同名数据的集合。 More info。基本上,您可以在

下存储所有测试分数的值(例如)
struct test_scores {
    int test1;
    int test2;
    int test3;
    ...
}

您可以使用

main下设置其值
main() 
{
    struct test_scores Math;
    Math.test1 = 90;
    Math.test2 = 85;
    Math.test3 = 100;
}

(当然,周围有更多代码!)行*中的struct PCB *ptr, *tmp, *tcn;表示pointer(是的,我非常喜欢cprogramming.com!它&#39 ; s我在哪里学习c ++)

你似乎对这些东西有了处理(正在阅读我输入的评论时出现的评论)。我认为让你感到困惑的是,因为它困扰了我很长时间(并且不可否认,仍然存在)就是这句话:

struct PCB *Next_PCB;

这是linked list。链接列表是一个可以在c ++中使用的设置,它允许您在链接列表下创建多个结构(类似于如何在结构中设置多个变量,只有一个级别更高)。 *Next_PCB是指向链表中下一个结构的指针。因此,按照测试类比,您可以将所有课程放在一个链表中。您可以只创建一个结构struct Math { int test1; }; struct Physics { int test1; };,然后在struct Courses { int test1; struct Courses *next; }函数中创建Math结构,然后创建Physics,而不是为每个课程创建单独的结构,即main()。 struct来创建一个链表。 (N.B.查看来源以获得一个很好的例子)。

您似乎明白自己在做什么,或者只是对您的编辑感到幸运!对于所有“实际”的#39; c程序员,我知道我简化了一些东西!如果您仍然遇到困难,我愿意更详尽地解释结构和链表如何工作。

编辑:您评论说在我发布之后/之前不会编译,要查看代码,尝试提供帮助。 编辑2.0:有人打败了我。好吧

答案 1 :(得分:2)

我继续并对上面的代码进行了评论,并提出了一些缺陷和大量的内存泄漏。

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

struct PCB 
{
  struct PCB *Next_PCB;
  int PID;
};

struct PCB *ptr, *tmp, *tcn;

void insert_ordered(struct PCB *Head, struct PCB *Add);
void print_list(struct PCB *);

int main() 
{
  int num_structs, i;

  // Allocate memory for the PCB structure and setup it's pointers and data
  ptr=(struct PCB *) malloc(sizeof (struct PCB));
  ptr->Next_PCB=NULL;
  ptr->PID=rand() % 20;

  // Let's create between 10 and 19 structures
  num_structs=10 + (rand() % 10);
  for (i=0; i<num_structs; i++)
  {
    tmp = (struct PCB *) malloc(sizeof(struct PCB));
    tmp->PID=rand() % 20;
    tmp->Next_PCB = NULL;

    // Add these structures in order of their PID
    insert_ordered(ptr, tmp); 
  }

  // Print the result to screen
  print_list(ptr);

  return EXIT_SUCCESS;
}

void insert_ordered(struct PCB *Head, struct PCB *Add)
{
  // Create a new structure, even though Add and Head already exist?

  tcn = (struct PCB*) malloc(sizeof(struct PCB));

  // If there is no Head, or if Head's PID is smaller than head's
  // value, make Add the new head.
  if (Head == NULL || Head->PID >= Add->PID)
  {
    Add->Next_PCB=Head;
    Head=Add;
  }
  else 
  {
    // Otherwise leak memory.
    tcn=Head;

    // Step through our list until we hit the end (Next_PCB == NULL)
    // Or the value we're adding is bigger than what is in the list.
    // Confusing as hell to swap from < and >= above on the Head check.
    while (tcn->Next_PCB != NULL && tcn->Next_PCB->PID < Add->PID)
    {
      tcn=tcn->Next_PCB;
    }

    // Point Add's next pointer to our current next pointer 
    // (Hope it isn't NULL because boom...)
    // And point the current list's Next to Add to complete list insertion.
    Add->Next_PCB=tcn->Next_PCB;
    tcn->Next_PCB=Add;
  }
}

void print_list(struct PCB *Head)
{
  // Malloc some memory and leak it just for the hell of it.
  tmp=(struct PCB *) malloc(sizeof(struct PCB));
  tmp=Head;

  // Step through the list and print IDs.
  while (tmp != NULL) 
  {
    printf("%d\n", tmp->PID);
    tmp=tmp->Next_PCB;
  }

  EXIT_SUCCESS;
}