lundi 26 mars 2018

How to sort doubly linked list alphabetically and display their count?

I want to read a paragraph containing words from an input file. Then create a doubly-linked list containing the distinct words read, and their occurrence frequency. The list should be alphabetically ordered. Print the words and their frequencies in alphabetical ascending and descending order, case insensitive.

Example data:

Input:

Everything LaTeX numbers for you has a counter associated with it. The name of the counter is the same as the name of the environment or command that produces the number, except with no . Below is a list of some of the counters used in LaTeX’s standard document styles to control numbering.

Output:

.:1
a:2
as:1
associated:1
Below:1
...

Till now I have done reading and storing it to linked list file :

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct node
{
    struct node *next;
    string num;
    struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
struct node *display_processed(struct node *);

int main()
{
    int option;

    do
    {
        cout<<"\n\n *****MAIN MENU *****";
        cout<<"\n 1: Create a list";
        cout<<"\n 2: Display the list";
        cout<<"\n\n Enter your option : ";
        cin>>option;
        switch(option)
        {
            case 1:
            { start = create_ll(start);
                cout<<"\n DOUBLY LINKED LIST CREATED";

            }
                break;
            case 2:
            {
                start = display(start);
            }
                break;
            case 3:
            {
                start = display_processed(start);
            }
                break;
        }
    }while(option!=3);
}
struct node *create_ll(struct node *start)
{
    string word;

    ifstream file;

    file.open("/Volumes/MAC Extended/Softwares/Xcode/DSA project/DSA project/file.txt",ios::in);
    if (!file) {
        cout << "File Does not Exist";
        return 0;
    }
    struct node *new_node, *ptr;
    while(file>>word)
    {
        if(start == NULL)
        {
            new_node = new node;
            new_node -> prev = NULL;
            new_node -> num = word;
            new_node -> next = NULL;
            start = new_node;
        }
        else {

            ptr=start;
            new_node = new node;
            new_node->num=word;
            while(ptr->next!=NULL)
                ptr = ptr->next;
            ptr->next = new_node;
            new_node->prev=ptr;
            new_node->next=NULL;
        }
    }
    return start;
}
struct node *display(struct node *start)
{
    struct node *ptr;
    ptr=start;
    while(ptr!=NULL)
    {
        cout<<ptr -> num<<endl;
        ptr = ptr -> next;
    }
    return start;
}

struct node *display_processed(struct node *)
{
 //it should display linked-list alphabetically sorted    
}

Can anyone suggest the logic for display_processed so that it should display the list as stated in example above ?

Hope, it explains the problem clearly tried my level best to explain.

Aucun commentaire:

Enregistrer un commentaire