Experiment 2 stack and queue
First, the experimental purpose
1. Familiar with the order and chain storage structure of stacks and queues
2. Master the basic operation of the stack and queue
3. Ability to use the basic operations of the stack and queue to complete the operation of the stack and queue application
Second, the experiment content
1. Setting the N identical, try a write write algorithm, determines whether the string has a central symmetrical relationship, such as XYZZYX is a central symmetrical string. (Tip: Put half character in a single-strand table first in the stack, and then make a comparison with the other half character in the single-strand table in turn.) (Folder: exercise 10_3)
Typedef char dattype;
/ * Define single-chain table structure type * /
Typedef struct node
{DataType Data;
Struct Node * Next;
linklist;
/ * Define the order stack structure type * /
/ * const Int maxSize = 40; * /
#define maxSize 40
Typedef struct
{Datatype Elements [MAXSIZE];
Int top;
} stack;
#include
#include
#include "project_10_3.h"
Int Symmetry (Linklist * HEAD, STACK * S);
Int main (int Argc, char * argv [])
{
Linklist * head;
Stack * S;
DataType Str [80];
/ *
CIN >> STR;
* /
Gets (STR);
HEAD = CREAT (STR);
PrintLink (HEAD);
SetNULL (S); Printf ("% D / N", Length (HEAD));
/ *
IF (Symmetry (HEAD, S)) cout << "String /" << Str << "/" Center Symmetrical / N ";
Else Cout << "String /" << Str << "/" Not a Center Symmetric / N ";
* /
IF (Symmetry ("String /"% s / "center symmetrical ./n" ,st);
Else Printf ("string /"% s / "is not a central symmetry ./n" ,st);
PUTS (STR);
System ("pause");
Return 0;
}
#include
#include
#include "project_10_3.h"
/ * Establish a single-link with head nodes * /
LinkList * Creat (Datatype * STR)
{DATATYPE * P = STR;
Linklist * head;
Linklist * s, * r;
Head = (linklist *) Malloc (SIZEOF (Linklist));
R = head;
While (* p! = '/ 0')
{
S = (linklist *) malloc (sizeof (linklist));
S-> DATA = * P;
R-> Next = S;
R = S;
P ;
}
R-> next = null;
Return head;
}
/ * Single-link surface length * /
INT Length (Linklist * Head) {
Linklist * p = head-> next;
INT n = 0;
While (p! = null)
{
N ;
P = P-> next;
}
Return n;
}
/ * Output single-link table * /
Void PrintLink (Linklist * HEAD)
{LinkList * P = head-> next;
While (p! = null)
{
/ * Cout << p-> data; * /
Printf ("% C", P-> Data);
P = P-> next;
}
/ * cout << Endl; * /
Printf ("/ n");
Return;
}
#include
#include
#include "project_10_3.h"
/ * Stack empty * /
Void setnull (stack * s)
{
/ * s = new stack; * /
s = (stack *) malloc (sizeof);
S-> TOP = -1;
Return;
}
/ * Sequential stack into the stack .h * /
Void Push (Stack * S, DataType E)
{
S-> TOP ;
S-> Elements [S-> TOP] = E;
Return;
}
/ * Sequential stack out stack * /
DataType Pop (stack * s)
{
DataType Temp;
Temp = S-> Elements [S-> TOP];
S-> TOP -;
Return Temp;
}
#include
#include
#include "project_10_3.h"
Int Symmetry (Linklist * HEAD, Stack * S)
{
INT N = Length (HEAD) / 2;
Linklist * p = head-> next;
INT I;
For (i = 0; i Push (S, P-> DATA); P = P-> next; } IF (Length (Head% 2 == 1) P = P-> Next; While (p! = null) { IF (POP (S) == P-> DATA) { P = P-> next; Continue; } Else Return 0; } Return 1; }