【发布时间】:2014-01-21 19:16:38
【问题描述】:
我在 C++ 中相当生疏,我正在尝试实现一个双链表,但我遇到了一些读取违规并给出了一些奇怪的值。
#include <iostream>
struct Node
{
int val;
Node* next;
Node* prev;
};
class linkedList
{
public:
linkedList(); //constructor
~linkedList(); //destructor
void push_back(int x);
void addtofront(int x);
//void deleteNode(int x);
bool isempty();
void firstelem();
void prnt_tail();
/*void insert_after(int x, int y);
void insert_before(int x, int y);*/
private:
Node* head;
Node* next;
Node* prev;
};
linkedList::linkedList(){};
linkedList::~linkedList(){};
void linkedList::push_back(int x)
{
linkedList* list = this;
Node temp;
temp.val=x;
temp.next=NULL;
temp.prev=NULL;
if (!head)
{
linkedList* list = new linkedList();
list->head;
head = new Node;
head->val = x;
head->next = NULL;
head->prev = NULL;
}
else
{
Node* temp1;
temp1=head;
while (temp1->next!=NULL)
{
temp1 = temp1->next;
}
temp.next= NULL;
temp.prev=temp1;
temp.val = x;
}
};
void linkedList::addtofront(int x)
{
linkedList* list = this;
Node temp;
temp.val=x;
temp.next=NULL;
temp.prev=NULL;
if (!head)
{
linkedList* list = new linkedList();
list->head;
head = new Node;
head->val = x;
head->next = NULL;
head->prev = NULL;
}
else
{
list->head->prev=&temp;
temp.next=head;
head=&temp;
}
};
//void linkedList::deleteNode(int x)
//{
// if(head)
// {
// linkedList *ptr = head;
// while(ptr->node.val != x)
// {
// ptr = ptr->node.next;
// }
// (ptr->node.next)->prev=ptr->node.prev;
// ptr->node.prev=ptr->node.next;
// delete ptr;
// }
// else
// std::cout<<"empty list";
//}
bool linkedList::isempty()
{
if(head)
return false;
else return true;
};
void linkedList::firstelem()
{
std::cout<<head->val;
};
void linkedList::prnt_tail()
{
if(head)
{
Node *temp;
temp=head;
temp=head->next;
std::cout<<temp;
while(temp->next!=NULL)
{
std::cout<<temp->val<<" ";
}
std::cout<<temp->val;
}
else
{
std::cout<<"empty list";
}
};
//linkedList::insert_after(int x, int y)
//{
//
//}
//
//linkedList::insert_before(int x, int y)
//{
//
//}
和我的主要
#include "linkedlist2.h"
#include <stdlib.h>
#include <iostream>
int main()
{
linkedList example;
if(example.isempty())
std::cout<<"this list is empty "<<"\n";
else
std::cout<<"this list is not empty"<<"\n";
for (int i = 1; i<=20; i++)
{
example.push_back(i);
//example.prnt_tail();
}
example.addtofront(25);
example.firstelem();
std::cout<<"\n";
example.addtofront(28);
example.firstelem();
std::cout<<"\n";
if(example.isempty())
std::cout<<"this list is empty "<<"\n";
else
std::cout<<"this list is not empty"<<"\n";
//example.push_back(26);
//std::cout<<example.head->next->val;
example.firstelem();
std::cout<<"\n";
example.prnt_tail();
std::cout<<"\n";
system("pause");
}
当我运行 main 我得到
此列表为空
-858993460
-858993460
此列表不为空
-858993460
CCCCCCCC
我也得到了错误
访问冲突读取位置0xCCCCCCD0。
接下来要执行的语句是“void linkedList::prnt_tail()”中的while循环
我很确定我的问题在于我的指针等等。就像我说的那样,我真的很生疏,所以如果你能提供任何帮助,我们将不胜感激,即使是在与我的问题没有直接关系的事情上。
【问题讨论】:
-
不要重新发明轮子。使用
std::list;如果您想学习,请使用调试器了解指针的值。 -
@BasileStarynkevitch 不能做一个练习来提高他们的技能吗?
-
addToFront中有 2 个奇怪的东西。第一个是您正在创建linkedList的新实例,而您可以执行this->head = head。第二个是你在函数之外使用temp,但它的作用域是函数addToFront。 -
你甚至没有初始化成员。完成此操作后,手动单步执行代码。不要使用调试器,而是在编写之前三思而后行。 (那里有足够的问题可以写一篇文章。)
-
0xCCCCCCD0 看起来您正在使用未初始化的内存。 stackoverflow.com/questions/370195/…
标签: c++ pointers linked-list