C 我正在尝试为我的a类制作一个linkedQueue数据结构程序
我觉得我快要完成了,但我不能完全确定。我的C 我正在尝试为我的a类制作一个linkedQueue数据结构程序,c,pointers,data-structures,linked-list,queue,C,Pointers,Data Structures,Linked List,Queue,我觉得我快要完成了,但我不能完全确定。我的deQueue方法出错,我试图指向(*head)并等于nodeString。错误是EXC\u BAD\u ACCESS。这个程序是用C语言编写的 这个程序最终应该是一个压力测试,我必须运行它1000000次,在这里我只是排队,然后每次程序运行时将100000个字符的字符串出列。我可能做得不对,但我认为我的算法非常接近。我主要只是想弄清楚为什么指针会出错。我不太擅长使用它们,因为我们只是在课堂上学习的 我甚至为部分问题添加了一个解决方案,其中我声明了cha
deQueue
方法出错,我试图指向(*head)并等于nodeString
。错误是EXC\u BAD\u ACCESS
。这个程序是用C语言编写的
这个程序最终应该是一个压力测试,我必须运行它1000000次,在这里我只是排队,然后每次程序运行时将100000个字符的字符串出列。我可能做得不对,但我认为我的算法非常接近。我主要只是想弄清楚为什么指针会出错。我不太擅长使用它们,因为我们只是在课堂上学习的
我甚至为部分问题添加了一个解决方案,其中我声明了
char*nodeString=NULL代码>,我以前没有*
。尽管它不喜欢我使用->
时
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
char nodeValue;//intitial filling of the queue
char nodeString;//for the main running of the program: enqueue and dequeue 1000000 times
int charCount = 0;//counts the amount of chars in the strings that fill the nodes.
int nodeCount = 0;//keeps track of the amount of nodes
int runCount = 0;//keeps track of amount of times ran
struct Node{
char data;
struct Node *next;
};
void enQueue(char * string, struct Node ** head, struct Node ** tail){
struct Node* newNode;
newNode = (struct Node*) malloc(sizeof(struct Node));
if(*head == NULL){
newNode = *head;
newNode = *tail;
}//if the queue is empty
newNode->data = (*string);//newNode obtains the data of the passed string
newNode->next = (*tail);//the next node is set to NULL
};
void * deQueue(struct Node **head, struct Node **tail){
char * nodeString = NULL;
struct Node* tempNode;
tempNode = (struct Node*) malloc(sizeof(struct Node));
*nodeString = (*head)->data;
nodeString = (*head);//value is set to the data of the head node
tempNode = *head;//tempNode is set to the head node
*head = (*head)->next;
free(tempNode);
//return &nodeString;
return 0;
};
////creates a 100000 character string for the node
char * createString(){
char *str = (char *) malloc(sizeof(char) * 10);
while(charCount <= 10){
char rand = 'A' + (random() % 26);
str[charCount] = rand;
charCount++;
}//while charCount < 15
return str;
};
int main(){
//makes the node
struct Node ** nodeH;
struct Node ** nodeT;
nodeH = (struct Node**) malloc(sizeof(struct Node));
nodeT = (struct Node**) malloc(sizeof(struct Node));
while(nodeCount < 10){
char *nodeString = createString();
//nodeValue = ;//creating a string with the createString function
enQueue(nodeString, nodeH, nodeT);//passing to enQueue
nodeCount++;//increment nodeCount
}//while nodeCount < 10
while(runCount <= 50){
if(nodeCount <= 10){
char *nodeString = createString();//creating a new string to enQueue everytime I deQueue
deQueue(nodeH, nodeT);//deQueue
nodeCount--;//substract nodeCount when deQueue
enQueue(nodeString, nodeH, nodeT);//enQueue
nodeCount++;//add nodeCount when enQueue
}//if
runCount++;
}//while runCount <= 1000000
}//main
#包括
#包括
#包括
#包括
#包括
字符节点值//队列的初始填充
字符节点限制//对于程序的主要运行:入队和出队1000000次
int charCount=0//计算填充节点的字符串中的字符数。
int nodeCount=0//跟踪节点的数量
int runCount=0//跟踪运行的次数
结构节点{
字符数据;
结构节点*下一步;
};
无效排队(字符*字符串,结构节点**头,结构节点**尾){
结构节点*newNode;
newNode=(结构节点*)malloc(sizeof(结构节点));
如果(*head==NULL){
newNode=*头;
newNode=*尾;
}//如果队列为空
newNode->data=(*string);//newNode获取传递字符串的数据
newNode->next=(*tail);//下一个节点设置为NULL
};
void*出列(结构节点**头,结构节点**尾){
char*nodeString=NULL;
结构节点*临时节点;
tempNode=(结构节点*)malloc(sizeof(结构节点));
*nodeString=(*head)->数据;
nodeString=(*head);//值设置为head节点的数据
tempNode=*head;//tempNode设置为head节点
*头部=(*头部)->下一个;
自由(tempNode);
//回报与无约束;
返回0;
};
////为节点创建100000个字符串
char*createString(){
char*str=(char*)malloc(sizeof(char)*10);
while(charCount)您的createString()
已损坏。您分配了10个字节,但第一次写入11个字符时没有终止符,因此内存损坏可能会发生任何情况。请仔细研究。char*nodeString=NULL;..*nodeString=(*head)->数据;
:*nodeString=..
是NULL
取消引用的。好的,谢谢,但是现在在我的队列中出现了相同的错误,其中newNode->data=*string….YourcreateString()
已损坏。您分配了10个字节,但第一次写入11个字符时没有终止符,因此内存损坏时可能会发生任何情况。请仔细研究。char*nodeString=NULL;..*nodeString=(*head)->数据;
:*nodeString=..
是NULL
取消引用的。好的,谢谢,但是现在在我的队列中出现了相同的错误,其中newNode->data=*string。。。。