64,639
社区成员
发帖
与我相关
我的任务
分享
//假设带表头结点的单向链表头指针为head,试编写一个算法将值为5的结点插入到连接表的第k个结点前,删除第k个节点,并对该链表进行排序。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
int data;
struct NODE *next;
} H,*head,*p,*q,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t,m;
int main() {
srand(time(NULL));
//填写头节点数据
H.data=-1;
H.next=NULL;
head=&H;
//创建10个节点的单链表
p=head;
for (i=0;i<10;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=rand()%100;//填写0..99的随机值
q->next=NULL;
p->next=q;
p=q;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//将值为5的结点插入到单链表的第k个结点前
k=3;
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==n) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=5;
q->next=p->next;
p->next=q;
break;
}
p=p->next;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//删除第k个节点
k=5;
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==n) {
q=p->next;
if (q) {
p->next=q->next;
free(q);
}
break;
}
p=p->next;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//从小到大排序
for (p=head;p!=NULL && p->next!=NULL;p=p->next) {
for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {
if (p->next->data > q->next->data) {
//交换data
// printf("swap %02d %02d\n",p->next->data,q->next->data);
// t=p->next->data;p->next->data=q->next->data;q->next->data=t;
//或者
//交换next
// printf("swap %02d %02d\n",p->next->data,q->next->data);
s1=p->next;
s2=p->next->next;
s3=q->next;
s4=q->next->next;
if (s2!=s3) {
p->next=s3;
s3->next=s2;
q->next=s1;
s1->next=s4;
} else {
p->next=s3;
s3->next=s1;
q=s3;
s1->next=s4;
}
//输出整个单链表
// s=head->next;
// while (1) {
// if (NULL==s) {
// printf("\n");
// break;
// }
// printf("%02d->",s->data);
// s=s->next;
// }
// getchar();
}
}
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10
m=4;
n=6;
k=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
k++;
if (m+1==k) {
q=p;
}
s=p;
p=p->next;
}
s1=head->next;
head->next=q->next;
s->next=s1;
q->next=NULL;
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//释放所有节点
p=head->next;
while (1) {
if (NULL==p) {
break;
}
q=p->next;
free(p);
p=q;
}
return 0;
}
//18->94->58->17->27->20->43->57->75->78->
//18->94->05->58->17->27->20->43->57->75->78->
//18->94->05->58->27->20->43->57->75->78->
//05->18->20->27->43->57->58->75->78->94->
//43->57->58->75->78->94->05->18->20->27->
//
File: "C:\Program Files\OpenCV\modules\calib3d\src\fundam.cpp" 2 occurrences found on 2 lines
588: CV_IMPL int cvFindFundamentalMat( const CvMat* points1, const CvMat* points2,
1101: int n = cvFindFundamentalMat( &_pt1, &_pt2, &matF, method, param1, param2, p_mask );
File: "C:\Program Files\OpenCV\modules\calib3d\test\test_fundam.cpp" 1 occurrences found on 1 lines
981: f_result = cvFindFundamentalMat( &_input0, &_input1, &F, method, MAX(sigma*3, 0.01), 0, &mask );
File: "C:\Program Files\OpenCV\modules\legacy\src\compat.cpp" 1 occurrences found on 1 lines
505: cvFindFundamentalMat(pointsMat1,pointsMat2,&fundMatr,CV_FM_RANSAC,1,0.99,0);
File: "C:\Program Files\OpenCV\modules\legacy\src\corrimages.cpp" 1 occurrences found on 1 lines
399: int num = cvFindFundamentalMat(tmpPoints1,tmpPoints2,&fundMatr,CV_FM_RANSAC,threshold,0.99,pStatus);
627: typedef struct CvMat
628: {
629: int type;
630: int step;
631:
632: /* for internal use only */
633: int* refcount;
634: int hdr_refcount;
635:
636: union
637: {
638: uchar* ptr;
639: short* s;
640: int* i;
641: float* fl;
642: double* db;
643: } data;
644:
645: #ifdef __cplusplus
646: union
647: {
648: int rows;
649: int height;
650: };
651:
652: union
653: {
654: int cols;
655: int width;
656: };
657: #else
658: int rows;
659: int cols;
660: #endif
661:
662: }
663: CvMat;