I'm trying to print all the elements of the list, but I'm getting a wrong output.
我正在尝试打印列表中的所有元素,但是输出错误了。
The code gets 2, 0 and 10, and when I call the procedure "travel_in" it only shows 0, 2.
代码得到2,0和10,当我调用程序“travel_in”时,它只显示0,2。
And have some doubts with my del_start(), it deletes the 0 and not the 2..
并且对我的del_start()有一些疑问,它会删除0而不是2 ..
What I'm doing wrong?
我做错了什么?
Compiled in Windows 64bits with Cygwin
使用Cygwin在Windows 64位中编译
Output
2 0 10 0 2
2 0 10 0 2
Here is the code
# include < iostream >
# include < stdio.h >
using namespace std;
template <class clali>
class double_list
{
protected:
clali node1;
clali *listad;
public:
double_list()//constructor
{
listad=NULL;
}
void insert_strt(clali node1)
{
clali *temp;
temp=new clali;
*temp=node1;
//check if list is not empty
if (listad==NULL)
{
listad=temp;
listad->next=NULL;
listad->before=NULL;
}
else
{
temp->next=listad;
listad->before=temp;
temp->before=NULL;
listad=temp;
}
}
int vertam()
{
int res=0;
clali *temp;
temp=listad;
if (temp==NULL)
{
cout<<"Empty list!"<<endl;
res=0;
}
else
while(temp!=NULL)
{
res++;
temp=temp->next;
}
return res;
}
void insert_mid(clali node1, int pos)
{
int i;
clali *temp,*temp2;
temp2=new clali;
temp=listad;
if(pos<vertam)
{
for(i=1;i<pos;i++)
temp=temp->next;
*temp2=node1;
temp2->next=temp->next;
temp->before=listad;
temp->next=temp2;
}
else
cout<<"Cant show the data!"<<endl;
}
clali del_start()
{
clali a,*temp;
a=*listad;
temp=listad;
listad=listad->next;
delete temp;
return a;
}
void insert_end(clali node1)
{
clali *temp,*temp2;
temp=listad;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp2=new clali;
*temp2=node1;
temp->next=temp2;
temp2->before=temp;
temp2->next=NULL;
}
clali clear_end()
{
clali b,*temp,*temp2;
int j=1;
temp=listad;
do
{
temp=temp->next;
cout<<"Element : "<<j<<endl;
j++;
}while(temp->next!=NULL);
b=*temp;
temp2=temp->before;
temp2->next=NULL;
// delete temp;
return b;
}
void travel_in()
{
clali *temp;
temp=listad;
while(temp->next!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
};
struct integer
{
int data;
integer*next,*before;
};
typedef struct integer Integer;
int main()
{
Integer node;
node.next=NULL;
node.before=NULL;
double_list<Integer> test_list;
test_list.insert_strt(node);
node.data=2;
cout<<node.data<<endl;
test_list.insert_end(node);
node.data=0;
cout<<node.data<<endl;
test_list.insert_end(node);
node.data=10;
cout<<node.data<<endl;
test_list.del_start();
test_list.travel_in();
}
2 个解决方案
#1
0
I see at least one obvious bug. Initial analysis indicates that the listad
class member is the pointer to the first element in the doubly-linked list. In that case, the following is obviously wrong (reformated for legibility, please indent your code correctly):
我看到至少一个明显的错误。初始分析表明listad类成员是指向双向链表中第一个元素的指针。在这种情况下,以下显然是错误的(为了易读性而重新格式化,请正确缩进代码):
void insert_mid(clali node1, int pos)
{
int i;
clali *temp,*temp2;
temp2=new clali;
The purpose of this class method is, apparently, to insert the new node in the middle of the linked list.
显然,此类方法的目的是将新节点插入链接列表的中间。
temp2
is the new node.
temp2是新节点。
temp=listad;
if(pos<vertam)
{
for(i=1;i<pos;i++)
temp=temp->next;
temp
appears to be the insert position in the middle of the list.
temp似乎是列表中间的插入位置。
temp->before=listad;
For some unclear reason this code attempts to set the before
pointer of an existing node in the middle of the list to the head of the list. This makes no sense, and is wrong.
由于某些不明原因,此代码尝试将列表中间的现有节点的before指针设置为列表的头部。这没有任何意义,也是错误的。
#2
0
Let's go step by step in main()
.
让我们一步一步地在main()中。
When you first call insert_strt()
the argument node
has garbage value for member data
. So an Integer
object with some garbage value for data
gets inserted at the start of test_list
. Then you insert Integer
objects with data
2 and 0, respectively, at the end of the list.
当您第一次调用insert_strt()时,参数节点具有成员数据的垃圾值。因此,在test_list的开头插入一个带有一些垃圾值的Integer对象。然后分别在列表末尾插入数据2和0的Integer对象。
Later, you delete the first clali
object from test_list
which deletes the object with garbage value in its data
field. So, after deletion, you have objects with data
value 2, and 0 in the list in that order.
稍后,从test_list中删除第一个clali对象,该对象在其数据字段中删除具有垃圾值的对象。因此,删除后,您将拥有数据值为2的对象,并且列表中的顺序为0。
At the end, you print the list with travel_in()
but it does not do what you think it does. What it is actually doing is that if the list has at least one element then it prints all but the last element in the list. If the list is empty, it will cause a segmentation fault (in the condition of while
loop as temp
would be NULL
). So it will print: 2 (but your list has 2 and 0).
最后,您使用travel_in()打印列表,但它不会按照您的想法执行。它实际上做的是,如果列表至少有一个元素,那么它将打印列表中除最后一个元素之外的所有元素。如果列表为空,则会导致分段错误(在while循环的情况下,temp将为NULL)。因此它将打印:2(但您的列表有2和0)。
You can write travel_in()
as follows.
您可以按如下方式编写travel_in()。
void travel_in()
{
clali *temp = listad;
while(temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
By the way, comment/remove the cout
statements in the main()
function. They may confuse you.
顺便说一句,注释/删除main()函数中的cout语句。他们可能会让你感到困惑。
#1
0
I see at least one obvious bug. Initial analysis indicates that the listad
class member is the pointer to the first element in the doubly-linked list. In that case, the following is obviously wrong (reformated for legibility, please indent your code correctly):
我看到至少一个明显的错误。初始分析表明listad类成员是指向双向链表中第一个元素的指针。在这种情况下,以下显然是错误的(为了易读性而重新格式化,请正确缩进代码):
void insert_mid(clali node1, int pos)
{
int i;
clali *temp,*temp2;
temp2=new clali;
The purpose of this class method is, apparently, to insert the new node in the middle of the linked list.
显然,此类方法的目的是将新节点插入链接列表的中间。
temp2
is the new node.
temp2是新节点。
temp=listad;
if(pos<vertam)
{
for(i=1;i<pos;i++)
temp=temp->next;
temp
appears to be the insert position in the middle of the list.
temp似乎是列表中间的插入位置。
temp->before=listad;
For some unclear reason this code attempts to set the before
pointer of an existing node in the middle of the list to the head of the list. This makes no sense, and is wrong.
由于某些不明原因,此代码尝试将列表中间的现有节点的before指针设置为列表的头部。这没有任何意义,也是错误的。
#2
0
Let's go step by step in main()
.
让我们一步一步地在main()中。
When you first call insert_strt()
the argument node
has garbage value for member data
. So an Integer
object with some garbage value for data
gets inserted at the start of test_list
. Then you insert Integer
objects with data
2 and 0, respectively, at the end of the list.
当您第一次调用insert_strt()时,参数节点具有成员数据的垃圾值。因此,在test_list的开头插入一个带有一些垃圾值的Integer对象。然后分别在列表末尾插入数据2和0的Integer对象。
Later, you delete the first clali
object from test_list
which deletes the object with garbage value in its data
field. So, after deletion, you have objects with data
value 2, and 0 in the list in that order.
稍后,从test_list中删除第一个clali对象,该对象在其数据字段中删除具有垃圾值的对象。因此,删除后,您将拥有数据值为2的对象,并且列表中的顺序为0。
At the end, you print the list with travel_in()
but it does not do what you think it does. What it is actually doing is that if the list has at least one element then it prints all but the last element in the list. If the list is empty, it will cause a segmentation fault (in the condition of while
loop as temp
would be NULL
). So it will print: 2 (but your list has 2 and 0).
最后,您使用travel_in()打印列表,但它不会按照您的想法执行。它实际上做的是,如果列表至少有一个元素,那么它将打印列表中除最后一个元素之外的所有元素。如果列表为空,则会导致分段错误(在while循环的情况下,temp将为NULL)。因此它将打印:2(但您的列表有2和0)。
You can write travel_in()
as follows.
您可以按如下方式编写travel_in()。
void travel_in()
{
clali *temp = listad;
while(temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
By the way, comment/remove the cout
statements in the main()
function. They may confuse you.
顺便说一句,注释/删除main()函数中的cout语句。他们可能会让你感到困惑。