首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

大哥小弟我的循环双向链表出的有关问题在哪?多谢

2012-02-12 
请教各位大哥我的循环双向链表出的问题在哪?谢谢!#include iostreamusingnamespacestd//#include list

请教各位大哥我的循环双向链表出的问题在哪?谢谢!
#include <iostream>
using   namespace   std;
//#include "list.h "
#include <stdlib.h>
template <class   type>
class   list;
template <class   type>
class   nodetype
{
    friend   class   list <type> ;
private:
    type   info;
    nodetype <type> *next;
    nodetype <type> *back;
public:
    nodetype(){next=NULL;back=NULL;}
    nodetype(const   type&   item,nodetype <type> *n,nodetype <type> *b){info=item;next=n;back=b;}
    void   setnode(nodetype <type> *n,nodetype <type> *b){next=n;back=b;}
  ~nodetype(void){}
};

template <class   type>
class   list
{
    private:
          int   size;
          nodetype <type> *head;
    public:
        list(void)
{
size=0;head=NULL;
}

      ~list(void)
      {
      if(size==0)
      exit(0);
      nodetype <type> *p=head;
      while(p-> next!=head)
      {
      nodetype <type> *q=p;p=p-> next;delete   q;
      }
      delete   p;
      head=NULL;
      size=0;
      }

      int   itsize()
      {
      return   size;
      }

      nodetype <type> *index(int   i)
      {
      if(i==1)return   head;
      nodetype <type> *p=head;
      int   j=1;
      while(j <i)
      {
      p=p-> next;
      j++;
      }
      return   p;
      }

      int   insert(const   type&   item,int   i)
      {      
                      if(size==0){nodetype <type> *A=new   nodetype <type> (item,NULL,NULL);head=A;A-> setnode(head,head);return   0;}
      if(i==1)
      {
      nodetype <type> *p=head;
      nodetype <type> *q=p-> back;
      nodetype <type> *A=new   nodetype <type> (item,p,q);
      head=A;
      p-> back=A;
      q-> next=A;
      size++;return   0;
      }
      else
      {
      nodetype <type> *p=index(i-1);
      nodetype <type> *q=p-> next;
      nodetype <type> *A=new   nodetype <type> (item,q,p);
      p-> next=A;
      q-> back=A;
      size++;return   0;
      }


      }

      void   delte(int   i)
      {
      if(i==1)
      {
      nodetype <type> *p=head;
      nodetype <type> *q=head-> back;
      nodetype <type> *A=p-> next;
      q-> next=A;
      A-> back=q;
      head=A;
      delete   p;
      size--;
      }
      else   ;
              nodetype <type> *   p=index(i-1);
              nodetype <type> *   A=p-> next;
              nodetype <type> *q=A-> next;
              p-> next=q;
              q-> back=p;
              delete   A;
              size--;
      }

      type   getinfo(int   i)
      {
      nodetype <type> *p=index(i);
      return   p-> info;
      }
};

//using   namespace   std;
void   main()
{
list <int>   my;
int   s[]={0,1,2,3,4,5,6,7,8,9,10},n=4;
int   temp;
for(int   i=1;i <n;i++)
my.insert(s[i],i);
//my.delte(2);
for(i=1;i <=my.itsize();i++)
{
temp=my.getinfo(i);
cout < <temp < < "   ";
}
}

[解决办法]
#include <iostream>
using namespace std;
//#include "list.h "
#include <stdlib.h>
template <class type>
class list;
template <class type>
class nodetype
{
friend class list <type> ;
private:
type info;
nodetype <type> *next;
nodetype <type> *back;
public:
nodetype(){next=NULL;back=NULL;}
nodetype(const type& item,nodetype <type> *n,nodetype <type> *b){info=item;next=n;back=b;}
void setnode(nodetype <type> *n,nodetype <type> *b){next=n;back=b;}
~nodetype(void){}
};

template <class type>
class list
{
private:
int size;
nodetype <type> *head;
public:
list(void)
{
size=0;head=NULL;
}

~list(void)
{
if(size==0)
exit(0);
nodetype <type> *p=head;
while(p-> next!=head)
{
nodetype <type> *q=p;p=p-> next;delete q;
}
delete p;
head=NULL;
size=0;
}

int itsize()
{
return size;
}

nodetype <type> *index(int i)
{
if(i==1)return head;
nodetype <type> *p=head;
int j=1;
while(j <i)
{
p=p-> next;
j++;
}
return p;
}

int insert(const type& item,int i)
{
if(size==0){nodetype <type> *A=new nodetype <type> (item,NULL,NULL);head=A;A-> setnode(head,head);return 0;}
if(i==1)
{
nodetype <type> *p=head;


nodetype <type> *q=p-> back;
nodetype <type> *A=new nodetype <type> (item,p,q);
head=A;
p-> back=A;
q-> next=A;
size++;return 0;
}
else
{
nodetype <type> *p=index(i-1);
nodetype <type> *q=p-> next;
nodetype <type> *A=new nodetype <type> (item,q,p);
p-> next=A;
q-> back=A;
size++;return 0;
}
}

void delte(int i)
{
if(i==1)
{
nodetype <type> *p=head;
nodetype <type> *q=head-> back;
nodetype <type> *A=p-> next;
q-> next=A;
A-> back=q;
head=A;
delete p;
size--;
}
else ;
nodetype <type> * p=index(i-1);
nodetype <type> * A=p-> next;
nodetype <type> *q=A-> next;
p-> next=q;
q-> back=p;
delete A;
size--;
}

type getinfo(int i)
{
nodetype <type> *p=index(i);
return p-> info;
}
};

//using namespace std;
int main()
{
list <int> my;
int s[]={0,1,2,3,4,5,6,7,8,9,10},n=4;
int temp;
for(int i=1;i <n;i++)
my.insert(s[i],i);
//my.delte(2);
for(int i=1;i <=my.itsize();i++) //此处有问题,问题在下面描述
{
temp=my.getinfo(i);
cout < <temp < < " ";
}
return 0;
}

//i在上一个循环的域里面,属于一个局部变量,当出了那个循环,i就不存在了,所以在下面用

到i时,还要重新声明

编译已经通过,至于能否正常执行,那就是你的算法问题了,嘿嘿

[解决办法]
重定义了i
[解决办法]
晕倒,楼上你恰恰说反了,不是重定义了,是在上一个for循环中定义了,在下一个for循环中仍

然引用,缺少定义。

嘿嘿,是不没仔细看? o(∩_∩)o
[解决办法]
这里insert函数
int insert(const type& item,int i)
{
if (size == 0)
{
nodetype <type> * A = new nodetype <type> (item,NULL,NULL);
head=A;
A-> setnode(head, head);
size++; // size要加1,否则为insert函数每次都会执行这里
return 0;
}
// ....
}

热点排行