分解链表

来源:互联网 发布:偶像梦幻祭卡牌数据 编辑:程序博客网 时间:2024/04/19 12:55

编写程序,将单链表A分解成两个单链表A和B,其头指针分别为head和head1,使得A链表中含原链表A中序号为奇数的元素,而B链表中含原链表A中序号为偶数的元素,且保持原来的相对顺序。

 

 

#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>

struct node
{
  int data;
  struct node *next;     
}*head, *head1;


int main()
{
  int i;
  struct node *p, *q;
  head=NULL;
 
 
  for(i=0;i<10;i++)
  {
      p=(struct node*)malloc(sizeof(struct node));
      p->data=i+1;
      p->next=head;
      head=p;            
  }
 
  p=head;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
 
  printf("\n");
  
  p=head;
  q=p->next;
  head1=q;
 
   
  while(p!=NULL)
  {
      p->next=q->next;
      p=p->next;
      if(p!=NULL)
      {   q->next=p->next;
          q=q->next;
      }
      else
        break;  
  }
  q->next=NULL;
 
  p=head;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
  printf("\n");
 
  p=head1;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
  printf("\n");
 
 
  system("pause");
  return 0;
}

 

 

 

/*
#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>
struct node
{
  char data;
  struct node *link;     
}*head;

void ins(struct node *q)
{
  if(head==NULL)
  {
     q->link=NULL;
     head=q;             
  } 
  else
  {
     q->link=head;
     head=q;    
  }  
}

int main()
{
  char ch;
  struct node *p;
  head=NULL;
 
  while((ch=getchar())!='\n')
  {
     p=(struct node *)malloc(sizeof(struct node));
     p->data=ch;
     ins(p);                         
  }
 
  p=head;
 
  while(p!=NULL)
  {
      printf("%c",p->data);
      p=p->link;              
  }
 
  printf("\n");
 
  system("pause");
  return 0;
}


*/


/*
#include<stdio.h>
#include<stdlib.h>

int main()
{
  FILE *fp;
  int i,j;
  if((fp=fopen("zheng.dat","wb"))==NULL)
      exit(0);
  for(i=0;i<5;i++)
  {
      scanf("%d",&j);
      fwrite(&j,sizeof(int),1,fp);               
  }
  fclose(fp);  
 
  system("pause");
  return 0;
}
*/

 

 

若n为奇数

#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>

struct node
{
  int data;
  struct node *next;     
}*head, *head1;


int main()
{
  int i,n;
  struct node *p, *q;
  head=NULL;
 
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
      p=(struct node*)malloc(sizeof(struct node));
      p->data=i+1;
      p->next=head;
      head=p;            
  }
 
  p=head;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
 
  printf("\n");
  
  p=head;
  q=p->next;
  head1=q;
 
   
  while(p!=NULL)
  {
      p->next=q->next;
      p=p->next;
      if(p->next!=NULL)
      {  
          q->next=p->next;
          q=q->next;
        
      }
     
      else
          break;  
     // }
     // else
        //break;  
  }
  q->next=NULL;
 
 
  p=head;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
  printf("\n");
 
  p=head1;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
  printf("\n");
 
 
  system("pause");
  return 0;
}

 


 

若n为偶数

#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>

struct node
{
  int data;
  struct node *next;     
}*head, *head1;


int main()
{
  int i,n;
  struct node *p, *q;
  head=NULL;
 
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
      p=(struct node*)malloc(sizeof(struct node));
      p->data=i+1;
      p->next=head;
      head=p;            
  }
 
  p=head;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
 
  printf("\n");
  
  p=head;
  q=p->next;
  head1=q;
 
   
  while(p!=NULL)
  {
      p->next=q->next;
      p=p->next;
      if(q->next!=NULL)
      {  
          q->next=p->next;
          q=q->next;
        
      }
     
      else
          break;  
     // }
     // else
        //break;  
  }
  q->next=NULL;
 
 
  p=head;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
  printf("\n");
 
  p=head1;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
  printf("\n");
 
 
  system("pause");
  return 0;
}

 

编写程序,将单链表A分解成两个单链表A和B,其头指针分别为head和head1,使得A链表中含原链表A中序号为奇数的元素,而B链表中含原链表A中序号为偶数的元素,且保持原来的相对顺序。(n不分奇数与偶数)

 

#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>

struct node
{
  int data;
  struct node *next;     
}*head, *head1;


int main()
{
  int i,n;
  struct node *p, *q;
  head=NULL;
 
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
      p=(struct node*)malloc(sizeof(struct node));
      p->data=i+1;
      p->next=head;
      head=p;            
  }
 
  p=head;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
 
  printf("\n");
  
  p=head;
  q=p->next;
  head1=q;
 
   
  while(p!=NULL)
  {
      p->next=q->next;
      p=p->next;
      if((p!=NULL)&&(p->next!=NULL))
        {  
          q->next=p->next;
          q=q->next;
        
        }
 
     else
        break;  
  }
  q->next=NULL;
 
 
  p=head;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
  printf("\n");
 
  p=head1;
  while(p!=NULL)
  {
      printf("%d  ",p->data);
      p=p->next;              
  }
  printf("\n");
 
 
  system("pause");
  return 0;
}

 

 

 

0 0
原创粉丝点击