链表移动 -链表

编写一个函数,将给定的带有头结点的单向链表的前m个节点移到链尾。
    结构的定义:
struct node
{  int num;
             struct node *next;
}
函数的原型:struct node *monenode( struct node *head,int m ), 其中:参数head 是单向链表的头指针;参数m 是要移动的节点数。函数的返回值是单向链表的头指针。
例如当m=4时:

 

预设代码

前置代码

view plainprint?

  1. /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. typedef struct numLink  
  5. {  
  6.     int no;  
  7.     struct numLink *next;  
  8. }NODE;  
  9.   
  10. NODE *monenode( NODE *head, int m );  
  11.   
  12. void SetLink( NODE *h, int n )  
  13. {  
  14.     NODE *p=NULL, *q=NULL;  
  15.     int i;  
  16.     for( i=0; i<n; i++)  
  17.     {  
  18.         p = (NODE *)malloc(sizeof(NODE));  
  19.         p->no = i+1;  
  20.         p->next = NULL;  
  21.         if( h->next == NULL )  
  22.         {  
  23.             h->next = p;  
  24.             q = p;  
  25.         }  
  26.         else  
  27.         {  
  28.             q->next = p;  
  29.             q = q->next;  
  30.         }  
  31.     }  
  32.     return;  
  33. }  
  34.   
  35. int main( )  
  36. {  
  37.     int n,m;  
  38.     NODE *head=NULL, *q=NULL;  
  39.     scanf("%d%d",&n,&m);  
  40.     head = (NODE *)malloc(sizeof(NODE));  
  41.     head->no = -1;  
  42.     head->next = NULL;  
  43.   
  44.     SetLink( head, n );  
  45.   
  46.     q = monenode( head, m );  
  47.   
  48.     do  
  49.     {  
  50.         printf("%d ",q->next->no);  
  51.         q = q->next;  
  52.     }while( q->next != NULL );   
  53.     printf("\n");  
  54.     return 0;  
  55. }  
  56.   
  57. /* PRESET CODE END - NEVER TOUCH CODE ABOVE */  
    /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
    /*编写一个函数,将给定的带有头结点的单向链表的前m个节点移到链尾。
        结构的定义:
    struct node
    {  int num;
                 struct node *next;
    }
    函数的原型:struct node *monenode( struct node *head,int m ),
     其中:参数head 是单向链表的头指针;参数m 是要移动的节点数。
     函数的返回值是单向链表的头指针。
    */  
    #include <stdio.h>  
    #include <stdlib.h>  
    typedef struct numLink  
    {  
        int no;  
        struct numLink *next;  
    }NODE;  
    NODE *monenode( NODE *head, int m )
    {
    	int j = 0;
    	NODE *p = head->next;
    	NODE *temp=head->next;
    	for(int i = 0;i < m;i++)
    	{
    		temp=head->next;
    		p = head->next;
    		while(temp->next != NULL)
    		{
    			temp = temp->next;
    			j ++;
    		}
    		if(j < m)
    			return head;
    		head->next = p->next;
    		p->next=NULL;
    		temp->next=p;
    	}
    	return head;
    } 
    void SetLink( NODE *h, int n )  
    {  
        NODE *p=NULL, *q=NULL;  
        int i;  
        for( i=0; i<n; i++)  
        {  
            p = (NODE *)malloc(sizeof(NODE));  
            p->no = i+1;  
            p->next = NULL;  
            if( h->next == NULL )  
            {  
                h->next = p;  
                q = p;  
            }  
            else  
            {  
                q->next = p;  
                q = q->next;  
            }  
        }  
        return;  
    }  
      
    int main( )  
    {  
        int n,m;  
        NODE *head=NULL, *q=NULL;  
        scanf("%d%d",&n,&m);  
        head = (NODE *)malloc(sizeof(NODE));  
        head->no = -1;  
        head->next = NULL;  
      
        SetLink( head, n );  
      
        q = monenode( head, m );  
      
        do  
        {  
            printf("%d ",q->next->no);  
            q = q->next;  
        }while( q->next != NULL );   
        printf("\n");  
        return 0;  
    }  
    /* PRESET CODE END - NEVER TOUCH CODE ABOVE */  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值