首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用structs的C程序发生了什么?/工作不正常

使用structs的C程序发生了什么?/工作不正常
EN

Stack Overflow用户
提问于 2015-05-06 02:28:39
回答 2查看 231关注 0票数 0

我目前正在修复我在过去几周里一直在做的一段代码。这样做的目的很大程度上是生成一个结构链表。目前它没有生成任何东西,但我仍在编写代码。我可以使用任何关于如何正确实现insert_ordered的建议。谢谢!

代码语言:javascript
运行
复制
#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);
}

这段代码现在可以编译了,但是我目前没有从文件中得到任何输出。

EN

回答 2

Stack Overflow用户

发布于 2015-05-06 02:53:44

简而言之,struct (用外行的话来说非常简短):同名的数据集合。More info。基本上,您可以将所有测试分数的值(例如)存储在

代码语言:javascript
运行
复制
struct test_scores {
    int test1;
    int test2;
    int test3;
    ...
}

您可以在main下使用以下命令设置它们的值

代码语言:javascript
运行
复制
main() 
{
    struct test_scores Math;
    Math.test1 = 90;
    Math.test2 = 85;
    Math.test3 = 100;
}

(当然,周围有更多的代码!)struct PCB *ptr, *tmp, *tcn;行中的*表示一个pointer (是的,我确实非常喜欢cprogramming.com!这是我学习c++的地方)

你看起来确实对这些东西很在行(在我输入这篇文章的时候,我正在阅读出现的评论)。我认为让你困惑的是这句话,因为它让我困惑了很长一段时间(诚然,现在仍然如此):

代码语言:javascript
运行
复制
struct PCB *Next_PCB;

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

你似乎明白你在做什么,或者仅仅是你的编辑很幸运!对于所有“实际的”c程序员来说,我知道我把事情简化了很多!如果你仍然有困难,我愿意比解释结构和链表是如何工作的更深入的一步。

编辑:在我发布这篇文章之前/之后,你评论说它不能编译,我来看看代码,试着帮助你。Edite2.0:和一些人比我更早做到这一点。哦,好吧

票数 2
EN

Stack Overflow用户

发布于 2015-05-06 02:54:55

我继续对上面的代码进行了注释,指出了一些缺陷和大量的内存泄漏。

代码语言:javascript
运行
复制
#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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30060600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档