-1
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;

struct node
{
    string data;
    node* next;
    node* bel;
};

class tree
{
public:

    node* head;
    tree()
    {
        head = NULL;
    }

    void insertmov(string mov)
    {
        node* n = new node;
        n->next = NULL;
        n->bel = NULL;
        n->data = mov;
        if (head == NULL)
        {
            head = n;
        }
        else
        {       
            node* temp = new node;
            
            temp = head;
            while (temp->next != NULL)
            {
                    
                temp = temp->next;
            }
            temp->next = n;
        }
    }

    void insert_actor(string mov, string act)
    {
        node* temp = new node;

        node* n = new node;
        n->next = NULL;
        n->bel = NULL;
        n->data = act;

        for (temp = head; temp != NULL; temp = temp->next)
        {
            if (temp->data == mov)
            {
                for (temp = temp; temp != NULL; temp = temp->bel)
                {
                    if (temp->bel == NULL)
                    {
                        temp->bel = n;
                        break;
                    }
                }

                break;
            }
        }
    }

    void printm(node *n)
    {
        node* temp = new node;
        temp = n;

        if (temp == NULL)
        {
            cout << "\nNo Movie ";
        }
        else if (temp->next == NULL)
        {
            cout  << temp->data<<endl;
        }
        else
        {
            cout  << temp->data<<endl;
            printm(temp->next);
        }
    }

    void print_actor(string mov)
    {
        node* temp = new node;
        temp = head;

        if (temp == NULL)
        {
            cout << "\nNo Movie ";
        }
        else
        {
            while (temp != NULL)
            {
                if (temp->data == mov)
                {
                    while (temp != NULL)
                    {
                        temp = temp->bel;
                        cout << "\n" << temp->data;
                        if (temp->bel == NULL)
                            break;
                    }
                }
                else
                {
                    temp = temp->next;
                }

                if (temp->next == NULL)
                    break;
            }
        }
    }
};

int main()
{
    tree t;
    ifstream input;
    int ch;
    input.open("C:\\Users\\Zubair\\mov.txt");
    string sec, fir;

    while (!input.eof())
    {
        getline(input, fir);
        if (fir == "#")
        {
            getline(input, sec);
            t.insertmov(sec);
        }
        else
        {
            t.insert_actor(sec, fir);
        }
    }

    input.close();

    do
    {
        cout << "\n\nMenu \n";
        cout << "1.Show the list of movies \n";
        cout << "2.Search\n";
        cout << "3.Exit\n";
        cout << "Enter Your Choice \n";
        cin >> ch;
        switch (ch)
        {
            case 1:
            {
                system("CLS");
                cout << "\n List of Movies \n";
                t.printm(t.head);
                break;
            }

            case 2:
            {
                string st;
                char inp[50];
                system("CLS");
                cin.ignore();
                cout << "\n\n Enter The Name Of Moive \n";
                getline(cin, st);
                t.print_actor(st);
                break;
            }
        }
    } while (ch != 3);
}

The task is to build this database using a Linked List data structure.

Tasks:

  1. String (to store the name of the movie or an actor)
  2. Pointer to the next node in the linked list.
  3. Pointer to the node below the current node.

image

Problem :

I am unable to search the first and last movie, and not getting answers.

PS: I am a noob but want to learn. Ignore my silly mistakes and inform me about it so that I can improve them.

image

File contains data in a way that the first line represents a movie and then some actors that worked in the movie. Then, before the next movie there is # so it can be detected.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The file contains data like this: 2 secondes (1998) John Walsh (III) Michael Scherer Ralph Bellamy # Accomplice (1946) Richard Arlen Walter Matthau Shannon Tweed – Zubair memon Dec 20 '21 at 18:19
  • What is the contents of `mov.txt`? What input do you enter into the program when it runs, what do you expect the output to be, and what does it actually output (and why is that wrong)? – scohe001 Dec 20 '21 at 18:20
  • What do you imagine `temp = temp;` does? – Drew Dormann Dec 20 '21 at 18:20
  • 2
    Why write a comment with information that should be in the question? Edit your question and add the information. I also feel like a list != tree. – sweenish Dec 20 '21 at 18:24
  • temp=temp , represent same movie whos actor we are gonna display – Zubair memon Dec 20 '21 at 18:25
  • u know there is no reason to shorten variable names so that can't be read properly. so e.g. instead of void insertmov(string mov) using insertMovie(string movieName) would not somehow inadvisably affect your program's performance but would make it easier to read. – AndersK Dec 20 '21 at 23:23

1 Answers1

0

I see a number of design and logic issues with your code, including:

  • memory leaks

  • an incorrect variable assignment in insert_actor(), and bad search logic in print_actor() that can lead to a runtime crash.

  • using node::bel as a downward-moving list when it makes more sense to use it as the head of a secondary sideways-moving list.

  • using incorrect logic to load the file into the tree to begin with:

    • while (!input.eof()) is bad
    • on the 1st loop iteration, you call getline() to read in a movie name, but since it is not #, you pass it to t.insert_actor() as an actor without a movie, rather than pass it to t.insertmov().
    • on subsequent loop iterations before the 1st #, you read in each actor and pass it to t.insert_actor() without a movie, since you haven't saved the current movie name anywhere yet.
    • once the 1st # is read in, you read in the next movie name and its actors and insert them into t correctly, repeating for each movie, until the final # is reached.
    • when the last # is read, you attempt to read in another movie that doesn't exist in the file, and pass it to t.insertmov() even though getline() failed. Then the loop ends due to eof.

Also, when inserting a new node, it is easier/customary to use a pointer-to-pointer rather than having to use an if-else to branch the code based on whether the head is NULL or not.

Try something more like this instead:

#include <iostream>
#include <string>
#include <fstream>
#include <limits>
#include <iomanip>
using namespace std;

struct node
{
    string data;
    node* next;
    node* bel;

    node(string data)
        : data(data), next(NULL), bel(NULL)
    {
    }

    ~node()
    {
        node* next;
        for(; bel != NULL; bel = next)
        {
            next = bel->next;
            delete bel;
        }
    }

    node* insert_bel(string data)
    {
        node** b;
        for (b = &bel; *b != NULL && (*b)->data != data; b = &((*b)->next));
        if (*b == NULL)
            *b = new node(data);
        return *b;
    }
};

class tree
{
    node* head;

    tree(const tree &) {}
    tree& operator=(const tree &) { return *this; }

public:
    tree() : head(NULL) {}

    ~tree()
    {
        clear();
    }

    void clear()
    {
        node* next;
        for (; head != NULL; head = next)
        {
            next = head->next;
            delete head;
        }
    }

    node* find_mov(string mov)
    {
        node* m;
        for (m = head; m != NULL; m = m->next)
        {
            if (m->data == mov)
                break;
        }
        return m;
    }

    node* insert_mov(string mov)
    {
        node** m;
        for (m = &head; *m != NULL && (*m)->data != mov; m = &((*m)->next));
        if (*m == NULL)
            *m = new node(mov);
        return *m;
    }

    void insert_actor(string mov, string act)
    {
        node* m = insert_mov(mov);
        m->insert_bel(act);
    }

    void load(string filename)
    {
        ifstream input(filename);
        load(input);
    }

    void load(istream &input)
    {
        clear();

        string mov, act;

        /*
        while (getline(input, mov))
        {
            while (getline(input, act) && act != "#")
                insert_actor(mov, act);
        }
        */

        while (getline(input, mov))
        {
            node* m = insert_mov(mov);

            while (getline(input, act) && act != "#")
                m->insert_bel(act);
        }
    }

    void print_mov()
    {
        if (head == NULL)
        {
            cout << "No Movie\n";
        }
        else
        {
            for(node *m = head; m != NULL; m = m->next)
                cout << m->data << "\n";
        }
    }

    void print_actors(string mov)
    {
        node *m = find_mov(mov);
        if (m == NULL)
        {
            cout << "No Movie\n";
        }
        else
        {
            for (node* a = m->bel; a != NULL; a = a->next)
                cout << a->data << "\n";
        }
    }
};

int main()
{
    tree t;
    t.load("C:\\Users\\Zubair\\mov.txt");

    bool keepRunning = true;
    do
    {
        cout << "\n\nMenu\n";
        cout << "1. Show the list of movies\n";
        cout << "2. Search\n";
        cout << "3. Exit\n";
        cout << "Enter Your Choice \n";

        int ch;
        if (!(cin >> ch))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Bad Input";
            continue;
        }

        if (ch < 1 || ch > 3)
        {
            cout << "Bad Input";
            continue;
        }

        switch (ch)
        {
            case 1:
            {
                system("CLS");
                cout << "\nList of Movies\n";
                t.print_mov();
                break;
            }

            case 2:
            {
                system("CLS");
                cout << "\n\nEnter the name of Movie\n";
                string mov;
                if (getline(cin >> ws, mov))
                    t.print_actors(mov);
                break;
            }

            case 3:
            {
                keepRunning = false;
                break;
            }
        }
    }
    while (keepRunning);
}

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770