Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "crlg.h"
- #include <string.h>
- #define MAX 100
- typedef struct node *node_ptr;
- typedef struct node {
- char* word;
- int line[MAX];
- int position;
- node_ptr next;
- }node;
- typedef node_ptr list_ptr;
- static list_ptr L;
- void init_list(){
- L = NULL;
- }
- static node_ptr new_node(char *string, int linenr){
- node_ptr n = malloc(sizeof(struct node));
- if(n == NULL) {
- printf("Out of memory");
- exit(-1);
- }
- n->word = malloc(sizeof(string));
- strcpy(n->word, string);
- n->line[0] = linenr;
- n->position = 1;
- n->next = NULL;
- return n;
- }
- void append(char *string, int position){
- node_ptr newN = new_node(string, position);
- node_ptr last;
- if(L == NULL){
- L = newN;
- }
- else {
- last = L;
- while(last->next != NULL){
- last = last->next;
- }
- last->next = newN;
- }
- }
- void read_from_file(FILE *fp){
- char buffer[MAX];
- char *wordinput;
- int count = 0;
- int legit = 1;
- node_ptr check;
- const char delimiter[] = "1234567890#,.-!§$%&/()=?';:_{[]}’@€<> \"\n";
- while(fgets(buffer, sizeof(buffer), fp) != NULL){
- count++;
- wordinput = strtok(buffer, delimiter);
- while(wordinput != NULL) {
- if (L == NULL){
- append(wordinput, count);
- }
- else{
- check = L;
- while(check != NULL){
- if (strcmp(check->word, wordinput) == 0){
- check->line[check->position] = count;
- check->position++;
- legit = 0;
- }
- check = check->next;
- }
- if (legit == 1){
- append(wordinput, count);
- }
- else{
- legit = 1;
- }
- }
- wordinput = strtok(NULL, delimiter);
- }
- }
- }
- void print_list(){
- int i;
- node_ptr node;
- node = L;
- while (node != NULL){
- printf("%s ", node->word);
- for (i = 0; i < node->position; i++){
- printf("%d ", node->line[i]);
- }
- printf("\n");
- node = node->next;
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement