struct number
{
int num;
number * next;
};number * head;void main()
{
number * ps;
number * end;
ps = new number;
head = new number;
cout<<"input your number!!  if you want to exit, input 0!!\n"<<endl;
         cin>>ps->num;
head = NULL;
end = ps; while(ps->num!=0)
{
if(head==NULL)
head = ps;
else

head->next = ps;
}  
end =ps;

cin>>ps->num; }
end->next = NULL;
    delete ps;    while(head)
{
cout<<head->num<<endl;
head = head->next;
}
   
}输入1
    2
    3
    0
运行结果:-56885459 ---并提示错误
请问1,怎样使它们能够顺序显示出来(1,2,3)    2,怎样反序显示出来(3,2,1)

解决方案 »

  1.   

    struct number
    {
    int num;
    number * next;
    };number * head;void main()
    {
    number * ps;
    number * end;
    ps = new number;
    cout<<"input your number!!  if you want to exit, input 0!!\n"<<endl;
             cin>>ps->num;
    head = NULL;
    end = ps; while(ps->num!=0)
    {
    if(head==NULL)
    head = ps;
    else

    head->next = ps;
    }  
    end =ps;

    ps = new number;
             cout<<"input your number!!  if you want to exit, input 0!!\n"<<endl;
                      cin>>ps->num; }
    end->next = NULL;
        delete ps;    while(head)
    {
    cout<<head->num<<endl;
    head = head->next;
    }
       
    }
      

  2.   

    #include "stdio.h"
    #define listsize   100      /*线性表的最大长度为100*/
    typedef  char  ElemType;
    typedef struct
    {ElemType  elem[listsize];
    int  length;
    }Sqlist;
    int  ListDelete (Sqlist *L,int i)
    {        int j;
             if(i<1 || i>L->length)
    { printf("Position error"); /*删除位置出错!*/
     return 0;     }
             for(j=i;j<=L->length-1;j++)
             L->elem[j-1]=L->elem[j];           /*数据元素前移*/
     L->length--;   return 1;   /*长度减1*/
          }
    int  ListInsert (Sqlist* L,int i,ElemType x)
     {  int j;
         if(i<1 || i>L->length+1)
     { printf("Position error");   /*插入位置出错*/
      return 0 ;}
      if(L->length>=listsize)
              printf("overflow");/*表已满*/
         for(j=L->length-1;j>=i-1;j--)
         L->elem[j+1]=L->elem[j];    /*数据元素后移 */
         L->elem[i-1]=x;                  /*插入x */
         L->length++;                     /*长度加1 */
         return  1;
    }void InitSqlist(Sqlist* L)
    {/*初始化线性表*/
     int i;
     printf("please input the length of list\nn="); /*输入线性表初始化时的长度*/
     scanf("%d",&L->length);
     printf("please input char from 1 to%d(char),example:abcdefg\n",L->length);
     getchar();
     for(i=0;i<L->length;i++) scanf("%c",&L->elem[i]); /*输入线性表的各元素*/
    }void print(Sqlist* v)          /*显示当前线性表所有元素*/
    {int i;
     for(i=0;i<v->length;i++) printf("%c ",v->elem[i]);
     printf("\n");
    }int locate(Sqlist* v,char ch)
    {/*在线性表中查找ch的位置,成功返回其位置,失败返回-1*/
     int i=0;
     while(i<v->length&&v->elem[i]!=ch) i++;  /*当前位置后移,直到找到为止*/
     if(v->elem[i]==ch)               /*找到当前元素*/
        return i;
     else  return(-1);
    }void main()
    {
    int n=0;
    int loc,flag=1;
    char j,ch;
    int temp;
    Sqlist L;
    InitSqlist(&L);       /*初始化线性表*/
     while(flag)
        { printf("请选择:\n");
          printf("1->显示所有元素\n");
          printf("2->插入一个元素\n");
          printf("3->删除一个元素\n");
          printf("4->查找一个元素\n");
          printf("5->退出程序    \n");
          scanf("%c",&j);
          switch(j)
      {case '1':print(&L); break; /*显示所有元素*/    case '2':{printf("请输入要插入的元素(一个字符)和插入位置:\n");
       printf("格式:字符,位置;例如:a,2\n");
       scanf(" %c,%d",&ch,&loc);  /*输入要插入的元素和插入的位置*/
       temp=ListInsert(&L,loc,ch);     /*插入*/
       if(temp==0)  printf("插入失败!\n");  /*插入失败*/
       else  {printf("插入成功!\n");   print(&L);} /*插入成功*/
       break;   }
        case '3':{printf("请输入要删除元素的位置:");
       scanf("%d",&loc);    /*输入要删除的元素的位置*/
       temp=ListDelete(&L,loc);  /*删除*/
       if(temp==1) printf("删除元素删除成功\n"); /*删除成功*/
       else printf("该元素不存在!\n");  /*删除失败*/
       print(&L);
       break;   }
        case '4':{printf("请输入要查找的元素:");
       scanf(" %c",&ch);      /*输入要查找的元素*/
       loc=locate(&L,ch);      /*定位*/
       if(loc!=-1) printf("该元素所在位置:%d\n",loc+1); /*显示该元素位置*/
       else    printf("%c 不存在!\n",ch);/*当前元素不存在*/
       break;   }
        default:flag=0;//printf("程序结束,按任意键退出!\n");
      }
     }
     //getchar();
    }
      

  3.   

    struct number
    {
    int num;
    number * next;
    };number * head;void main()
    {
    number * ps;
    number * end;
    ps = new number;
    cout<<"input your number!!  if you want to exit, input 0!!\n"<<endl;
             cin>>ps->num;
    //head = NULL;
    //end = ps;
    end = NULL;//different
    head = ps;//different
    while(ps->num!=0)
    {
    //if(head==NULL)
    // head = ps;
    //else
    //{ 
    // head->next = ps;
    //}
    end->next = ps;//different  
    end =ps;

    ps = new number;
             cout<<"input your number!!  if you want to exit, input 0!!\n"<<endl;
                      cin>>ps->num; }
    end->next = NULL;
        delete ps;    while(head)
    {
    cout<<head->num<<endl;
    head = head->next;
    }
       
    }我把  yxzbbc(口 水) 修改了一下。
      

  4.   

    楼主的最大问题就是没有再循环中动态申请内存,循环外又把唯一的一块内存给Delete掉。
    实现双向链表需要把结构体改成
    struct number
    {
    int num;
    number * next;
    number * prior;
    };
    循环中需要
    ps->prior = end;
    end = ps;
    循环外
    end->next = head;