Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "crlg.h"
  4. #include <string.h>
  5.  
  6. #define MAX 100
  7.  
  8.  
  9. typedef struct node *node_ptr;
  10.  
  11. typedef struct node {
  12.   char* word;
  13.   int line[MAX];
  14.   int position;
  15.   node_ptr next;
  16. }node;
  17.  
  18. typedef node_ptr list_ptr;
  19.  
  20. static list_ptr L;
  21.  
  22. void init_list(){
  23.   L = NULL;
  24. }
  25.  
  26.  
  27. static node_ptr new_node(char *string, int linenr){
  28.     node_ptr n = malloc(sizeof(struct node));
  29.     if(n == NULL) {
  30.         printf("Out of memory");
  31.         exit(-1);
  32.     }
  33.     n->word = malloc(sizeof(string));
  34.     strcpy(n->word, string);
  35.     n->line[0] = linenr;
  36.     n->position = 1;
  37.     n->next = NULL;
  38.     return n;
  39. }
  40.  
  41. void append(char *string, int position){
  42.     node_ptr newN = new_node(string, position);
  43.     node_ptr last;
  44.     if(L == NULL){
  45.       L = newN;
  46.     }
  47.     else {
  48.       last = L;
  49.       while(last->next != NULL){
  50.         last = last->next;
  51.       }
  52.       last->next = newN;
  53.     }
  54. }
  55.  
  56.  void read_from_file(FILE *fp){
  57.   char buffer[MAX];
  58.   char *wordinput;
  59.   int count = 0;
  60.   int legit = 1;
  61.   node_ptr check;
  62.   const char delimiter[] = "1234567890#,.-!§$%&/()=?';:_{[]}’@€<> \"\n";
  63.   while(fgets(buffer, sizeof(buffer), fp) != NULL){
  64.     count++;
  65.     wordinput = strtok(buffer, delimiter);
  66.  
  67.     while(wordinput != NULL) {
  68.       if (L == NULL){
  69.         append(wordinput, count);
  70.       }
  71.       else{
  72.         check = L;
  73.         while(check != NULL){
  74.           if (strcmp(check->word, wordinput) == 0){
  75.             check->line[check->position] = count;
  76.             check->position++;
  77.             legit = 0;
  78.           }
  79.           check = check->next;
  80.         }
  81.         if (legit == 1){
  82.           append(wordinput, count);
  83.         }
  84.         else{
  85.           legit = 1;
  86.         }
  87.  
  88.       }
  89.       wordinput = strtok(NULL, delimiter);
  90.     }
  91.   }
  92. }
  93.  
  94. void print_list(){
  95.  
  96.     int i;
  97.     node_ptr node;
  98.     node = L;
  99.  
  100.         while (node != NULL){
  101.             printf("%s ", node->word);
  102.             for (i = 0; i < node->position; i++){
  103.               printf("%d ", node->line[i]);
  104.             }
  105.             printf("\n");
  106.             node = node->next;
  107.         }
  108.         printf("\n");
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement