Question type: Simulated question
Test: Data Structure
Subject experience: 1tle, 2y
Solution:
Use the chain stack to answer the question and simulate the full operation...
Code:
# Include <stdio. h>
# Include <string. h>
# Include <malloc. h>
Int num;
Typedef struct Node
{
Int data;
Struct node * next;
} Node, * stacklist; // chain Stack
Stacklist s [25];
Int create (stacklist & L)
{
L = (stacklist) malloc (sizeof (node ));
L-> next = NULL;
Return 1;
} // Create a chain Stack
Int pos (int)
{
Int I;
For (I = 0; I <num; I ++)
{
Int flag = 0;
Stacklist P, Q, R;
P = s [I];
While (P)
{
If (p-> DATA =)
{Flag = 1; break ;}
P = p-> next;
}
If (FLAG) return I;
}
Return-1;
} // Locate the stack
Int moveonto (int A, int B)
{
Int I, j, n, m;
Stacklist P, Q, R;
If (A = B) Return 0;
M = pos (a); n = pos (B );
If (M = N) return 0;
P = s [m];
While (P)
{
If (p-> data! =)
{Q = p-> next; P-> next = s [p-> data]; s [p-> data] = P; P = Q ;}
Else break; // remember to add break or else ....
}//
Q = s [N];
While (q)
{
If (Q-> data! = B)
{R = Q-> next; q-> next = s [q-> data]; s [q-> data] = Q; q = r ;}
Else break;
}
S [m] = p-> next;
P-> next = Q;
S [N] = P;
Return 1;
}
Int moveover (int A, int B)
{
Int I, j, n, m;
Stacklist P, Q, R;
If (A = B) Return 0;
M = pos (a); n = pos (B );
If (M = N) return 0;
P = s [m];
While (P)
{
If (p-> data! =)
{Q = p-> next; P-> next = s [p-> data]; s [p-> data] = P; P = Q ;}
Else break;
}
S [m] = p-> next;
P-> next = s [N];
S [N] = P;
Return 1;
}
Int pileonto (int A, int B)
{
Int I, j, n, m;
Stacklist P, Q, R;
If (A = B) Return 0;
M = pos (a); n = pos (B );
If (M = N) return 0;
Q = s [N];
While (q)
{
If (Q-> data! = B)
{R = Q-> next; q-> next = s [q-> data]; s [q-> data] = Q; q = r ;}
Else break;
}
R = P = s [m];
While (P)
{
If (p-> DATA = A) break;
P = p-> next;
}
S [m] = p-> next;
P-> next = Q;
S [N] = R;
Return 1;
}
Int pileover (int A, int B)
{
Int I, j, n, m;
Stacklist P, Q, R, T;
If (A = B) Return 0;
M = pos (a); n = pos (B );
If (M = N) return 0;
R = P = s [m];
While (P)
{
If (p-> data! = A) P = p-> next;
Else break;
}
S [m] = p-> next;
P-> next = s [N];
S [N] = R;
Return 1;
}
Int main ()
{
Int I;
Scanf ("% d", & num );
For (I = 0; I <num; I ++)
{
Create (s [I]);
S [I]-> DATA = I;
}
While (1)
{
Int A, B;
Char Command [50], COM1 [20], com2 [20];
Scanf ("% s", COM1 );
If (COM1 [0] = 'q') break;
Scanf ("% d % S % d", & A, com2, & B );
If (COM1 [0] = 'M' & com2 [1] = 'n') moveonto (A, B );
Else if (COM1 [0] = 'M' & com2 [1] = 'V') moveover (A, B );
Else if (COM1 [0] = 'P' & com2 [1] = 'V') pileover (A, B );
Else if (COM1 [0] = 'P' & com2 [1] = 'n') pileonto (A, B );
// Printf ("###/N ");
}
For (I = 0; I <num; I ++)
{
If (s [I] = NULL)
Printf ("% d:/N", I );
Else
{
Printf ("% d:", I );
Int temp [50];
Int J = 0;
Stacklist P = s [I];
While (P)
{
Temp [J ++] = p-> data;
P = p-> next;
}
For (j = J-1; j> = 0; j --)
Printf ("% d", temp [J]);
Printf ("/N ");
}
}
Return 0;
}
It seems that this question can be done with the ordinary stack operation, worship