6-1链表的插入算法
题目:
代码
c
int InsertPost_link(LinkList llist,DataType x,DataType y)
{
LinkList m=llist->next;
LinkList n;
while(m->data!=x)
{
m=m->next;4
if(m==NULL)
{
printf("not exist data %d\n",x);
return 0;
}
}
n=(LinkList)malloc(sizeof(struct Node));
if(n!=NULL)
{
n->data=y;
n->next=m->next;
m->next=n;
return 0;
}
}
6-2链表的删除算法
代码
c
void DelNode_Link(LinkList head, DataType deldata)
{
PNode p=head->next;
PNode beforeP=head;
while(p!=NULL)
{
if(p->data==deldata)
{
beforeP->next=p->next;
free(p);
return 0;
}
else
{
beforeP=p;
p=p->next;
}
}
printf("not exist %d\n",deldata);
}
6-3移动链表中的最大值到尾部
c
void MoveMaxToTail(LinkList head)
{
PNode pmax=NULL,p=NULL,pre=NULL,end=NULL;
pmax=head->next;
p=head->next->next;
while(p)
{
if(p->data>pmax->data)
{
pmax=p;
}
p=p->next;
}
if(pmax->next==NULL)
{
return 1;
}
else
{
p=head;
while(p)
{
if(p->next==pmax)
{
pre=p;
}
if(p->next==NULL)
{
end=p;
}
p=p->next;
}
pre->next=pmax->next;
pmax->next=end->next;
end->next=pmax;
}
return 0;
}
6-4合并两个递增有序的单循环链表
c
PNode mergeNDeduplicateList(PNode tail1, PNode tail2)
{
int temp;
PNode pre,q;
LinkList head=tail1->next->next;
tail1->next->next=tail2->next->next;
tail2->next->next=NULL;
for(pre=head;pre->next!=NULL;pre=pre->next)
{
for(q=pre->next;q!=NULL;q=q->next)
{
if(pre->data>q->data)
{
temp=pre->data;
pre->data=q->data;
q->data=temp;
}
}
}
pre=head;
q=pre->next;
while(q)
{
if(pre->data==q->data)
{
if(q==tail2->next)
{
tail2->next=pre;
pre->next=NULL;
}
else
{
pre->next=q->next;
free(q);
q=pre->next;
}
}
else
{
pre=q;
q=q->next;
}
}
tail2->next->next=head;
return tail2;
}
6-5链表中奇偶结点的移动
c
PNode Move_Odd_Even(LinkList tail)
{
PNode head=tail->next,pre=head->next,q=pre->next;
PNode pre1,head1=(PNode)malloc(sizeof(struct Node));
PNode pre2,head2=(PNode)malloc(sizeof(struct Node));
pre1=head1;
pre2=head2;
while(q!=head->next)
{
if(pre->data%2==0)
{
pre->next=pre1->next;
pre1->next=pre;
pre1=pre;
}
else
{
pre->next=pre2;
pre2->next=pre;
pre2=pre;
}
pre=q;
q=q->next;
}
head1=head1->next;
pre2->next=head1;
pre1->next=head2;
return pre1;
}
6-6循环队列入队出队
c
void EnQueue_seq(SeqQueue squeue, DataType x)
{
if((squeue->r+1)%squeue->Max==squeue->f)
{
printf("It is FULL Queue!");
}
else
{
squeue->elem[squeue->r]=x;
squeue->r=(squeue->r+1)%(squeue->Max);
}
}
void DeQueue_seq(SeqQueue squeue)
{
if(squeue->f==squeue->r)
{
printf("It is empty queue!");
}
else
{
squeue->f=(squeue->f+1)%(squeue->Max);
}
}
6-7 进制转换(10->16)
c
void Push_seq(SeqStack sstack ,DataType x)
{
if(sstack->top>=(sstack->MAX-1))
{
printf("overflow!\n");
}
else
{
sstack->top++;
sstack->elem[sstack->top]=x;
}
}
void Hexconversion(SeqStack sstack,int n)
{
while(n)
{
int tmp=n%16;
switch(tmp)
{
case 10:tmp='A';break;
case 11:tmp='B';break;
case 12:tmp='C';break;
case 13:tmp='D';break;
case 14:tmp='E';break;
case 15:tmp='F';break;
}
Push_seq(sstack,tmp);
n=n/16;
}
while(!IsNullStack_seq(sstack))
{
n=Top_seq(sstack);
if(n<10)
{
printf("%d",n);
}
else
{
printf("%c",n);
}
Pop_seq(sstack);
}
}
6-8 递归建立和层次遍历二叉树
我先说一下,这道题我没做出来,但是代码我敲了
c
BinTree CreateBinTree_NRecursion()
{
LinkQueue queue=SetNullQueue_Link();
BinTreeNode *s, *p,*Bt;
char ch;
int count=-1;
ch=getchar();
Bt=NULL;
while(ch!='#')
{
s=NULL;
if(ch!='@')
{
s=(BinTreeNode*)malloc(sizeof(BinTreeNode));
s->data=ch;
s->leftchild=s->rightchild=NULL;
}
EnQueue_link(queue,s);
count++;
if(count==0)
{
Bt=s;
}
else
{
p=FrontQueue_link(queue);
if(s!=NULL&&p!=NULL)
{ if(count%2==1)
{
p->leftchild=s;
}
else
{
p->rightchild=s;
}
}
if(count%2==0)
DeQueue_link(queue);
}
ch=getchar();
}
}
return Bt;
}
void LevelOrder(BinTree bt)
{
BinTree p;
LinkQueue queue=SetNullQueue_Link();
if(bt==NULL)return;
p=bt;
EnQueue_link(queue,bt);
while(!IsNullQueue_Link(queue))
{
p=FrontQueue_link(queue);
DeQueue_link(queue);
printf("%c",p->data);
if(p->leftchild!=NULL)
EnQueue_link(queue ,p->leftchild);
if(p->rightchild!=NULL)
EnQueue_link(queue,p->rightchild);
}
}