In C language, I need help inserting the
frequency of each character value in a file and insert them in a
Priority Queue. The code I have currently, uses Sturct pair() to
count the frequency of characters in a file. I need to add another
struct called struct Qnode(), etc. Here is the code I have, but the
priority Queue is not working.
Please use my code, and fix it.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
struct pair //struct to store frequency and value
{
int frequency;
char value;
};
struct Qnode
{
struct pair nodeValue;
struct Qnode *next;
struct Qnode *front;
};
void popQueue(struct Qnode *front)
{
struct Qnode *min = front;
struct Qnode *cur = front;
struct Qnode *prev = NULL;
while (cur != NULL)
{
if((cur -> nodeValue).value < (min ->
nodeValue).value)
min = cur;
prev = cur;
cur = cur->next;
}
if (cur != front)
{
prev->next = min->next;
}
else
{
front = front ->next;
}
//return min; (gave error saying is must not return
something)
}
void printQueue(struct Qnode *front)
{
struct Qnode *cur = front;
while (cur!= NULL)
{
printf("%c\n",cur->nodeValue.value);
}
cur = cur->next;
}
void pushQueue(struct Qnode *front, struct Qnode *newQnode)
{
newQnode->next = front;
front = newQnode;
}
struct Qnode *createQnode(struct pair Pairs)
{
struct Qnode *p = malloc(sizeof(struct Qnode));
(*p).next=NULL;
p->nodeValue = Pairs;
return p;
}
int isEmpty(struct Qnode** front)
{
return (*front) == NULL;
}
int main(int argc, char *argv[]) //command line takes in the
file of text
{
struct pair table[128]; //set to 128 because these are the main
characters
int fd; // file descriptor for opening file
char buffer[1]; // buffer for reading through files bytes
fd = open(argv[1], O_RDONLY); // open a file in read mode
for(int j = 0; j < 128; j++)//for loop to initialize the
array of pair (struct)
{
table[j].value = j; // table with index j sets the struct char
value to equal the index
table[j].frequency = 0; // then the table will initialize the
frequency to be 0
}
while((read(fd, buffer, 1)) > 0) // read each character and
count frequency
{
int k = buffer[0]; //index k is equal to buffer[0] with integer
mask becasue each letter has a ASCII number.
table[k].frequency++; //using the struct pair table with index k
to count the frequency of each character in text file
}
close(fd); // close the file
for (int i = 32; i < 128; i++) // use for loop to print
frequency of characters
{
if (table.frequency > 0)
printf("%c: %d\n",table.value, table.frequency); // print
characters and its frequency
}
struct Qnode *fr = NULL;
struct Qnode *np; // new pointer
for (int i = 0; i < table.value; i++)
{
np = createQnode (table.frequency); //whater frequency
pushQueue(fr,np);
}
while(!isEmpty(&np))
{
printf("%d \n", &np);
popQueue(np);
}
return 0; //end of code
}
In C language, I need help inserting the frequency of each character value in a file and insert them in a Priority Queue
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
In C language, I need help inserting the frequency of each character value in a file and insert them in a Priority Queue
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!