samedi 16 mai 2020

Print random words

I'm just trying to print random words and sort them, now I have a program that prints random numbers and it works, but I don't know why my code doesn't work with random words, I'm new to linked lists, if anyone can help me.There is something I've tried, i don't know why insert/display function doesn't work or either the random words generator.

#include <iostream>
#include <chrono>
#include <string.h>
#include <string>
#include <cstdlib>

using namespace std;

 static const char pool[] ={
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z'};
int poolsize=sizeof(pool)-1;

struct cuvinte
{
    int data;
    string cuvintele;
    cuvinte*next;
};

cuvinte* head,*p,*t;


void insertwords()
{
    string x;
    cuvinte**head_ref;

     cuvinte*temp=new cuvinte();
     temp->cuvintele=x;
     temp->next=*head_ref;
     *head_ref=new cuvinte();

}   

void display(cuvinte* head)
{
    cuvinte* temp = head;
    while (temp != NULL)
    {

        cout << " " << temp->cuvintele<<endl;
        temp = temp->next;

    }

}


cuvinte* getTail(cuvinte* cur)
{
    while (cur != NULL && cur->next != NULL)
        cur = cur->next;
    return cur;
}


cuvinte* partition(cuvinte* head, cuvinte* end, cuvinte** newHead, cuvinte** newEnd)
{
    cuvinte* pivot = end;
    cuvinte* prev = NULL, * cur = head, * tail = pivot;


    while (cur != pivot)
    {
        if (cur->data < pivot->data)
        {

            if ((*newHead) == NULL)
                (*newHead) = cur;

            prev = cur;
            cur = cur->next;
        }
        else
        {

            if (prev)
                prev->next = cur->next;
            cuvinte* tmp = cur->next;
            cur->next = NULL;
            tail->next = cur;
            tail = cur;
            cur = tmp;
        }
    }


    if ((*newHead) == NULL)
        (*newHead) = pivot;


    (*newEnd) = tail;


    return pivot;
}



cuvinte* sort(cuvinte* head, cuvinte* end)
{



    if (!head || head == end)
        return head;

    cuvinte* newHead = NULL, * newEnd = NULL;


    cuvinte* pivot = partition(head, end, &newHead, &newEnd);


    if (newHead != pivot)
    {

        cuvinte* tmp = newHead;
        while (tmp->next != pivot)
            tmp = tmp->next;
        tmp->next = NULL;


        newHead = sort(newHead, tmp);


        tmp = getTail(newHead);
        tmp->next = pivot;
    }


    pivot->next = sort(pivot->next, newEnd);

    return newHead;


}


void quickSort(cuvinte** headRef)
{
    auto start = std::chrono::high_resolution_clock::now();
    (*headRef) = sort(*headRef, getTail(*headRef));


    return;

    auto end1 = std::chrono::high_resolution_clock::now();
    std::chrono::duration<float>duration = end1 - start;
    std::cout <<duration.count()<< "s" << std::endl;

}






int main()
{
    string x;
    int n;
    cout<<"How many words:\n";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        x=x+pool[rand()%poolsize];    
        insertwords();
    }
    cout << "\nLista:\n";
    display(head);
    quickSort(&head);
    cout << "\nSortat,frumos:";
    display(head);


    return 0;
} 



Aucun commentaire:

Enregistrer un commentaire