单链表逆置算法(c++)

  在VC环境中可调试:

 

#include < stdio.h >
#include
< malloc.h >
typedef 
int  elemtype;
typedef 
struct  linknode
{   elemtype data;
    
struct linknode *next;
}
nodetype;

nodetype 
*  createlist()
{   elemtype d;
    nodetype 
*h=NULL,*s,*t;
    
int i=1;
    printf(
"Create a single Link list. ");
    
while(1)
    
{  
       printf(
"Pls Enter the elements of the list(0:Exit)");
       scanf(
"%d",&d);
       
if(d==0)
           
break;

      
if(i==1)
      
{    
         h
=(nodetype*)malloc(sizeof(nodetype));
         h
->data=d;
         h
->next=NULL;
         t
=h;
      }

      
else
      
{    
         s
=(nodetype*)malloc(sizeof(nodetype));
         s
->data=d;
         s
->next=NULL;
         t
->next=s;
         t
=s;
       }

       i
++;
    }

    
return h;
}

void  disp(nodetype  * h)
{
    nodetype 
*p=h;
    printf(
"Print the single link list: ");
    
if(p==NULL)
       printf(
"The list has no elements! ");
    
while(p!=NULL)
    
{   
        printf(
"%d  ",p->data);
        p
=p->next;
    }

    printf(
" ");
}

nodetype 
* reverse(nodetype  * h)
{  
    nodetype 
*p,*q,*r;
    p
=h;
    
if(p->next=NULL)
    
{    
        printf(
"The reversed link list is: ");
        
return NULL;
    }

    
else
    
{   
        p
=h;
        q
=p->next;
        
while(q!=NULL)
        
{    
            r
=q->next;
            q
->next=p;
            p
=q;
            q
=r;
        }

        h
->next=NULL;
        h
=p;
        
return h;
    }

}
    
void  main()
{   
    nodetype 
*head;
    head
=createlist();
    disp(head);
    head
=reverse(head);
    disp(head);
}
 

 第二个,未编译:

 

#include < stdio.h >
#include
< stdlib.h >
#include
< malloc.h >
typedef 
struct  node {
  
int data;
  
struct node *next;
}
Node, * LinkList;
void  CreateList(LinkList  * head)
{   LinkList p,q;
    
int x;
if((*head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
    (
*head)->next=NULL;
q
=(*head);
printf(
"please input data: ");
scanf(
"%d",&x);
do{
        
if((p=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
  p
->data=x;
  p
->next=NULL;
  q
->next=p;
  q
=p;
  p
=NULL;
  printf(
"please input number: ");
  fflush(stdin);
  scanf(
"%d",&x);
}
while(x!=-1);
}
  void  Reverse(LinkList head)
{LinkList p,q,s=NULL,Head;
int count=0,i;
p
=head->next;
while(p)
   
{   count++;
       p
=p->next;
   }

   p
=head->next;
   
if((Head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
   Head
->next=p;
   head
->next=NULL;
for(i=0;i<count;i++)
   
{  p=Head->next;
      Head
->next=p->next;
   p
->next=NULL;
   
if(s==NULL)
   s
=p;
   
else
   
{ p->next=s;
      s
=p;
   }

   }

   head
->next=s;
   free(Head);
}
  void  Print(LinkList head)
{ LinkList p;
  p
=head->next;
  
while(p)
  
{  printf("%d ",p->data);
     p
=p->next;
   }

  
}
 main()
{ LinkList head;
  CreateList(
&head);
  Print(head);
  Reverse(head);
  printf(
" ");
  Print(head);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值