c语言练习题【数据类型、递归、双向链表快速排序】
练习1:数据类型
请写出以下几个数据的数据类型
整数 a
a 的地址
存放a的数组 b
存放a的地址的数组
b的地址
c的地址
指向 printf 函数的指针 d
存放 d的数组
-
整数
a
的类型
数据类型是int
-
a
的地址
数据类型是int*
(指向int
类型的指针) -
存放
a
的数组b
数据类型是int[]
(整型数组) -
存放
a
的地址的数组c
数据类型是int*[]
(指针数组,每个元素是指向int
的指针)
c的数据类型是指针数组类型,可以表示为:int * c[ ] = {&a}; -
b
的地址
指向整型数组的指针
b的数据类型是int(*)[](
数组指针),b
的地址可以表示为: b, &b, &b[0] -
c
的地址
数据类型是int*(*)[]
(指向指针数组的指针) -
指向
printf
函数的指针d
printf
函数的类型是int(*)(const char*, ...)
,d
的数据类型是int(*)(const char*, ...)
-
存放
d
的数组
数据类型是int(*[])(const char*, ...)
(函数指针数组,每个元素是指向printf
函数的指针)。#include <stdio.h> int main() { // 打印 "Hello, World!" 到控制台 printf("Hello, World!\n"); // 定义一个整型变量 a,并初始化为 10 int a = 10; // 打印变量 a 的地址 printf("a的地址是:%p\n", &a); // 定义一个整型数组 b,并将 a 的值存入数组中 int b[] = {a}; // 定义一个整型指针 c,并将 a 的地址赋值给它 int * c[] = {&a}; // 下面的注释是对 b 和 c 的说明 // b, &b, &b[0]; // b 是数组名,&b 是数组的地址,&b[0] 是数组第一个元素的地址 // c, &c, &c[0]; // c 是指针,&c 是指针的地址,&c[0] 是指针指向的第一个元素的地址 // 定义一个指向 printf 函数的指针 funcs_d int (*funcs_d)(const char*, ...) = printf; // 指向printf函数的指针 // 定义一个数组 d,存放指向 printf 函数的指针 int (*d[])(const char*, ...) = {printf}; // 存放指向printf函数的指针的数组 return 0; // 返回 0,表示程序正常结束 }
练习2:递归
请用递归实现计算:1+1/3-1/5+1/7-1/9+…. 1/n 的值,n通过键盘输入
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
double rec_add(int n)
{
double sum=0;
if (n==1)
{
return 1;
}else
{
double sum=1.0/n;
if ((n/2)%2==0)
{
return sum+rec_add(n-2);
}else{
return -sum+rec_add(n-2);
}
}
return sum;
}
int main(int argc, const char *argv[])
{
int n=0;
printf("输入n(奇数):");
scanf("%d",&n);
if (n<=0 ||n%2==0)
{
printf("重新输入一个正奇数\n");
return 1;
}
double result=rec_add(n);
printf("result=%f\n",result);
return 0;
}
练习3:写一个双向链表的快速排序函数
#include <stdio.h>
#include <stdlib.h>
// 双向链表节点结构体
typedef struct Node {
int data;
struct Node *prev;
struct Node *next;
} Node;
// 创建新的节点
Node* create_node(int data) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->prev = new_node->next = NULL;
return new_node;
}
// 在链表尾部插入节点
void append(Node **head, int data) {
Node *new_node = create_node(data);
if (*head == NULL) {
*head = new_node;
} else {
Node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
new_node->prev = temp;
}
}
// 打印链表
void print_list(Node *head) {
Node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// 切分链表并返回基准节点
Node* partition(Node *low, Node *high) {
int pivot = high->data;
Node *i = low->prev;
Node *j = low;
while (j != high) {
if (j->data <= pivot) {
i = (i == NULL) ? low : i->next;
int temp = i->data;
i->data = j->data;
j->data = temp;
}
j = j->next;
}
i = (i == NULL) ? low : i->next;
int temp = i->data;
i->data = high->data;
high->data = temp;
return i;
}
// 快速排序递归函数
void quick_sort(Node *low, Node *high) {
if (low != NULL && high != NULL && low != high && low != high->next) {
Node *p = partition(low, high);
quick_sort(low, p->prev);
quick_sort(p->next, high);
}
}
// 获取链表的最后一个节点
Node* get_tail(Node *head) {
while (head != NULL && head->next != NULL) {
head = head->next;
}
return head;
}
int main() {
Node *head = NULL;
// 插入一些数据到链表
append(&head, 5);
append(&head, 3);
append(&head, 8);
append(&head, 4);
append(&head, 1);
append(&head, 7);
printf("Original list: ");
print_list(head);
Node *tail = get_tail(head);
// 执行快速排序
quick_sort(head, tail);
printf("Sorted list: ");
print_list(head);
return 0;
}