C中的多个数据结构
发布时间:2020-12-16 10:18:56 所属栏目:百科 来源:网络整理
导读:我有一个文件queue.c,用C语言定义一个队列.我如何使3个独立的队列相互独立?我对C不是很有经验,我在OO视图中一直在思考它,我知道我不能这样做. #include stdio.h#include stdlib.hstruct Node{ char data; struct Node *next;} *Head,*Tail;void addCharacte
我有一个文件queue.c,用C语言定义一个队列.我如何使3个独立的队列相互独立?我对C不是很有经验,我在OO视图中一直在思考它,我知道我不能这样做.
#include <stdio.h> #include <stdlib.h> struct Node { char data; struct Node *next; } *Head,*Tail; void addCharacter(char c) { struct Node *temp1,*temp2; temp1 = (struct Node *)malloc(sizeof(struct Node)); temp1->data = c; temp2 = Tail; if(Head == NULL) { Head = temp1; Head->next = NULL; Tail = Head; } else { Tail = temp1; temp1->next = NULL; temp2->next = temp1; } } void deleteCharacter() { struct Node *temp1 = Head; Head = temp1->next; free(temp1); } int replaceCharacter(char c) { Head->data = c; } int main() {} 这是我的队列,我对另一个C文件的所有内容基本上是: #include "queue.h" 我不知道从哪里去… 解决方法
而不是创建Head和Tail全局变量,而是创建包含它们的另一个结构,例如:
struct Queue { struct Node *head; struct Node *tail; }; 然后更改在队列上运行的函数以获取指向Queue结构的指针,并对其进行操作. 您还需要一个初始化head和tail为NULL的initQueue函数.然后使用队列可以看起来像: struct Queue queue1; initQueue(&queue1); addCharacter(&queue1,'a'); //.... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- flex 4.5安装调试 Logging API
- cocos2dx 2.2.6 hitWidget->onTouchEnded(pTouch, pEvent
- Ajax – 在后台运行PHP脚本进行通信?
- ios – performSegueWithIdentifier和viewDidLoad
- Oracle Database(rdbms) 12.2 安装组件
- React Native HelloWorld for mac
- ajax加载json三级联动-省市县
- reactor和proactor模式
- c# – 如何修剪以下字符串?
- iphone – NSString stringWithUTF8String中的内存泄漏