C语言:移动数列给定一个长度为N的连续数列,给M次操作,每次操作给定一个数K,要求把当前数列中的第K个数移动到数列最前面

问题描述:

C语言:移动数列
给定一个长度为N的连续数列,给M次操作,每次操作给定一个数K,要求把当前数列中的第K个数移动到数列最前面,用链表实现,输出M次操作后的数列.
#include
#include
#define LEN sizeof(struct Hn)
struct Hn
{
int num;
struct Hn *next;
};
struct Hn * head;
struct Hn* zhao(int x)
{
struct Hn*p = head;
struct Hn*q = NULL;
while (p -> next = NULL)
p = p -> next;
q = (struct Hn *)malloc(LEN);
q -> num = x;
q -> next = NULL;
p -> next = q;
return(head);
}
void move(struct Hn *head,int a)
{
int j;
struct Hn *p,*k;
p=head;
if(head!=NULL)
{
for(j=1;jnext;
k=p->next;
p->next=k->next;
k->next=head;
head=k;
}
}
void print()
{
struct Hn *q;
q=head;
if(head!=NULL)
do
{
printf("%d ",q->num);
q=q->next;
}while(q!=NULL);
}
void main()
{
int x,i,N,M,a;
struct Hn *head;
scanf("%d",&N);
for(i=0;i
1个回答 分类:综合 2014-11-16

问题解答:

我来补答
一次移动操作 等价于 一次删除操作 加上 一次插入操作.
删除操作 跟 插入操作 书上都有.
 
 
展开全文阅读
剩余:2000