A
A
Andrey2018-10-12 20:24:28
C++ / C#
Andrey, 2018-10-12 20:24:28

How to populate a list from a stream?

Need help. It is necessary to fill the list with words from the file.

Library:
#ifndef DLIST_H
#define DLIST_H
#define LISTSIZE 1000
 
typedef enum{false, true} bool;
 
typedef struct NODE NODE;
typedef struct NODE* PNODE;
typedef struct DLIST DLIST;
typedef struct DLIST* PDLIST;
 
struct NODE {
    int key;
    char *info;
    PNODE prev, next;
}node;
 
struct DLIST {
    int count, size;
    PNODE left, right;
}list;
 
void initList(PDLIST lst);
bool isEmpty(PDLIST lst);
bool isOverflow(PDLIST lst);
bool addLeft(PDLIST lst, PNODE nd);
bool addRight(PDLIST lst, PNODE nd);
PNODE getLeft(PDLIST lst);
PNODE getRight(PDLIST lst);
PNODE findList(PDLIST lst, int k);
bool deleteList(PDLIST lst, int k);
bool insertRight(PDLIST lst, PNODE nd, int k);
bool insertLeft(PDLIST lst, PNODE nd, int k);
void printList(PDLIST lst);
void destroy(PDLIST lst);
 
#endif
Functions:
#include <stdlib.h>
#include <stdio.h>
#include "dlist.h"
 
void initList(PDLIST lst) {
    lst->count = 0;
    lst->size = LISTSIZE;
    lst->left = lst->right = NULL;
}
 
bool isEmpty(PDLIST lst) {
    return lst->count==0?true:false;
}
 
bool isOverflow(PDLIST lst) {
    return lst->count>=lst->size?true:false;
}
 
bool addLeft(PDLIST lst, PNODE nd) {
    bool res = false;
    if(!isOverflow(lst)) {
        res = true;
        if(isEmpty(lst)) {
            lst->count=1;
            lst->left = lst->right = nd;
            nd->prev = nd->next = NULL;
        } else {
            lst->count++;
            nd->next = lst->left;
            nd->prev = NULL;
            lst->left->prev = nd;
            lst->left = nd;
        }
    }
    return res;
}
 
bool addRight(PDLIST lst, PNODE nd) {
    bool res = false;
    if(!isOverflow(lst)) {
        res = true;
        if(isEmpty(lst)) {
            lst->count=1;
            lst->left = lst->right = nd;
            nd->prev = nd->next = NULL;
        } else {
            lst->count++;
            lst->right->next = nd;
            nd->prev = lst->right;
            nd->next = NULL;
            lst->right = nd;
        }
    }
    return res;
}
 
PNODE getLeft(PDLIST lst) {
    PNODE nd = NULL;
    if(!isEmpty(lst)) {
        if(lst->count==1) {
            nd = lst->left;
            lst->count = 0;
            lst->left = lst->right = NULL;
        } else {
            nd = lst->left;
            lst->count--;
            lst->left = lst->left->next;
            lst->left->prev = NULL;
        }
    }
    return nd;
}
 
PNODE getRight(PDLIST lst) {
    PNODE nd = NULL;
    if(!isEmpty(lst)) {
        if(lst->count==1) {
            nd = lst->right;
            lst->count = 0;
            lst->left = lst->right = NULL;
        } else {
            nd = lst->right;
            lst->count--;
            lst->right = lst->right->prev;
            lst->right->next = NULL;
        }
    }
    return nd;
}
 
PNODE findList(PDLIST lst, int k) {
    PNODE nd = lst->left;
    while(nd!=NULL && nd->key!=k) {
        nd = nd->next;
    }
    return nd;
}
 
bool deleteList(PDLIST lst, int k) {
    bool res = false;
    PNODE nd = findList(lst, k);
    if(nd!=NULL) {
        res = true;
        nd->prev->next = nd->next;
        nd->next->prev = nd->prev;
        lst->count--;
        free(nd);
    }
    return res;
}
 
bool insertLeft(PDLIST lst, PNODE nd, int k) {
    PNODE nd1;
    bool res = false;
    if(!isOverflow(lst)) {
        res = true;
        if(isEmpty(lst)) {
            lst->count = 1;
            lst->right = lst->left = nd;
            nd->prev = nd->next = NULL;
        } else {
            nd1 = findList(lst, k);
            if(nd1==NULL || nd1==lst->right) {
                nd->prev = lst->right;
                lst->right->next = nd;
                nd->next = NULL;
                lst->right = nd; //???
                lst->count++;
            } else {
                nd->next = nd1->next;
                nd1->next->prev = nd;
                nd1->next = nd;
                nd->prev = nd1;
                lst->count++;
            }
        }
    }
    return res;
}
 
bool insertRight(PDLIST lst, PNODE nd, int k) {
    PNODE nd1;
    bool res = false;
    if(!isOverflow(lst)) {
        res = true;
        if(isEmpty(lst)) {
            lst->count = 1;
            lst->right = lst->left = nd;
            nd->prev = nd->next = NULL;
        } else {
            nd1 = findList(lst, k);
            if(nd1==NULL || nd1==lst->left) {
                nd->next = lst->left;
                lst->left->prev = nd;
                nd->prev = NULL;
                lst->left = nd;
                lst->count++;
            } else {
                nd->prev = nd1->prev;
                nd->next = nd1;
                nd1->prev->next = nd;
                nd1->prev = nd;
                lst->count++;
            }
        }
    }
    return res;
}
 
void printList(PDLIST lst) {
    PNODE nd = lst->left;
    int i;
    for (i = 0; i < LISTSIZE; i++)
        printf("====");
    printf("\n");
    while(nd!=NULL) {
        printf("[%s] ", nd->info);
        nd = nd->next;
    }
    printf("\n");
    for (i = 0; i < LISTSIZE; i++)
        printf("====");
    printf("\n");
}
 
void destroy(PDLIST lst) {
    PNODE nd, nd1;
    nd = nd1 = lst->left;
    while(nd!=NULL) {
        nd1 = nd;
        nd = nd->next;
        free(nd1);
    }
}

My code:
#include <stdio.h>
#include "dlist.h"
#include <stdlib.h>
#include <string.h>
 
int main() {
    PNODE nd;
    PDLIST lst;
    int i, res, keyCounter = 0;
 
    lst = (PDLIST)malloc(sizeof(DLIST));
    initList(lst);
 
    FILE *polidromFile;
    if ((polidromFile = fopen("polidrom.txt", "r")) == NULL) {
        printf("File not found !\n");
        return 1;
    }
 
    for (i = 0; i < LISTSIZE; i++) {
        nd = (PNODE)malloc(sizeof(NODE));
        nd->key = keyCounter;
        fscanf(polidromFile, nd->info);
        addLeft(lst, nd);
        keyCounter++;
    }
    printf("LIST:\n");
    printList(lst);
    printf("\n");
 
    fclose(polidromFile);
 
    return 0;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question