首页 新闻 会员 周边

c版本的list,哪里出了问题了

0
悬赏园豆:5 [待解决问题]

#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct node{
node(int x):data(x){}
int data;
struct node* next;
};

bool insert(node **root,int i,int x){
node *tmp = *root;
if(tmp == NULL || i == 0){
if(tmp == NULL){
tmp = new node(x);
tmp->next= NULL;
*root = tmp;
}else{
tmp = new node(x);
tmp->next = *root;
*root = tmp;
}
}else{
int j=0;
while(tmp && j < i-1){
j ++;
tmp = tmp->next;
}
if(tmp == NULL){
cout <<"insert out of range "<< endl;
return false;
}
node *p1 = new node(x);
if(tmp->next == NULL){
tmp->next = p1;
p1->next = NULL;
}else{
p1->next = tmp->next;
tmp->next = p1;
}
}
return true;
}

bool remove_pos(node **root,int i){
int j=0;
node *tmp = *root;
if(tmp == NULL || i <0 ){
return false;
}else{
while(tmp && j < i -1){
tmp = tmp->next;
j ++;
}
if(tmp == NULL || tmp->next == NULL){
cout << "remove out of range" << endl;
return false;
}
//tmp->next is x
if(i == 0){
*root = tmp->next;
tmp->next = NULL;
delete tmp;
return true;
}else{
node *t = tmp->next;
tmp->next = t->next;
t->next = NULL;
delete t;
return true;
}
}

}

bool remove_value(node **root,int x){
int j=0;
node *tmp = *root;
if(tmp == NULL){
cout << "list is empty " << endl;
return false;
}
node *t= NULL;
while(tmp){
if(tmp->data == x){
break;
}
t = tmp;
tmp = tmp->next;
}
if(tmp == NULL){
cout << "x not in the list" << endl;
return false;
}else{
if(t == NULL){
delete tmp;
}else{
t->next = tmp->next;
delete tmp;
}
}
return true;
}

int get_size(node **root){
int i=0;
node *tmp = *root;
while(tmp){
i++;
tmp = tmp->next;
}
return i;
}


void print(node **root){
node *tmp= *root;
int i=0;
while(tmp){
i ++;
cout << setw(5) << tmp->data;
if(i % 10 == 0){
cout << endl;
}
tmp = tmp->next;
}
}

 

void test1(){
node *root;
srand(NULL);
int all[100];
#if 1
cout << "init list" << endl << endl;

for(int i=0;i < 100;i ++){
all[i] = rand() % 1987;
insert(&root,0,all[i]);
}
#endif

#if 1
cout << "print all" << endl<< endl;
print(&root);
#endif

#if 1
cout << "print size"<< endl;
cout << get_size(&root)<< endl;;
#endif

#if 0
cout << "remove_value" << endl << endl;
for(int i=0;i < 100;i ++){
remove_value(&root,all[i]);
}
#endif

#if 0
cout << "remove_pos" << endl << endl;
for(int i=0;i < 100;i ++){
cout << remove_pos(&root,0)<< endl;
}
#endif

}

 

int main(){

  test1();

 

return 0;

}

 

问题1 如果只调用print 和get_size,则没有问题,如果不调用print,get_size,单独调用remove_pos或者remove_value,都没有问题,但是只要前面调用了print或者get_size,则后面调用remove_pos或者remove_value,就会出现问题,请问是是什么原因导致的呢????

demps_c的主页 demps_c | 初学一级 | 园豆:128
提问于:2014-01-04 13:48
< >
分享
所有回答(1)
0

好吧,很久没搞C了,大致看了下,结合你说的现象,应该是在print或get_size中的node *tmp = *root;tmp = tmp->next;这二句已经把node的当前指向移到最后了.你试下在print或get_size中把指针再移到第一个元素.

天天不在 | 园豆:926 (小虾三级) | 2014-01-04 17:34

真是非常感谢你,我找出问题所在了,

void test1(){
node *root = NULL;
srand(NULL);

因为root没有被初始化,直接被使用了,所以才会导致后面的问题,用gdb分析了一个下午,

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2014-01-04 18:13

@demps_c: 使用未初始化的内存,害死人害死自己,魔鬼在细节当中。。。

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2014-01-04 18:14

因为,root没有被初始化为NULL,所以导致后面分析的时候get_size等于了102,还有print也是导致大于了100的值,然后我就估计是root没有初始化,导致root的值在第一个时候不为NULL,导致了后面的问题,。。。。当真魔鬼在细节中。。。。,自己要好好学习西方人的细节精神。。

支持(0) 反对(0) demps_c | 园豆:128 (初学一级) | 2014-01-04 18:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册