问题遇到的现象和发生背景
学校的一个项目,没有教员全靠自学,现在已经快要掉头发了
问题相关代码,请勿粘贴截图
template <typename T>
Forward_list<T>::Forward_list(std::initializer_list<T> input)
{
for(auto it = input.begin(); it!= input.end(); it++)
{
Node* new_node = new Node(*it,head_);
new_node->data = *it;
head_=new_node;
size_++;
}
}
运行结果及报错内容
两个都是test报错,代码本身可以运行,但是一定进入教员的设置test function就会出错
a.out: tests.hpp:86: void Tests::test_initializer_list(): Assertion `my_list.front() == real_list.front()' failed.
我的解答思路和尝试过的方法
我现在是一头雾水,本身是这学期刚接触的c++,完全是懵的。
我想要达到的结果
希望能解决问题
看了你的解题有了一点自己的思路,这里把他分享给你
Forward_list<T>::Forward_list(const Forward_list& other)
{
this->head_=NULL;
Node* temp=other.head_;
while(temp!=NULL)
{
Node* pnew=new Node;
pnew->next=NULL;
pnew->data=temp->data;
if(head_==NULL)
head_=pnew;
else
{
Node* prear=head_;
while(prear->next!=NULL)prear=prear->next;
prear->next=pnew;
}
temp=temp->next;
++size_;
}
}
可以参考一下
#include <iostream>
#include <iomanip>
#include <string.h>
#include <cmath>
#include <algorithm>//算法头文件
#include <fstream>
#include <cstdlib>
#include <vector>
#include <sstream>
using namespace std;
//模版类实现单链表
//一个类就是一个节点,头结点
template<typename T>class Node{
protected:
T data;//数据域
Node* next; //指针域
public:
Node(){ init(); }
//初始化
void init();
//头插法
void insertPre(T ele);
//遍历
void print();
//查找元素
int find(T ele);
//删除元素
void delEle(T ele);
//区间删除
void del(T min, T max);
//删除链表中某元素前面的元素
void del3( T ele);
//链表的拷贝
void copy(Node *head);
//创建链表
void createList(T low, T high);
};
//初始化
template<typename T> void Node<T>::init(){
this->data = 0;
this->next = NULL;
}
//头插法
template<typename T> void Node<T>::insertPre(T ele){
Node* cur = new Node;
cur->data = ele;
cur->next = this->next;
this->next = cur;
}
//遍历
template<typename T> void Node<T>::print(){
Node *p = this->next;
while(p!=NULL){
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
//创建链表
template<typename T> void Node<T>::createList(T low, T high){
for(T i=low; i<=high; i++){
this->insertPre(i);
}
}
//查找元素
template<typename T> int Node<T>::find(T ele){
Node* p = this->next;
while(p!=NULL){
if(p->data == ele){
return 1;
}
p = p->next;
}
return -1;
}
//删除元素
template<typename T> void Node<T>::delEle(T ele){
Node* p = this->next;
Node* pr = this;
while(p!=NULL){
if(p->data == ele){
pr->next = p->next;
delete p;
p = p->next;
}else{
p = p->next;
pr = pr->next;
}
}
}
//区间删除
template<typename T> void Node<T>::del(T min, T max){
Node* p = this->next;
Node* pr = this;
while(p!=NULL){
if(p->data>=min && p->data<=max){
pr->next = p->next;
delete p;
p = pr->next;
}else{
p = p->next;
pr = pr->next;
}
}
}
//删除链表中某元素前面的元素
template<typename T> void Node<T>::del3(T ele){
Node* pr2 = this;//当前元素的要删除元素的前驱
Node* pr1 = this->next;//要删除的元素
Node* p = this->next->next;//当前元素
while(p!=NULL){
if(p->data == ele){
pr2->next = p;
delete pr1;
return ;
}else{
p = p->next;
pr1 = pr1->next;
pr2 = pr2->next;
}
}
}
//链表的拷贝
template<typename T> void Node<T>::copy(Node* head){
//一个类调用此方法,所以不用申请头结点的内存空间
Node* pb = this;
Node* pa = head->next;
while(pa!=NULL){
Node* cur = new Node;
cur->data = pa->data;
//尾插法向新表插入元素
cur->next = NULL;
pb->next = cur;
pb = pb->next;
pa = pa->next;
}
}
int main(){
Node<int> head;
head.createList(1,10);
head.print();
cout<<head.find(6)<<endl;
head.del(1,5);
head.print();
head.insertPre(3);head.insertPre(3);
head.print();
head.del3(10);
head.print();
head.delEle(3);
head.print();
Node<int> headb;
headb.copy(&head);
headb.print();
return 0;
}
第一个报错指你的两个链表数据不一致,我看了下你的复制,比如你目前是链表A,要从链表B复制到A,你的B一直next没问题,关键是你的A呢?你每次都是将B节点的数据赋值给A.head_,你的A的next指向什么?所以就导致了你A.head_最后指向的是B的最后一个节点,你觉得这样对吗?
下面的初始化一样的道理,你的A.next指向什么了?链表链表,你要用next指向下一个节点连起来才对.
参考这篇文章
https://blog.csdn.net/qq_42363032/article/details/103795269
把你的代码都贴出来,或者上传网盘,帮你改一下。
#ifndef Linklist_h
#define Linklist_h
#include
template
struct node
{
T date;
node* pNext;
};
template
class Linklist
{
public:
Linklist();
Linklist(const Linklist &list);
Linklist& operator= (const Linklist &rhs);
~Linklist();
public:
void headAdd(const T& date); //在头结点插入
void rearAdd(const T& date); //在尾节点插入
int size() const; //返回链表长度
bool isEmpty() const; //判断是否为空
void print() const; //遍历链表
T getNElement(int n) const; //返回链表第n个节点值
void insertNElement(int n,const T& data);//将元素插入在链表的第pos位置上;
void deleteNElement(int n = 1); //删除第n个节点
void modifyElement(int n, const T& date); //修改n位置的元素值为x;
int find(const T& date); //查找x第一次出现的位置,若没有,返回-1;
void sort(); //对链表元素进行升序排序;
void destroy();//销毁链表,将链表成为空链表;
private:
node* header;
int length;
};
template
Linklist::Linklist() :header(NULL), length(0) {};
template
Linklist::Linklist(const Linklist &list) : header(NULL), length(0)
{
int i = 1;
while (i <= list.size())
{
rearAdd(list.getNElement(i));
i++;
}
}
template
Linklist& Linklist::operator= (const Linklist &rhs)
{
if (this == &rhs)
{
return *this;
}
destroy();
for (int i = 1; i <= rhs.size(); ++i)
{
rearAdd(rhs.getNElement(i));
}
return *this;
}
template
Linklist::~Linklist()
{
destroy();
}
template
void Linklist::headAdd(const T& date)
{
node *pnode = new node;
pnode->date = date;
pnode->pNext = NULL;
if (header == NULL)
{
header = pnode;
}
else
{
pnode->pNext = header;
header = pnode;
}
length++;
}
template
void Linklist::rearAdd(const T& date)
{
node pnode = new node;
pnode->date = date;
pnode->pNext = NULL;
if (header == NULL)
{
header = pnode;
}
else
{
node rear = header;
while (rear->pNext != NULL)
{
rear = rear->pNext;
}
rear->pNext = pnode;
}
length++;
}
template
int Linklist::size() const
{
return length;
}
template
bool Linklist::isEmpty() const
{
return header == NULL;
}
template
void Linklist::print() const
{
node *ptemp = header;
int count = 0;
while (ptemp != NULL)
{
std::cout << ptemp->date << "\t";
ptemp = ptemp->pNext;
count++;
if (count % 5 == 0)
{
std::cout << std::endl;
}
}
std::cout << std::endl;
}
template
T Linklist::getNElement(int n) const
{
if (n < 1 || n > length)
{
throw "获得元素位置非法!";
}
else
{
int i = 1;
node *ptemp = header;
while (i++ < n)
{
ptemp = ptemp->pNext;
}
return ptemp->date;
}
}
template
void Linklist::insertNElement(int n, const T& date)
{
if (n < 1 || n > length)
{
std::cout << "插入元素位置非法!" << std::endl;
}
else
{
if (n == 1)
{
node *ptemp = new node;
ptemp->date = date;
ptemp->pNext = header;
header = ptemp;
}
else
{
int i = 1;
node *ptemp = header;
while (++i < n)
{
ptemp = ptemp->pNext;
}
node *pinsert = new node;
pinsert->date = date;
pinsert->pNext = ptemp->pNext;
ptemp->pNext = pinsert;
}
length++;
}
return;
}
template
void Linklist::deleteNElement(int n = 1)
{
if (n < 1 || n > length)
{
std::cout << "删除元素位置非法!" << std::endl;
}
else
{
node *deleteElement;
if (n == 1)
{
deleteElement = header;
header = header->pNext;
}
else
{
int i = 1;
node *ptemp = header;
while (++i < n)
{
ptemp = ptemp->pNext;
}
deleteElement = ptemp->pNext;
ptemp->pNext = deleteElement->pNext;
}
delete deleteElement;
length--;
}
return;
}
template
void Linklist::modifyElement(int n, const T& date)
{
if (n < 1 || n > length)
{
std::cout << "修改元素位置非法!" << std::endl;
}
else
{
if (n == 1)
{
header->date = date;
}
else
{
node *ptemp = header;
int i = 1;
while (i++ < n)
{
ptemp = ptemp->pNext;
}
ptemp->date = date;
}
}
return;
}
template
int Linklist::find(const T& date)
{
int i = 1;
int re = -1;
node *ptemp = headr;
while (!ptemp)
{
if (ptemp->date == date)
{
re = i;
break;
}
i++;
ptemp = ptemp->pNext;
}
return re;
}
template
void Linklist::sort()
{
if (length > 1)
{
for (int i = length; i > 0; --i)
{
for (int j = 1; j < i; j++)
{
T lift = getNElement(j);
T right = getNElement(j + 1);
if (lift > right)
{
modifyElement(j, right);
modifyElement(j + 1, lift);
}
}
}
}
return;
}
template
void Linklist::destroy()
{
while (header != NULL)
{
node *ptemp = header;
header = header->pNext;
delete ptemp;
}
length = 0;
}
#endif
// 利用typedef struct 定义个结构体,用stu这个别名简化定义名
// 如果要用到链表存放学生个人信息,就需要修改好多部分
//本代码只是用于理解并实践单链表的最基础功能(创建和输出链表)
#include<stdio.h>
#include<stdlib.h>
typedef struct student { //声明结构体
char name[20];
int number;
struct student *next;
}stu;
int count ;
stu* Create(){ //创建链表
stu* Head = NULL;
stu* End,*New;
count = 0;
End = New = (stu*)malloc (sizeof (stu)); //初始化链表的长度
printf("输入数据,以number为0 为结束\n");
scanf("%s",&New->name );
scanf("%d",&New->number);
while(New->number != 0){
count ++; //为if条件做基础
if(count == 1){
New->next = Head;
End = New;
Head = New;
}
else {
New->next = NULL;
End->next = New;
End = New;
}
New = (stu*) malloc ( sizeof ( stu ) ); //再次分配空间
scanf ("%s",& New -> name) ;
scanf ("%d",& New -> number) ;
}
free(New);
return Head;
}
void Print(stu *Head){ //输出函数
stu *temp; //临时变量
int cnt = 1;
temp = Head;
while(temp != NULL){
printf("输出链表第%d个数:\n",cnt);
printf("%s ",temp -> name);
printf("%d ",temp -> number);
printf("\n");
temp = temp->next;
cnt++;
}
}
int main(){
stu *Head;
Head = Create();
Print(Head);
return 0;
}