Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- FILE *file1, *file2;
- int valh = 0, valt = 0, total = 0;;
- struct node
- {
- unsigned int nr, occ;
- struct node *next;
- };
- struct s
- {
- unsigned int nr;
- };
- struct node *create(struct node *head, unsigned int n1)
- {
- struct node *p, *q;
- p = (struct node*)malloc(sizeof(struct node));
- if (!p)
- {
- printf("Error at allocating the memory");
- exit(0);
- }
- p->nr = n1;
- p->occ = 1;
- p->next = NULL;
- if (head == NULL) {
- head = p;
- head->occ = 1;
- return head;
- }
- if (p->nr < head->nr) {
- p->next = head;
- return p;
- }
- if (p->nr == head->nr) {
- head->nr++;
- free(p);
- return head;
- }
- q = head;
- while (q ->next != NULL && p->nr > q->next->nr)q = q->next;
- if (q ->next != NULL && q->next->nr == p->nr) {
- q->next->nr++;
- free(p);
- return head;
- }
- else {
- p->next = q->next;
- q ->next = p;
- return head;
- }
- }
- void displaylist(struct node *head, int nr)
- {
- struct node *p;
- p = head;
- if (p == NULL)
- printf("lIST IS EMPTY!");
- while (p != NULL)
- {
- double percent = (double)((p->occ) * 100) / nr;
- fprintf(file2, "%u : %u : %lf \n", p->nr, p->occ, percent);
- p = p->next;
- }
- }
- int main(int argc, char* argv[])
- {
- struct node *head= NULL;
- int nr = 0;
- file1 = fopen("file2.bin", "rb");
- if (!file1)
- {
- printf("File input not open!");
- exit(0);
- }
- file2 = fopen("out.txt", "w");
- if (!file2)
- {
- printf("File output not open!");
- exit(0);
- }
- struct s input;
- while (fread(&input, sizeof(struct s), 1, file1))
- {
- head = create(head, input.nr);
- nr++;
- }
- displaylist(head, nr);
- fclose(file1);
- fclose(file2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement