1、问题描述
输入参数n n 为正整数,如输入n=5n=5,则按行打印如下的数字:
2、问题的理解
这个问题是将数字1…n2 1 … n 2 按照一圈一圈的方式存储好,再按照行的方式对其进行打印。
3、解决的方法
最简单的方法是利用数组:
- 声明一个二维数组[n][n]
- 按照一圈一圈的方式向数组中添加对应数字
- 再按照一行一行的方式打印
这个方法比较简单,就不给出代码了。
4、问题升级
有人觉得上述的问题没什么难度,现在对问题进行升级。
使用链表的方式,不得使用数组。最终按行打印出来。(纯链表的操作)
5、解决的方法
由于本问题并不难,只是有些麻烦,利用这个问题,可以补习C语言中的指针的操作。方法有很多,在这里我给出我自己的方法,不见得是最简单的方法,若有简单的方法大家可以试试,我的方法主要分为以下几步:
- 对每个节点声明结构体,结构体中的内容包括:数值,指向上、下、左、右四个方向的指针;
- 函数1:实现一圈的节点关系和数值的设置;
- 函数2:通过循环调用函数1将所有节点联系起来;
- 函数3:按行打印。
下面是我的结果截图:
n=1
n
=
1
n=2
n
=
2
n=3
n
=
3
n=6
n
=
6
n=8
n
=
8
以下是我实现的程序代码,仅供参考:
#include<stdio.h>
#include<stdlib.h>
typedef struct node node;
struct node{
int value;
node *nextRightNode;
node *nextLeftNode;
node *nextUpNode;
node *nextDownNode;
};
node* setMatrix(int start, int n){
node* p_left_up = (node*)malloc(sizeof(node));
p_left_up->value = start;
if (n < 2){
p_left_up->nextRightNode = NULL;
p_left_up->nextLeftNode = NULL;
p_left_up->nextUpNode = NULL;
p_left_up->nextDownNode = NULL;
}else{
node* p_right_up = (node*)malloc(sizeof(node));
p_right_up->value = 1 * (n - 1) + p_left_up->value;
node* p_right_down = (node*)malloc(sizeof(node));
p_right_down->value = 1 * (n - 1) + p_right_up->value;
node* p_left_down = (node*)malloc(sizeof(node));
p_left_down->value = 1 * (n - 1) + p_right_down->value;
//设置
p_left_up->nextRightNode = p_right_up;
p_left_up->nextLeftNode = NULL;
p_left_up->nextUpNode = NULL;
p_left_up->nextDownNode = p_left_down;
p_right_up->nextRightNode = NULL;
p_right_up->nextLeftNode = p_left_up;
p_right_up->nextUpNode = NULL;
p_right_up->nextDownNode = p_right_down;
p_right_down->nextRightNode = NULL;
p_right_down->nextLeftNode = p_left_down;
p_right_down->nextUpNode = p_right_up;
p_right_down->nextDownNode = NULL;
p_left_down->nextRightNode = p_right_down;
p_left_down->nextLeftNode = NULL;
p_left_down->nextUpNode = p_left_up;
p_left_down->nextDownNode = NULL;
//设置中间的值
//p_left_up=p_right_up
node* p_1 = p_left_up;
int i = 0;
for (i = 0; i < (n-2); i++){
node* p = (node*)malloc(sizeof(node));
p->value = p_1->value + 1;
p->nextRightNode = p_right_up;
p_1->nextRightNode = p;
p->nextLeftNode = p_1;
p_right_up->nextLeftNode = p;
p_1 = p;
}
//p_left_down-->p_left_up
//p_left_down-->p_right_down
node* p_2 = p_left_down;
node* p_3 = p_left_down;
for (i = 0; i < (n-2); i++){
//up
node* p_up = (node*)malloc(sizeof(node));
p_up->value = p_2->value + 1;
p_up->nextUpNode = p_left_up;
p_2->nextUpNode = p_up;
p_up->nextDownNode = p_2;
p_left_up->nextDownNode = p_up;
p_2 = p_up;
//right
node* p_right = (node*)malloc(sizeof(node));
p_right->value = p_3->value - 1;
p_right->nextRightNode = p_right_down;
p_3->nextRightNode = p_right;
p_right->nextLeftNode = p_3;
p_right_down->nextLeftNode = p_right;
p_3 = p_right;
}
//p_right_up-->p_right_down
node* p_4 = p_right_up;
node* r = p_left_up;
for (i = 0; i < (n-2); i++){
node* p = (node*)malloc(sizeof(node));
p->value = p_4->value + 1;
r = r->nextDownNode;
p->nextDownNode = p_right_down;
p_4->nextDownNode = p;
p->nextUpNode = p_4;
p_right_down->nextUpNode = p;
r->nextRightNode = p;
p->nextLeftNode = r;
p_4 = p;
}
}
return p_left_up;
}
node* setAll(int start, int n){
node* p = NULL;
if (n <= 0){
printf("please input correct n\n");
}else{
p = setMatrix(start, n);
n = n - 2;
node* p_tmp = p;
while (n > 0){
node* p_down = p_tmp->nextDownNode;
start = p_down->value + 1;
node* f = setMatrix(start, n);
node* p_1 = f;
//deal
int i = 0;
for (i = 0; i < n; i++){
node* r = p_down;
node* r_2 = p_1;
while (r_2->nextRightNode != NULL){
r_2 = r_2->nextRightNode;
}
r_2->nextRightNode = r->nextRightNode;
r->nextRightNode->nextLeftNode = r_2;
r->nextRightNode = p_1;
p_1->nextLeftNode = r;
p_down = p_down->nextDownNode;
p_1 = p_1->nextDownNode;
}
n = n - 2;
p_tmp = f;
}
}
return p;
}
void printMatrix(node* p){
node* q = p;
while (q != NULL){
node* r = q;
while (r != NULL){
printf("%d\t", r->value);
r = r->nextRightNode;
}
printf("\n");
q = q->nextDownNode;
}
}
int main(int argc, char** argv){
if (argc != 2){
printf("useage : int n\n");
exit(0);
}
int n = atoi(argv[1]);
//node* p = setMatrix(25, n);
node* p = setAll(1, n);
printMatrix(p);
return 0;
}