---2012---
Problem1:/*将链表中所有正数结点移到负数结点前面,并返回第一个负数结点的位置*/
Solution:/*将链表L从头结点后断开,用p遍历头结点后的所有结点,将正数结点头插到L链表上,负数结点尾插到链表L上,头插的次数加一就是第一个负数结点位置 */
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
int movep(LNode *&L){
LNode *p=L->next;
LNode *pre=L;
L->next=NULL;
LNode *temp=NULL;
int count=0;
while(p!=NULL){
if(p->data<0){//尾插
temp=p;
p=p->next;
pre->next=temp;
temp->next=NULL;
pre=temp;
}
if(p->data>0){//头插
count++;
temp=p;
p=p->next;
temp->next=L->next;
L->next=temp;
if(count==1)
pre=temp;
}
}
printf("%d\n",count+1);
return 0;
}
Problem2:/*树采用孩子兄弟结构存储,采用非递归算法,求树的宽度*/
int Width(TreeNode *T){
if(T==NULL)
return 0;
TreeNode *queue[maxsize]=NULL;
TreeNode *node=NULL;
int front=-1,rear=-1,last=0;
int width=1,count=0;
queue[++rear]=NULL;
while(front<rear){
node=queue[++front];
if(node->firstchild!=NULL){
node=node->firstchild;
queue[++rear]=node;
while(node->nextsibling!=NULL){
node=node->nextsibling;
queue[++rear]=node;
}
}
if(front==last){
last=rear;
count=rear-front;
}
if(count>width)
width=count;
}
return width;
}
---2013---
Problem1:/*A,B是两个递增链表,使A中保留A与B的交集,且A中元素递减*/
void solve1(LNode *A,LNode *B){
LNode *p=A->next;
LNode *q=B->next;
LNode *temp=NULL;
A->next=NULL;
while(p!=NULL&&q!=NULL){
if(p->data<q->data)
p=p->next;
if(p->data>q->data)
q=q->next;
if(p->data==q->data){
temp=p;
p=p->next;
q=q->next;
temp->next=A->next;
A->next=temp;
}
}
}
/*若A,B为普通链表,求A,B的交集,并成为一个递减有序的单链表*/
void solve11(LNode *A,LNode *B){
LNode *p=A->next;
LNode *q=B->next;
LNode *temp,*pre=A;
int flag=0;
while(p!=NULL){
while(q!=NULL&&flag==0){
if(q->data==p->data)
flag=1;
else
q=q->next;
}
if (flag==0){
pre->next=p->next;
free(p);
p=pre->next;
}
else{
pre=p;//始终要让pre为p的前驱结点
p=p->next;
flag=0;
}
q=B->next;//一轮结束后,让q继续从头开始查找
}
p=A->next; //相当于把A结点复制给了p
A->next=NULL;
while(p!=NULL){//相当于把元素重新插到头结点A后面
temp=p;
p=p->next;
temp->next=A->next;
A->next=temp;
}
}
Problem2:/*采用非递归法求二叉树高度*/
int Depth(BTNode *T){
BTNode *queue[maxsize]={NULL};
BTNode *node=NULL;
int front=-1,,rear=-1,last=0;
int levle=0;
queue[++rear]=T;
while(front<rear){
node=queue[++front];
if(node->lchild!=NULL)
queue[++rear]=node->child;
if(node->rchild!=NULL)
queue[++rear]=node->rchild;
if(front==last){
last=rear;
++level;
}
}
return level;
}
Problem1:/*设二叉树T,用二叉链表结构存储。要求对每个元素值等于x的节点,
删除以它为根的子树,并释放相应的空间*/
Solution:/*采用层次遍历法遍历二叉树,先让元素入队,再出队,
并在出队的时候将其与x比较,若相等,则递归删除该子树*/
typedef struct BTNode{
int data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode;
void delet(BTNode *root){
if (root!=NULL){
delet(root->lchild);
delet(root->rchild);
}
free(root);
}
void Delete(BTNode *T,int x){
BTNode *queue[max]={NULL};
BTNode *node=NULL;
int front=-1,rear=-1,last=0;
queue[++rear]=T;
while(front<rear){
node=queue[++front];
if (node->data===x){
delet(node->lchild);
delet(node->rchild);
free(node);
}
if(node->lchild!=NULL)
queue[++rear]=node->lchild;
if(node->rchild!=NULL)
queue[++rear]=node->rchild;
}
}
Problem2:/*设N个学生的成绩在0~100之间且用线性表表示,编写函数使>=60的元素在<60的前面*/
Solutio:/*让 i和j同时从前和从后扫描,i遇到小于60的就将其换到后面去,
j遇到大于等于60就将其换到前面去*/
void swap(int num[],int n){
int i=0,j=n-1;
int temp=0;
while(i<j){
while(i<j&&num[i]>=60)
++i;
while(i<j&&num[j]<60)
--j;
if(i<j){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Problem3:/*设A,B为递减有序的单链表,编写函数使A,B链表合并成一个递增的链表,
且相同的元素只保留一个*/
Solution:/*先将A,B的头结点与其后结点元素断开,让指针p,q分别遍历A,B后面的元素,
每次从中选出最大的头插在A结点后面。每次在插插之前都要先和上一次插的结点
进行比较,如果不相同就插,否则删除*/
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
LNode *merge(LNode *A,LNode *B){
LNode *p=A->next;
LNOde *q=B->next;
A->next=NULL,B->next=NULL;
while(p!=NULL||q!=NULL){
if(q==NULL||(p!=NULL&&p->data>=q->data)){
temp=p;
p=p->next;
}
if(p==NULL||(q!=NULL&&q->data>=p->data)){
temp=q;
q=q->next;
}
if(A->next!=NULL&&temp->data==A->next->data){
free(temp);
}
else{
temp->next=A->next;
A->next=temp;
}
}
free(B);
return A;
}
Problem5:/*用非递归的方法,在二叉树中找两个给定的整数,若都没找到,则返回-2;找到一个,返回-1;找到两个则返回两个数的层数差*/
Solution:/*采用层次遍历的方法,查找元素,然后输出相应的结果*/
typedef struct BTNode{
int data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode;
int Find(BTNode *root,int x1,int x2){
BTNode *queue[maxsize]={NULL};
BTNode *node;
int front=-1,rear=-1,last=0;
int level=0,v=-2,levle_temp=0;
queue[++rear]=root;
while(front<rear){
node=queue[++front];
if(node->lchld!=NULL)
queue[++rear]=node->lchild;
if(node->rchild!=NULL)
queue[++rear]=node->rchild;
if(front==last){
last=rear;
++level;
}
if(node->data==x1||node->data==x2){
++v;
if(v==-1)
level_temp=level;
if(v==0)
v=level-level_temp;
}
}
return v;
}
Problem6:/*设单链表的元素为整型,查找中位结点,使该结点前所有元素之和 与该结点后所有元素之和的的差值最小,最后删除该结点*/
Solution:/*让指针p遍历链表所有结点,指针q遍历p结点前的所有结点,指针r遍历p结点后面的所有结点在此过程中分别计算出前后的和,并计算差值,最后找出差值最小的结点,然后删除*/
void solvezhenti(LNode *L){
LNode *p=L->next;
LNode *q=L->next;
LNode *pre=L;
LNode *r,*k,*remove=NULL;
int suml,sumr,delta=0,min=INFMax;
while(p!=NULL){
suml=0,sumr=0;
while(q!=p){
suml=suml+q->data;
q=q->next;
}
r=p->next;
while(r!=NULL){
sumr=sumr+r->data;
r=r->next;
}
delta=abs(suml-sumr);
if(delta<min){
min=delta;
k=pre;
}
pre=p;
p=p->next;
q=L->next;
}
remove=k->next;
k->next=k->next->next;
free(remove);
}
Problem7:/*设一棵二叉树采用孩子兄弟二叉链表结构存储,给定书中结点p,求p结点的双亲结点*/
Solution:/*采用层次遍历法,在结点进队时将其暂时设置为父节点,在后续查找时,若发现其孩子
结点中存在和p相等的结点,则返回父节点,否则继续查找*/
typedef struct TreeNode{
Elemtype data;
struct TreeNode *firstchild;
struct TreeNode *nextsibling;
}TreeNode;
TreeNode *solve7(TreeNode *root,TreeNode *p){
TreeNode *queue[maxsize]={NULL};
TreeNode *node=NULL,temparent=NULL;
int front=-1,rear=-1,last=0;
queue[++rear]=root;
while(front<rear){
node=queue[++front];
if(node->firstchild!=NULL)
temparent=node;
if(node->firstchild==p)
return temparent;
node=node->firstchild;
while(node->nextsibling!=NULL){
node=node->nextsibling;
if(node==p)
return temparent;
queue[++rear]=node;
}
if(front==last)
last=rear;
}
}
829代码练习
最新推荐文章于 2025-12-05 23:25:47 发布
2378

被折叠的 条评论
为什么被折叠?



