C++ 如何使用动态数组的双链表?
我的家庭作业是关于用双链表制定时间表。我们可以创建一个动态数组来保存天数。但每天都必须有一个包含时间段的双链接列表。禁止使用向量和数组代替链表。我对函数有困难 这是我的头文件:C++ 如何使用动态数组的双链表?,c++,arrays,C++,Arrays,我的家庭作业是关于用双链表制定时间表。我们可以创建一个动态数组来保存天数。但每天都必须有一个包含时间段的双链接列表。禁止使用向量和数组代替链表。我对函数有困难 这是我的头文件: #ifndef _SCHEDULE_H #define _SCHEDULE_H #include <string> using namespace std; struct Node { string courseName; int time; Node *next; //f
#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
\ifndef\u时间表
#定义计划
#包括
使用名称空间std;
结构体类型
{
字符串courseName;
整数时间;
Node*next;//前进方向
Node*prev;//向后方向
Node::Node(){}
Node::Node(常量字符串和cName、常量整数和时间、Node*pRight、Node*pLeft)
:courseName(cName)、time(time)、next(pRight)、prev(pLeft)
{}
};
课程表
{
公众:
Schedule();//构造函数
//增加新课程取决于时间
void addCourse(字符串courseName、char-day、int-time、Node*Days[6]);
//根据时间删除课程
void deleteCourse(字符天、整数时间、节点*天[6]);
//显示特定课程的时间
void displayCourse(字符串courseName,节点*天);
//打印时间表
作废打印(节点*天);
私人:
Node*head;//head节点,基于日期的链接列表的开始
Node*tail;//tail节点,基于日期的链接列表的结尾
};
#恩迪夫
这是我的实现文件:
#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)
{
}
#包括
#包括“附表h”
使用名称空间std;
日程安排::日程安排()
{
head=新节点(“”,0,NULL,NULL);
tail=NULL;
}
void Schedule::addCourse(字符串courseName、char-day、int-time、节点*天[6])
{
int i;
如果(天=='M')
{i=0;}
否则,如果(天=='T')
{i=1;}
否则,如果(天=='W')
{i=2;}
否则,如果(天=='R')
{i=3;}
否则,如果(天=='F')
{i=4;}
否则如果(天=='S')
{i=5;}
节点*cur=Days[i]->next=head;
如果(天[i]>next==NULL)
{
天[i]=新节点;
天[i]->next->courseName=courseName;
天[i]->时间=时间;
天[i]->next=NULL;
天[i]>prev=NULL;
coutnext->next=newcourse;
cout除了@WhozCraig提出的所有问题之外,我认为您应该为自己的利益解决这些问题。您的编译器正在与您交谈,它告诉您addCourse方法接收到指向节点数组的指针
但是在你的主要代码中,你用下面的list.addCourse(cname,day,time,*Days[6]);
调用它。通过*Days[6]
你告诉了你想要发送Days[6]
指向的方法。因此,你的编译器接收的是一个节点
对象,而不是一个指向节点数组的指针
使用以下列表进行尝试。addCourse(cname,day,time,Days);
,这将在天内发送指向第一个元素的指针
要记住一点,老师可能会注意到:
内存泄漏是另一个非常重要的问题
你在问什么?它有什么问题?它没有编译吗?或者它编译了,但是没有运行到完成?或者它运行了,但是产生了错误的输出吗?请添加细节和你看到的任何错误消息。但是你使用的数组显然违反了分配规则。这:if(Days[i]->next->time==time)
如果以下情况不会有好的结局:Days[i]->next!=NULL
是先前循环退出时的原因。您的代码可以而且可能会调用未定义的行为。我想问题是将数组传递给函数IntelliSense:no operator“*”匹配这些操作数操作数类型为:*节点您遇到的问题远不止这些。您的Days
数组是全局的,动态分配并调用默认的节点()
在创建新节点时为每个新节点创建构造函数。但是,你的构造函数从未初始化其任何成员。而且你的sentinel节点没有任何帮助,事实上最终是无用的。老实说,我很难相信,在集成到你的项目之前,如果不测试至少部分代码隔离,就可以编写这么多代码.他在问题中明确指出,在项目中禁止使用向量(可能是因为作业的重点是处理链表,因此也就是指针).哦,很抱歉,是的,我曾经有过类似的作业。啊,我在其他帖子中对类似的作业进行了评论。我可以理解老师试图强迫你学习某些概念的地方,但通常情况下,这只会导致坏的编程习惯,而不是其他任何东西。我同意你的看法,我有很多assi像这样的事情也是如此。
#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;
}