我正在尝试为我的类创建一个linkedQueue数据结构程序

时间:2017-10-02 00:14:13

标签: c pointers data-structures linked-list queue

我觉得我接近完成,但我并不完全确定。我的deQueue方法出错,我试图指向(* head)并等于nodeString。错误是EXC_BAD_ACCESS。这个程序是用C语言编写的。

这个程序最终应该是一个压力测试,我必须运行它1,000,000次,我只是排队,然后每次程序运行时出现一个100,000字符的字符串。我可能做得不对,但我认为我的算法非常接近。我大多只是想弄清楚为什么我用指针得到错误。我不太熟练使用它们,因为我们刚刚在课堂上学到它们。

我甚至为我宣布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

0 个答案:

没有答案