免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2074 | 回复: 8

这样创建双链表的问题所在 [复制链接]

论坛徽章:
0
发表于 2004-03-16 19:40 |显示全部楼层

程序如下:
   dlinklist *CREATLIST()
  {
   char ch;
   head=NULL;
   p=head;
   printf("\nPLEASE ENTER CHAIN BELTPRESS '*' TO EXIT)\n";
   ch=getchar();
   while(ch!='*')
     {
      s=(dlinklist *)malloc(LEN);
      s->;data=ch;
      p->;next=s;
      s->;prior=p;
      p=s;
      ch=getchar();
     }
   head->;prior=p;
   p->;next=head;/*-------------------将尾接点连到头接点---------------*/
   printf("LEASE PRESS ANYKEY TO CONTINUE";
   getch();
   return(head);
  }
dlinklist *GET(dlinklist *p,int x)/*X为输入想要找到n个结点*/
  {
   int i=1;
   while(p->;next!=NULL)
     {
      printf("THE %d GET IS %c\n\n",i,p->;data);/*---------------1------*/
      p=p->;next;
      printf("THE %d GET IS %c\n\n",i,p->;data);/*---------------2------*/
      if(i==x)
        {
         printf("THE %d GET IS %c\n\n",x,p->;data);
         return(p);
        }
      i++;
     }
   printf("THERE ISN'T THE GET IN THE LIST\n";
   return NULL;
  }
1 2输出的为什么会多出一行的呢?
而且为什么有些值会是NULL的呢?
怎么样可以做到找到想要找到的结点,呼~~~
  请各位知道的说说看

论坛徽章:
0
发表于 2004-03-16 19:46 |显示全部楼层

这样创建双链表的问题所在

“1 2输出的为什么会多出一行的呢?”意思就是为什么会多出一个结点或者怎么会循环多一次的呢?不是p->;next吗?
        请多多指教

论坛徽章:
0
发表于 2004-03-16 22:27 |显示全部楼层

这样创建双链表的问题所在

原帖由 "senfar" 发表:

程序如下:
   dlinklist *CREATLIST()
{
char ch;
head=NULL;
p=head;
printf("\nPLEASE ENTER CHAIN BELTPRESS '*' TO EXIT)\n";
ch=getchar();
while(ch!='*')
{
s=(dlinklist *)malloc(LEN);
s->;data=ch;
p->;next=s;
s->;prior=p;
p=s;
ch=getchar();
}
head->;prior=p;
p->;next=head;/*-------------------将尾接点连到头接点---------------*/
printf("LEASE PRESS ANYKEY TO CONTINUE";
getch();
return(head);
}


兄弟,你的程序有问题吧,head 指向哪里啊?

论坛徽章:
0
发表于 2004-03-17 13:55 |显示全部楼层

这样创建双链表的问题所在

[那个LEN是sizeof(dlinklist)]
  head有指向的吗?您是说它的头还是尾指向呢?
  但是问题可能就出在这里啦
  我想这样做的,但是输出的就是有问题
 您认为怎么样呢?而我原因出在那里呢?
 写出来最好了,呵呵

论坛徽章:
0
发表于 2004-03-17 14:20 |显示全部楼层

这样创建双链表的问题所在

你的head有问题,head的值一直是NULL,所以你可以想象一下你的这个双向链表
可以这样改一下,

head = malloc(LEN);
head ->;ch = ...   ;   //读入第一个字符
head ->;next = NULL:
head ->;pre = NULL;

p = head;     // 这样后面的代码差不多都不用改了

论坛徽章:
0
发表于 2004-03-17 15:48 |显示全部楼层

这样创建双链表的问题所在

是啊,head是一直是NULL的,它是作为整个循环双链表的一个标志
问题是输出的时候指针不听话

论坛徽章:
0
发表于 2004-03-17 15:51 |显示全部楼层

这样创建双链表的问题所在

#ifndef _DOUBLELINKEDLISTTEP_2004_01_16_XHL
#define _DOUBLEINKEDLISTTEP_2004_01_16_XHL
template <class T>;
struct nodeType
{
        T info;
        nodeType<T>; *next;
        nodeType<T>; *back;
};
////define DoubleLinkedListTep template/////////////////////////////////////////////////////
template<class T>;
class DoubleLinkedListTep
{
public:
        DoubleLinkedListTep();
    DoubleLinkedListTep(const DoubleLinkedListTep<T>;& otherList);
        const DoubleLinkedListTep<T>;& operator=(const DoubleLinkedListTep<T>;& otherList);
    ~DoubleLinkedListTep();
   
    void InitializeList();
    bool IsEmptyList();
    void Destroy();

    void Print();
    int Length();
    void Search(const T& searchItem);
    void InsertNode(const T& insertItem);
    void DeleteNode(const T& deleteItem);
   
private:
        nodeType<T>; *first;
};
////implication DoubleLinkedListTep template/////////////////////////////////////////////////////////
template<class T>;
DoubleLinkedListTep<T>;:oubleLinkedListTep()
{
        first=NULL;
}
template<class T>;
DoubleLinkedListTep<T>;::~DoubleLinkedListTep()
{
   nodeType<T>; *temp;
        while(first!=NULL)
        {
                temp=first;
                first=first->;next;
                delete temp;
       
        }

}
template<class T>;
DoubleLinkedListTep<T>;:oubleLinkedListTep(const DoubleLinkedListTep<T>;& otherList)
{
        nodeType<T>; *newNode;
        nodeType<T>; *first_move;
        nodeType<T>; *current;
        if(otherList.first==NULL){
                first=NULL;
        }
        else
                {
                        current=otherList.first;
                        first=new nodeType<T>;;
                        first->;info=current->;info;
                        first->;next=NULL;
                        first->;back=NULL;
       
                        first_move=first;
            current=current->;next;

                        while(current!=NULL)
                        {
                                newNode=new nodeType<T>;;
                                newNode->;info=current->;info;
                                newNode->;next=NULL;
                                newNode->;back=first_move;
                                first_move->;next=newNode;
                               
                                first_move=first_move->;next;
                                current=current->;next;       
                        }
                }

}
template<class T>;
const DoubleLinkedListTep<T>;& DoubleLinkedListTep<T>;:perator=(const DoubleLinkedListTep<T>;& otherList)
{
    nodeType<T>; *newNode;
        nodeType<T>; *first_move;
        nodeType<T>; *current;
        if(this!=&otherList)
        {
                if(first!=NULL)
                        Destroy();
                if(otherList.first==NULL)
                {
                        first=NULL;
                }
                else
                {
                        current=otherList.first;
                        first=new nodeType<T>;;
                        first->;info=current->;info;
                        first->;next=NULL;
                        first->;back=NULL;

                        first_move=first;

                        current=current->;next;

                        while(current!=NULL)
                        {
                                newNode=new nodeType<T>;;
                                newNode->;info=current->;info;
                                newNode->;next=NULL;
                                newNode->;back=first_move;
                                first_move->;next=newNode;
                               
                                first_move=first_move->;next;
                                current=current->;next;       
                        }
                }
        }
        return *this;
}

template<class T>;
bool DoubleLinkedListTep<T>;::IsEmptyList()
{
        return(first==NULL);
}
template<class T>;
void DoubleLinkedListTep<T>;:estroy()
{
        nodeType<T>; *temp;
        while(first!=NULL)
        {
                temp=first;
                first=first->;next;
                delete temp;
        }
}
template<class T>;
void DoubleLinkedListTep<T>;::InitializeList()
{
        Destroy();
}
template<class T>;
int DoubleLinkedListTep<T>;::Length()
{
        int length=0;
        nodeType<T>; *current;
        current=first;
        while(current!=NULL)
        {
                length++;
                current=current->;next;
        }
        return length;
}
template<class T>;
void DoubleLinkedListTep<T>;:rint()
{
        nodeType<T>; *current;
        current=first;
        while(current!=NULL)
        {   
                cout<<current->;info<<" ";
                current=current->;next;
        }
}
template<class T>;
void DoubleLinkedListTep<T>;::Search(const T& searchItem)
{
        bool found;
        nodeType<T>; *current;
        if(first==NULL)
                cout<<"Cannot search an empty list"<<endl;
        else
        {
                found=false;
                current=first;
                while(current!=NULL&&!found)
                        if(current->;info>;=searchItem)
                                found=true;
                        else
                                current=current->;next;
                       
                        if(current==NULL)
                                cout<<"Item not in the list ."<<endl;
                        else
                                if(current->;info==searchItem)
                                        cout<<"Item is found in the list"<<endl;
                                else
                                        cout<<"Item not in the list."<<endl;
                                }
}
template<class T>;
void DoubleLinkedListTep<T>;::InsertNode(const T& insertItem)
{
        nodeType<T>; * current;
        nodeType<T>; * trailCurrent;
        nodeType<T>; *newNode;
        bool found;

        newNode=new nodeType<T>;;
        newNode->;info=insertItem;
        newNode->;next=NULL;
        newNode->;back=NULL;

        if(first==NULL)
                first=newNode;
        else
        {
                found=false;
                current=first;
               
                while(current!=NULL&&!found)
                        if(current->;info>;=insertItem)
                                found=true;
                        else
                        {
                                trailCurrent=current;
                                current=current->;next;
                        }

                        if(current==first)
                        {
                                first->;back=newNode;
                                newNode->;next=first;
                                first=newNode;
                        }
                        else
                        {
                                if(current!=NULL)
                                {
                                        trailCurrent->;next=newNode;
                                        newNode->;back=trailCurrent;
                                        newNode->;next=current;
                                        current->;back=newNode;
                                }
                                else
                                {
                                        trailCurrent->;next=newNode;
                                        newNode->;back=trailCurrent;
                                }
                        }
        }
}
template<class T>;
void DoubleLinkedListTep<T>;:eleteNode(const T& deleteItem)
{
        nodeType<T>; * current;
        nodeType<T>; *trailCurrent;
        bool found;

        if(first==NULL)
                cout<<"Cannot delete from a empty list."<<endl;
        else
                if(first->;info==deleteItem)
                {
                        current=first;
                        first=first->;next;

                        if(first!=NULL)
                                first->;back=NULL;
                        delete current;
                }
                else
                {
                        found=false;
                        current=first;

                        while(current!=NULL&&!found)
                                if(current->;info>;=deleteItem)
                                        found=true;
                                else
                                        current=current->;next;

                                if(current==NULL)
                                        cout<<"Item to be deleted is not in the list."<<endl;
                                else
                                        if(current->;info==deleteItem)
                                        {
                                                trailCurrent=current->;back;
                                                trailCurrent->;next=current->;next;
                                                if(current->;next!=NULL)
                                                        current->;next->;back=trailCurrent;
                                                delete current;
                                        }
                                        else
                                                cout<<"Item to be deleted is not in the list."<<endl;
                }
}
#endif//_DOUBLEINKEDLISTTEP_2004_01_16_XHL

论坛徽章:
0
发表于 2004-03-17 15:54 |显示全部楼层

这样创建双链表的问题所在

这个是和调试好的双链表类模版,也许对楼主有帮助

论坛徽章:
0
发表于 2004-03-18 12:56 |显示全部楼层

这样创建双链表的问题所在

首先谢谢你啦
   这对我以后会用的上的,因为思维都差不多的,呵呵
   不过我想用C来写
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP