链表

#include "stdio.h"
#include"stdlib.h"

typedef struct Node
{
    int data;  
    struct Node *next;  
}Linklist;
 
Linklist *InitList(Linklist *L)    
{
    L = (Linklist *) malloc(sizeof(Linklist));
    L->next = NULL;
    return L;
}
Linklist *CreateList()
{
   
    int x;
    Linklist *L,*r,*p;
    L = InitList(L); 
    r = L;
    while(1)
    {
  scanf("%d",&x);
        p = (Linklist *)malloc(sizeof(Linklist));
        p -> data = x;
        p -> next = NULL;
        r->next = p;
        r = r->next;    
  if( getchar()=='\n')
  {
   break;
  }
    }
    return L;
}
void connect(Linklist *L,Linklist *S){
 Linklist *p;
 for(p=L;p->next!=NULL;p=p->next);
 p->next = S->next;

}
void output(Linklist *L) 
{
    Linklist *p,*temp;
 int t;
    p = L->next;
  for (temp = L->next; temp->next != NULL; temp = temp->next){
        for (p = L->next; p->next != NULL; p = p->next){
            if (p->data > p->next->data){
                t = p->data;
                p->data = p->next->data;
                p->next->data = t;
            }
        }
    }
 p = L->next;
 for(;p!=NULL;p=p->next)
    {
        printf("%d ",p->data);
    }
 printf("\n");
}
void put(Linklist *L)
{
  Linklist *p;
  p = L->next;
 for(;p!=NULL;p=p->next)
    {
        printf("%d ",p->data);
    }
 printf("\n");
}
void change(Linklist *L,Linklist *S)
{
  Linklist *p,*q,*tmp;
  p = L->next->next;
  q = S->next->next->next ;

 tmp = S->next->next->next->next ;
  q->next = L->next->next->next ;
  L->next->next = q;
 
  

  p->next = tmp ;
  S->next->next->next= p;
} 
int main()
{

    Linklist *L,*S;
    L = CreateList();
 S = CreateList();
    output(L);
 output(S);
 change(L,S);
 put(L);
 put(S);
 

}

你可能感兴趣的:(链表)