33,010
社区成员
发帖
与我相关
我的任务
分享
typedef int Element ;
typedef struct Node *List;
struct Node{
Element Date;
List Link;
};
List Merge(List list1,List list2,List list3);
void Insert(Element x,List L);
void prin(List l);
List InitList();
int main()
{
int a[]={1,3,5,7,9};
int b[]={2,4,6,8,10};
List list1,list2,list3;
list1=InitList();
list2=InitList();
list3=InitList();
for(int i=0;i<5;i++)
{
Insert(a[i],list1);
Insert(b[i],list2);
}
//prin(list1);
prin(list3);
}
void Insert(Element x,List L)
{
List temp;
temp=L;//</strong><span style="color:#ff0000;"><strong>这里应该是L的指针指向的地址赋给了temp,所以并不需要传出temp,因为temp指的正是L;</strong></span><strong style="color: rgb(51, 51, 51);">
while(temp->Link!=NULL)
{
temp=temp->Link;//<span style="color:#ff0000;">加入的循环会一直到尾部再进行插入</span>
}
List Next;
Next=(List)malloc(sizeof(struct Node));
if(Next==NULL)
{
printf("error");
}
Next->Date=x;
Next->Link=temp->Link;
temp->Link=Next;
}
typedef int ElementType;
typedef struct AVLTreeNode *AVLTree,*Position;
struct AVLTreeNode{
ElementType Date;
AVLTree left;
AVLTree right;
int Height;
};
static int Height(Position P);
AVLTree Insert(ElementType *x,AVLTree *T);
AVLTree Insert(ElementType *x,AVLTree *T)
{
if(*T== NULL)
{
*T=(AVLTree)malloc(sizeof(AVLTree));
if(*T==NULL)
printf("out of space");
else
{
(*T)->Date=*x;
(*T)->left=(*T)->right=NULL;
(*T)->Height=0;
}
}
else
{
if(*x<(*T)->Date)
{
(*T)->left=Insert(x,&((*T)->left));
if(Height((*T)->left)-Height((*T)->right)==2)
if(*x<((*T)->left->Date))
*T=SingleRotateLeft(*T);
else
*T=DoubleRotataeleft(*T);
}
else if(*x>(*T)->Date)
{
(*T)->right=Insert(x,&((*T)->right));
if(Height((*T)->right)-Height((*T)->left)==2)
if(*x>(*T)->left->Date)
*T=SingleRotateRight(*T);
else
*T=DoubleRotataeright(*T);
}
}
return *T;
}
int main()
{
AVLTree T=NULL;
AVLTree Q=NULL;
int a[]={3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9};
for(int i=0;i<16;i++)
Insert(&a[i],&T);
}