#include<iostream>
using namespace std;
typedef struct stack
{
int date;
stack* next;
};
int empty_stack(stack* top)
{
return (top == NULL);
}
stack* push_stack(stack* top, int date)
{
stack* p = new stack;
p->date = date;
p->next = top;
top = p;
return top;
}
void fun(stack *top,int i,int j) {
int t = 9 - i * 3 - j-1;
for (int n = 0; n < t; n++) {
top = top->next;
}
cout << top->date;
}
int main() {
int a[3][3] = {1,2,3,4,5,6,7,8,9};
stack *top=new stack;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
top = push_stack(top, a[i][j]);
}
}
fun(top,0, 0);
fun(top,0, 1);
fun(top, 0, 2);
fun(top, 1, 0);
fun(top, 1, 1);
fun(top, 1, 2);
fun(top, 2, 0);
fun(top, 2, 1);
fun(top, 2, 2);
}
```cpp
#include<iostream>
using namespace std;
typedef struct stack
{
int date;
stack* next;
};
int empty_stack(stack* top)
{
return (top == NULL);
}
stack* push_stack(stack* top, int date)
{
stack* p = new stack;
p->date = date;
p->next = top;
top = p;
return top;
}
int fun(stack* top, int i, int j) {
int t = 9 - i * 3 - j - 1;
for (int n = 0; n < t; n++) {
top = top->next;
}
return top->date;
}
int find(stack *top,int n) {
int sum = 0;
for (int i = 0; i < 3&&i+1<3; i++) {
if (i == n)continue;
cout<< fun(top, i, i + 1)<<" ";
sum = sum + fun(top, i, i+1);
}
if (n != 2) {
cout<< fun(top, 2, n)<<" ";
sum=sum+ fun(top, 2, n);
}
return sum;
}
int main() {
int a[3][3] = { 1,2,3,4,5,6,7,8,9 };
stack* top = new stack;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
top = push_stack(top, a[i][j]);
}
}
cout <<find(top, 1) << endl;
cout<<find(top, 2)<<endl;
cout<<find(top, 0)<<endl;
}
这里是从左到右(从上到下)依次入栈。我还是不够理解题目,值给代码吧,应该很容易看懂的