如何使用动态数组的双链表?

时间:2014-03-18 20:10:35

标签: c++ arrays

我的作业是制作一份带有双向链表的时间表。我们可以创建一个动态数组来保持数天。但每天必须有一个包含时间段的双向链表。禁止使用向量和数组,而不是链接列表。我对功能有困难。

这是我的头文件:

#ifndef _SCHEDULE_H
#define _SCHEDULE_H


#include <string>
using namespace std;

struct Node
{
    string courseName;
    int time;
    Node *next;    //forward direction
    Node *prev;    //backward direction

    Node::Node() {}

    Node::Node(const string &cName,const int&time, Node * pRight, Node * pLeft)
        : courseName(cName),time(time),next(pRight), prev(pLeft)
    {}
};

class Schedule
{
public:
    Schedule(); //Constructor

    //adding new course depend on time
    void addCourse(string courseName, char day, int time,Node *Days[6]);

    // delete course depend on time
    void deleteCourse(char day, int time,Node *Days[6]);

    // display a particular course's      time
    void displayCourse(string courseName,Node *Days);

    //prints schedule
    void print(Node *Days);

private:

    Node *head;     //Head node, start of a linked list based on Day
    Node *tail;     //Tail node, end of a linked list based on Day
};

#endif

这是我的实施文件:

#include <iostream>
#include "Schedule.h"

using namespace std;

Schedule::Schedule()
{
    head=new Node(" ",0,NULL,NULL);
    tail = NULL;
}

void Schedule::addCourse(string courseName, char day, int time,Node *Days[6])
{
    int i;

    if (day=='M')
    {i=0;}
    else if(day=='T')
    {i=1;}
    else if(day=='W')
    {i=2;}
    else if(day=='R')
    {i=3;}
    else if(day=='F')
    {i=4;}
    else if(day=='S')
    {i=5;}

    Node*cur=Days[i]->next=head;

    if(Days[i]->next==NULL)
    {
        Days[i]=new Node;
        Days[i]->next->courseName=courseName;
        Days[i]->time=time;
        Days[i]->next=NULL;
        Days[i]->prev=NULL;

        cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
    }

    else if(time<Days[i]->next->time && time!=Days[i]->next->time)
    {
        Node*newcourse=new Node;
        //Days[i]=new Node;
        Days[i]->next->courseName=courseName;
        Days[i]->next->time=time;
        Days[i]->next=head;
        Days[i]->prev=NULL;
        Days[i]->next=newcourse;

        cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
    }

    else if(time>Days[i]->next->time)
    {

        while(Days[i]->next!=NULL && Days[i]->next->time<time && Days[i]->next->time!=time)
        {
            Days[i]->next=Days[i]->next->next;
        }

        if(Days[i]->next->time==time)
        {
            cout<<"Time conflict"<<endl;
        }
        else
        {
            Node*newcourse=new Node;
            Days[i]->next->courseName=courseName;
            Days[i]->next->time=time;
            Days[i]->next=Days[i]->next->next;
            Days[i]->prev=Days[i]->next;
            Days[i]->next->next=newcourse;

            cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
        }
    }
}

void Schedule::deleteCourse(char day, int time,Node *Days[6])
{
    int d;

    if (day=='M')
    {d=1;}
    else if(day=='T')
    {d=1;}
    else if(day=='W')
    {d=2;}
    else if(day=='R')
    {d=3;}
    else if(day=='F')
    {d=4;}
    else if(day=='S')
    {d=5;}

    Node*cur=Days[d]->next=head;

    if(Days[d]->next==NULL)
    {
        cout<<"Schedule is empty for this day"<<endl;
    }
    else
    {
    }
}

void Schedule::displayCourse(string courseName,Node *Days)
{
}

void Schedule::print(Node *Days)
{
}

这是我的主要内容:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Schedule.h"

using namespace std;

Node *Days = new Node[6];

void CoutSelection()
{
    cout<<endl<<endl;

    cout<<"Welcome to Schedule Maker. Please select an option:"<<endl;
    cout<<" 1) Load the course schedule from a known file"<<endl;
    cout<<" 2) Add a time slot manually"<<endl;
    cout<<" 3) Remove a time slot manually"<<endl;
    cout<<" 4) Print a particular course's time slot"<<endl;
    cout<<" 5) Print all schedule"<<endl;
    cout<<" 6) Exit" <<endl;
    cout<<endl;
    cout<<" Please enter your selection as 1-2-3-4-5-6"<<endl;
    cout<<endl;
}

int main()
{
    int selection;
    CoutSelection();
    cin>>selection;
    Schedule list;

    while (selection!=6)
    {
        if (selection==1)
        {   string fileName;
            cout<<"Please enter the filename that you want to load"<<endl;
            cin>>fileName;

            ifstream input;
            input.open(fileName);//open file

            if(!input.is_open())//control if correctly open
            {
                cout<<"Couldn't open input file: "<<fileName<<endl;
            }
            else
            {
                string cname,line; //course name and day identifier
                char day;
                int time; //time
                while(!input.eof())
                {getline(input, line);
                    stringstream ss(line);
                    int num;
                    ss>>cname>>day>>num;

                    list.addCourse(cname,day,time,*Days[6]);
                }
            }
        }

        else if (selection==2)
        {
            int timeAdded;
            string cnameAdded;
            char dayAdded;

            cout<<"Please enter course name,day and it's time that you want to add like   : coursename dayidentifier time"<<endl;
            cout<<"Enter the day as M/T/W/R/F/S. (MONDAY:M, TUESDAY:T, WEDNESDAY:W, THURSDAY:R, FRIDAY:F, SATURDAY:S)"<<endl;
            cin>>cnameAdded>>dayAdded>>timeAdded;

            list.addCourse(cnameAdded,dayAdded,timeAdded,*Days[6]);
        }
        else if(selection==3)
        {
            char dayDeleted;
            int timeDeleted;
            cout<<"Please enter the day and time that you want to delete like : dayidentifider time"<<endl;
            cout<<"Enter the day as M/T/W/R/F/S. (MONDAY:M, TUESDAY:T, WEDNESDAY:W, THURSDAY:R, FRIDAY:F, SATURDAY:S)"<<endl;
            cin>>dayDeleted>>timeDeleted;
            list.deleteCourse(dayDeleted,timeDeleted,*Days[6]);
        }
        else if(selection==4)
        {
            string coursedisplayed;
            cout<<"Please enter course name that you want to display"<<endl;
            cin>>coursedisplayed;

            list.displayCourse(coursedisplayed,*Days);
        }
        else if(selection==5)

        {
            list.print(*Days);
        }

        CoutSelection();
        cin>>selection;

    }
    return 0;

}

我的代码出了什么问题?如果我处理其中一个功能,我相信我可以做其他功能。

错误:

错误C2664:'Schedule :: addCourse':无法将参数4从'Node'转换为'Node * []'

IntelliSense:没有运算符“*”匹配这些操作数             操作数类型是:* Node

1 个答案:

答案 0 :(得分:1)

除了@WhozCraig提出的所有问题,我认为你应该为自己的利益而努力。你的编译器在跟你说话,它告诉你你的addCourse方法收到一个指向节点数组的指针。

但在您的主要内容中,您使用以下list.addCourse(cname,day,time,*Days[6]);来调用它。通过执行*Days[6],您将告诉您要发送Days[6]指向的方法。因此,您的编译器正在接收Node对象,而不是指向节点数组的指针。

使用以下list.addCourse(cname,day,time,Days);进行尝试,这将在几天内将指针发送到第一个元素。

要记住的一个指针,教师可能会注意到这一点:

  1. 你有内存泄漏,这是另一个非常重要的主题。