ZBLOG

参照: 参考程序 2: #include typedef struct DataNode{ char *data; struct DataNode *next; } DataNode; typedef struct{ DataNode *head, *tail; } Que; void queue_init(Que *myroot) { myroot->head=NULL; myroot->tail=NULL; } void queue_put(Que *...

以下是一个基于你提供的参考程序的完整示例,它以队列的方式管理内存。程序会将输入字符串存入队列中,并在最后逐个释放这些节点。同时,我会添加一些注释,帮助理解程序结构。

请确保你的环境支持 C 语言编译,如 GCC 或 Clang,并且记得链接标准库。

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

typedef struct DataNode {
    char *data;
    struct DataNode *next;
} DataNode;

typedef struct { 
    DataNode *head, *tail; 
} Que;

// 初始化队列
void queue_init(Que *myroot) { 
    myroot->head = NULL;
    myroot->tail = NULL;
}

// 入队操作
void queue_put(Que *myroot, DataNode *myDataNode) {
    if (myroot->tail != NULL)
        myroot->tail->next = myDataNode; // 将当前尾节点指向新节点
    myroot->tail = myDataNode; // 更新尾节点为新节点
    if (myroot->head == NULL)
        myroot->head = myDataNode; // 如果队列为空,则头节点也指向新节点
}

// 出队操作
DataNode* queue_get(Que *myroot) { 
    DataNode *myDataNode = myroot->head; // 保存当前头节点
    
    if (myroot->head != NULL) {
        myroot->head = myroot->head->next; // 移动头指针到下一个节点
        if (myroot->head == NULL)
            myroot->tail = NULL; // 如果新的头指针为空,更新尾指针为NULL
        
        myDataNode->next = NULL; // 断开旧头与后续节点的连接
    }
    
    return myDataNode; // 返回被取出的数据节点
}

int main() {
    Que *myroot;
    DataNode *tmpNode;
    char str[1024];
    
    int qs, ts, i;
    
    qs = sizeof(Que);
    ts = sizeof(DataNode);
    
    printf("sizeof(Que): %d, sizeof(DataNode): %d\n", qs, ts);
    
    // 分配内存给队列结构体并初始化
    myroot = (Que *)malloc(qs);
    
    if (myroot == NULL) {
        fprintf(stderr, "Failed to allocate memory for queue.\n");
        return EXIT_FAILURE;
    }

  	queue_init(myroot);  // 初始化队列

  	// 从用户输入填充数据到队列中(最多4个)
  	for(i=0; i<4; i++) {
      	printf("Enter a string: ");
      	scanf("%s", str);

      	// 分配内存给数据节点和字符串数据
      	tmpNode=(DataNode *)malloc(ts);
      	if (tmpNode == NULL) {
          	fprintf(stderr, "Failed to allocate memory for data node.\n");
          	free(myroot);  // 清理已分配内存
          	return EXIT_FAILURE;
      	}
        
      	tmpNode->data=(char *)malloc(strlen(str)+1); 
      	if(tmpNode->data == NULL){
          	fprintf(stderr, "Failed to allocate memory for string data.\n");
          	free(tmpNode);  // 清理已分配内存
          	free(myroot);
          	return EXIT_FAILURE;
      	}

      	strcpy(tmpNode->data,str); 
      	tmpNode->next=NULL;

      	queue_put(myroot,tmpNode); 

      	printf("queue_put a node , used: %ld\n", strlen(str)+1+ts);
  	}

  	while(myroot -> tail != NULL) {  
      tmpNode=queue_get(myroot);  
      printf("queue_get a node: %s\n", tmpNode -> data); 

      free(tmpNode -> data);  // 回收字符串数据的内存空间   
      free(tmpNode);          // 回收数据节点的内存空间  
  	}

  	free(myroot);             // 最后清理整个队列结构体
  
  	return 0;
}

程序说明:

  1. 结构体定义DataNode 用于保存字符串及其在链表中的下一个元素。Que 用于表示整个队列,包括头和尾指针。

  2. 函数实现

    • queue_init():初始化队列,将头和尾指针设为 NULL
    • queue_put():将新的 DataNode 加入到队列末尾。
    • queue_get():从队列前端移除并返回一个 DataNode。
  3. 主函数

    • 用户最多可以输入四个字符串,每个字符串都将被封装成一个 DataNode 并加入到队列中。
    • 在输出所有元素后,释放每个元素所占用的动态内存,以防止内存泄漏。

编译与运行:

使用以下命令编译并运行代码(假设文件名为 main.c):

gcc main.c -o main && ./main

希望这个示例能帮助你更好地理解如何以队列方式管理动态内存。如果你有进一步的问题或需要额外功能,请告诉我!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=19168

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?