如何将整数编号插入文本文件中的双向链表?

时间:2016-03-15 16:50:39

标签: c++

这个程序将显示文本文件

中的整数

1 2 3 4 5 6

并要求用户

  1. 添加数字“
  2. 从列表中删除号码
  3. 显示列表。
  4. 有人可以帮助我实施以下步骤:

    1.如何将文本文件中的整数插入到双向链表中。

    2.如果用户添加了数字,则应将该数字添加到文本文件中。

    3-如果用户删除号码,则应从文本文件中删除该号码。

    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    struct  node
    {
        int number; 
        node *next; 
        node *prev; 
    }*start;
    
    class double_llist
    {
    public:
        bool isEmpty(node *head);
        char menu();
        void insertAsFirstElement(node *&head, node *&last, int number);
        void inser(node*&head, node*&last, int number);
        void remove(node *&head, node *&last);
        void showlist(node *current);
        int numIntElement(ifstream&x);
    private:
    
    };
    
    int main() {
    
        string filename;
        cout << " Please enter the name of the file = "; 
        cin >> filename; 
        ifstream myfile; 
        myfile.open(filename); 
        if (!myfile.is_open()) {
            cout << endl << " failed to open file, check that it exists and you have access \n" << "\n" << endl; 
    
        }
        else if (myfile.is_open())
        {
            ifstream x;
            double_llist dl;
            dl.numIntElement(x); 
            int int1 = 0; 
            cout << " File exists \n";
    
            ////
            node * head = NULL;
            node *last = NULL;
            char choice;
            int number;
            do {
                choice = dl.menu();
                switch (choice)
                {
                case '1':
                    cout << " Please enter number : ";
                    cin >> number;
                    dl.inser(head, last, number);
                    break;
                case '2':
                    dl.remove(head, last);
                case '3':
                    dl.showlist(head);
                    break;
                default:
                    break;
                }
            } while (choice != '4');
            {
    
            }
        }
    
        system("pause");
        return 0; 
    }
    int double_llist::numIntElement(ifstream&x) {
    
        int n = 0;
        int m; 
        ifstream myfile("file.txt");
        int countsingfie = 0;
    
        while (!myfile.eof())
        {
            myfile >> n;
            //if (countsingfie == 0) {
            //  cout << "Error : file exist but no data " << endl;
            //}
            cout << "The numbers in the file are  " << n<< "" << endl;
        }
        return countsingfie;
    }
    bool double_llist::isEmpty(node *head) {
        if (head == NULL) {
            return true; 
        }
        else
        {
            return false;
        }
    }
    char double_llist::menu() {
        char choice; 
        cout << " Main Menu \n"; 
        cout << " 1 . Add a number \n";
        cout << " 2 . Delete a number from the list \n";
        cout << " 3 . Show the list \n"; 
        cout << " 4.  Exit \n";
        cin >> choice; 
        return choice;
    }
    void  double_llist::insertAsFirstElement(node *&head, node *&last, int number) {
        node *temp = new node; 
        temp->number = number; 
        temp->next = NULL; 
        head = temp; 
        last = temp; 
    }
    void double_llist::inser(node*&head, node*&last, int number) {
        if (isEmpty(head)>0){
            insertAsFirstElement(head, last, number); 
        }
        else if (isEmpty(head) < 0)
        {
            cout << " Please enter a number above 0 " << endl;
        }
        else
        {
            node *temp = new node;
            temp->number = number;
            temp->next = NULL;
            last->next = temp;
            last = temp;
        }
    }
    void  double_llist::remove(node *&head, node *&last) {
        if (isEmpty(head)) {
            cout << " Sorry, The list is already empty \n"; 
    
        }
        else if (head == last)
        { 
            delete head; 
            head = NULL;
            last = NULL; 
    
        }
        else
        {
            node *temp = head; 
            head = head->next; 
            delete temp; 
        }
    }
    void double_llist::showlist(node *current) {
    
        if (isEmpty(current)) {
            cout << " The list is empty \n";
        }
        else
        {
            cout << " The list contains: \n "; 
            while (current != NULL)
            {
                cout << current->number << endl; 
                current = current->next; 
    
            }
        }
    }
    

0 个答案:

没有答案