王道数据结构大题代码
线性表
#include <stdio.h>
#include <stdlib.h>
void delMin(int *arr,int len){
if(!len){
printf("数组为空");
return 0;
}
int min = *arr,minPos = 0;
for(int i = 0;i < len;i++){
if(min > *(arr+i)){
min = *(arr+i);
minPos = i;
}
}
//找到最小值之后,将最后一个元素方法哦之前最小值元素那里去
*(arr + minPos) = *(arr + len -1);
*(arr + len -1) = NULL;
}
int main(){
int n;//手动输入元素的个数
printf("请输入数组的长度:n=");
scanf("%d",&n);
int *arr = (int*)malloc(sizeof(n));
printf("请输入数组的元素值:");
for(int i = 0;i < n;i++){
scanf("%d",arr+i);
}
for(int i = 0;i < n;i++){
printf("%d",*(arr+i));
}
printf("--------");
delMin(arr,n);
for(int i = 0;i < n-1;i++){
printf("%d",*(arr+i));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void revrese(int *arr,int len){
int tmp;
for(int i = 0;i < len/2;i++){
tmp = *(arr+i);
*(arr+i) = *(arr+len-i-1);
*(arr+len-i-1) = tmp;
}
}
int main(){
int n;//手动输入元素的个数
printf("请输入数组的长度:n=");
scanf("%d",&n);
int *arr = (int*)malloc(sizeof(n));
printf("请输入数组的元素值:");
for(int i = 0;i < n;i++){
scanf("%d",arr+i);
}
for(int i = 0;i < n;i++){
printf("%d",*(arr+i));
}
printf("\n----\n");
revrese(arr,n);
for(int i = 0;i < n;i++){
printf("%d",*(arr+i));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int delX(int *arr,int len,int x){
int k = 0;//用来记录删除元素的总数
int i = 0;
while(i < len){
if(*(arr+i) == x){
k++;
}else{
*(arr + i-k) = *(arr+i);
}
i++;
}
for(int i = len -k;i<len;i++){
*(arr+i) = NULL;
}
return k;
}
int main(){
int arr[] = {1,2,3,2,2,4};
int len = sizeof(arr) / sizeof(arr[0]);
int k = delX(arr,len,2);
for(int i = 0;i < len -k;i++){
printf("%d",*(arr+i));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void deletePoint(int *arr,int s,int t,int len){
int k = 0;
int i = 0;
for(i = 0;i < len && *(arr + i) < s;i++);
if(i >= len)
return ;
for(k = i;k<=len && *(arr+k) <= t;k++);
for(;k<len;i++,k++){
*(arr+i) = *(arr+k);
}
printf("当前数组为:");
for(int j = 0;j<i;j++)
printf("%d ",*(arr+j));
}
int main(){
int arr[] = {1,2,4,6,10,15};
int s,t;//用于给定区间范围
int len = sizeof(arr) / sizeof(int);
printf("请输入区间范围:,s<t\n");
do{
printf("s=");
scanf("%d",&s);
printf("t=");
scanf("%d",&t);
}while(s>t);
deletePoint(arr,s,t,len);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int deleteRepitition(int *arr,int len){//C语言无法直接修改数组长度,返回的是去重后的数组长度
int k = 0;//记录下标
for(int i = 0;i<len;i++){
if(*(arr+i) - *(arr+i+1)){
*(arr + k++) = *(arr+i);
}
}
if(*(arr+len-1) == *(arr+len)){//如果最后一个元素恰好和后一个存储空间元素相等,需要单独处理
*(arr + k++) = *(arr+len-1);
}
return k;
}
int main(){
int arr[] = {1,1,2,2,2,3,3,4,5};
int len = sizeof(arr) / sizeof(arr[0]);
len = deleteRepitition(arr,len);
for(int i = 0;i<len;i++){
printf("%d ",*(arr+i));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int *mergeList(int *arr1,int *arr2,int len1,int len2){
int *newArr = (int*) malloc((len1+len2));
int k = 0,i = 0,j = 0;
for(;i<len1&&j<len2;){
*(arr1+i) < *(arr2+j) ? (*(newArr+k++) = *(arr1+i++)) : (*(newArr+k++) = *(arr2+j++));
}
while(i<len1){
*(newArr+k++) = *(arr1+i++);
}
while(j<len2){
*(newArr+k++) = *(arr2+j++);
}
return newArr;
}
int main(){
int arr1[] = {1,3,5,8};
int arr2[] = {2,4,6,7};
int len1 = sizeof(arr1) / sizeof(int);
int len2 = sizeof(arr2) / sizeof(int);
int *res;
res = mergeList(arr1,arr2,len1,len2);
for(int i = 0;i<len1+len2;i++){
printf("%d ",*(res+i));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void reverseList(int *arr,int from,int end){
if(from > end){
return ;
}
int tmp;
for(int i = from;i <= (from+end)/2;i++){
tmp = *(arr+i);
*(arr+i) = *(arr+end-i+from);
*(arr+end-i+from) = tmp;
}
}
int main(){
int arr[] = {1,2,3,4,5,11,22,33,44,55,66};
reverseList(arr,0,10);
reverseList(arr,0,5);
reverseList(arr,6,10);
for(int i = 0;i<11;i++){
printf("%d ",arr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int findX(int *arr,int len,int x){
int low = 0,high = len -1,mid;
while(low <= high && (mid = (low + high)/2)){
if(x == *(arr+mid)){
break;
}
if(x < *(arr+mid)){
high = mid-1;
}else{
low = mid +1;
}
}
if(low > high){//未找到,插入
int i;
for(i = len-1;i>high;i--){
*(arr+i+1) = *(arr+i);
}
*(arr+i+1) = x;
return len + 1;
}else{//找到了,进行对调
int tmp = *(arr+mid+1);
*(arr+mid+1) = *(arr+mid);
*(arr+mid) = tmp;
return len;
}
}
int main(){
int arr[10] = {1,3,5,6,9,12,45,78};
int x,len;
printf("请输入要查找的元素:x=");
scanf("%d",&x);
len = findX(arr,8,x);
for(int i =0;i < len;i++){
printf("%d ",*(arr+i));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void reverseList(int *arr,int from,int end){
if(from > end){
return ;
}
int tmp;
for(int i = from;i <= (from+end)/2;i++){
tmp = *(arr+i);
*(arr+i) = *(arr+end-i+from);
*(arr+end-i+from) = tmp;
}
}
int main(){
int arr[] = {1,2,3,4,5,11,22,33,44,55,66};
printf("请输入开始位置");
int p = 0;
scanf("%d",&p);
reverseList(arr,0,10);
reverseList(arr,p,10);
reverseList(arr,0,p-1);
for(int i = 0;i<11;i++){
printf("%d ",arr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int findMidNum(int *l1,int *l2,int len){
int count = 0,midlle = 0;
for(int i = 0,j = 0;*(l1+i) && *(l2+j);){
if(count++ == len){
midlle = *(l1+i) < *(l2+j) ? *(l1+i) : *(l2+j);
break;
}else{
*(l1+i) < *(l2+j) ? i++ : j++;
}
}
return midlle;
}
int main(){
int l1[] = {1,3,4,7,9};
int l2[] = {2,4,6,8,10};
int len = sizeof(l1) / sizeof(int);
printf("中位数是:%d",findMidNum(l1,l2,len-1));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int findMain(int *arr,int len){
int c = *arr,couunt = 1,mainCount = 0;
for(int i = 1;i < len;i++){
if(c == *(arr+i)){
couunt++;
}else{
if(couunt > 0 ){
couunt--;
}else{
c = *(arr+i);
couunt = 1;
}
}
}
if(couunt >0){
for(int i = 0;i < len;i++){
if(*(arr+i)==c){
mainCount++;
}
}
}
if(mainCount > len/2){
return c;
}else{
return -1;
}
}
int main(){
int arr[] = {0,5,5,3,5,7,5,5};
int mainNum = 0;
int len = sizeof(arr) / sizeof(int);
mainNum = findMain(arr,len);
printf("该数组的主元素为:%d",mainNum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int findMinZ(int *arr,int n){//n为数组长度
int *arrB = (int*) malloc(sizeof(int)*n);
for(int i = 0;i < n;i++){
arr[i] > 0 ? (arrB[arr[i] -1] = 1): (arrB[i]=0);
}
int k = 0;
for(;k<n;k++){
if(arrB[k] == 0){
return k+1;
}
}
return n+1;
}
int main(){
int arr[] = {1,2,3};
int min = findMinZ(arr,3);
printf("%d",min);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int dis(int a,int b,int c){//返回距离
return abs(a-b) + abs(b-c) + abs(a-c);
}
int min(int a,int b,int c){//返回当前最小值,此时便移动它的指针
if(a<b && a<c){
return a;
}else if(b < c && b<a){
return b;
}else {
return c;
}
}
int findMinofTrip(int *S1,int *S2,int *S3,int len1,int len2,int len3){
int i = 0;
int j = 0;
int k = 0;
int Max = 1000;//设定一个最大值,假定为1000
int nowD = 0;
while(i<len1 && j<len2 && k<len3){
nowD = dis(S1[i],S2[j],S3[k]);
if(nowD < Max){
Max = nowD;
}
if(S1[i] == min(S1[i],S2[j],S3[k]))
{
i++;
}else if(S2[j] == min(S1[i],S2[j],S3[k]) ){
j++;
}else{
k++;
}
}
return Max;
}
int main(){
int S1[] = {-1,0,9};
int S2[] = {-25,-10,10,11};
int S3[] = {2,9,17,30,41};
int len1 = sizeof(S1) / sizeof(int);
int len2 = sizeof(S2) / sizeof(int);
int len3 = sizeof(S3) / sizeof(int);
int minD = findMinofTrip(S1,S2,S3,len1,len2,len3);
printf("%d",minD);
return 0;
}
标签:arr,include,return,线性表,int,代码,len,++,数据结构
From: https://www.cnblogs.com/kohler21/p/17658064.html