Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.4 kB
1
Indexable
Never
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "list.h"

#define HELLO_STRING "Hello"

// List ReadList(List l) {
//     Create(&l);
//     char *str = NULL, c;
//     int len = 1;
//     str = (char*) malloc(sizeof(char));
//     // Iterator i = First(&l);
//     Iterator last = Last(&l);

//     while (scanf("%c", &c) != EOF) {
//         if (c == '\n') {
//             str[len - 1] = '\0';
            
//             Insert(&l, &last, str);

//             str = (char*) malloc(sizeof(char));
//             len = 1;
//             continue;
//         }
//         str[len - 1] = c;
//         len++;
//         str = (char*) realloc(str, len);
//     }
//     Insert(&l, &last, str);
//     str = (char*) malloc(sizeof(char));
//     free(str);
//     return l;
// }


char* ReadString() {
    char* str = malloc(sizeof(char));
    char c;
    int i = 0;

    while ((c = getchar()) != '\n') {
        str[i] = c;
        i++;
        str = realloc(str, sizeof(char) * (i + 1));
    }   
    str[i] = '\0';
    return str;
}



void PrintList(List l) {
    printf("\n");
    for (Iterator i = First(&l), last = Last(&l); !Equal(&i, &last); Next(&i, &l)) {
        char* res = Fetch(&i);
        printf("-%s-\n", res);
    }
}

void InsertElement(List l, int i, char* str) {
    if (0 <= i && i <= Size(&l)) {
        Iterator t = First(&l);
        for (int k = 0; k < i ; k++) {
            Next(&t, &l);
        }
        Insert(&l, &t, str);
    } else {
        printf("Error: index out of range");
    }
}

void DeleteElement(List l, int i) {
    if (0 <= i && i < Size(&l)) {
        Iterator t = First(&l);
        for (int k = 0; k < i ; k++) {
            Next(&t, &l);
        }
        Delete(&l, &t);
    } else {
        printf("Error: index out of range");
    }
}

List ReverseList(List l) {
    int sz = Size(&l);
    Iterator k = First(&l);
    for (int i = 0; i < sz; i++) {
        InsertElement(l, sz, Fetch(&k));
        Next(&k, &l);
    }
    for (int i = 0; i < sz; i++) {
        DeleteElement(l, 0);
        Next(&k, &l);
    }
    return l;
}

void Menu(List l) {
    printf("What do you want to do:\n");

    printf("1. Insert element\n");
    printf("2. Delete element\n");
    printf("3. Print list size\n");
    printf("4. Print list\n");
    printf("5. Rearrange the elements of the list in reverse order\n");
    printf("If you want to quit press: ctrl + c\n");
    int command, index;
    scanf("%d", &command);

    switch (command)
    {
    case 1:
        printf("Insert mode ...\n\n");
        printf("Enter index: ");
        scanf("%d", &index);
        printf("\nEnter your string: ");
        char* str =  ReadString();
        
        InsertElement(l, index, str);
        free(str);
        Menu(l);
    case 2:
        printf("Delete mode ...\n\n");
        printf("Enter index: ");
        scanf("%d", &index);
        DeleteElement(l, index);
        free(str);
        Menu(l);
        break;
    case 3:
        printf("\nSize of the list: %d\n", Size(&l));
        Menu(l);
        break;
    case 4:
        PrintList(l);
        Menu(l);
        break;
    default:
        printf("Incorrect command\n");
        Menu(l);
        break;
    }
}





int main() {

    List l;
    Create(&l);

    // char* str = ReadString();

    // printf("--%s--\n", str);

    Menu(l);




    
    Destroy(&l);
    return 0;
}