线性表
栈&队列
串
二叉树
持续更新中,尽请期待!!!
栈的顺序存储结构代码实现
#include<bits/stdc++.h>
using namespace std;
#define SElemType int
#define Status int
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
Status InitStack(SqStack &s){
s.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!s.base)
exit(_OVERFLOW);
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return 1;
}
Status DestoryStack(SqStack &s){
free(s.base);
s.base = NULL;
s.top = NULL;
s.stacksize = 0;
return 1;
}
Status ClearStack(SqStack &s){
if(!s.base)
return 0;
s.top = s.base;
return 1;
}
Status StackEmpty(SqStack s){
if(s.top==s.base)
return 1;
return 0;
}
int StackLength(SqStack s){
if(!s.base)
return 0;
return (int)(s.top - s.base);
}
Status GetTop(SqStack s,SElemType &e){
if(s.base==s.top)
return 0;
e = *(s.top - 1);
return 1;
}
Status Push(SqStack &s,SElemType e){
if(!s.base)
return 0;
if (s.top - s.base >= s.stacksize){
s.base = (SElemType *)realloc(s.base, (s.stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!s.base)
exit(_OVERFLOW);
s.top = s.base + s.stacksize;
s.stacksize += STACKINCREMENT;
}
*s.top++ = e;
return 1;
}
Status Pop(SqStack &s,SElemType &e){
if(!s.base||s.top==s.base)
return 0;
e = *--s.top;
return 1;
}
void visit(SElemType e){
cout << e << " ";
}
Status StackTraverse(SqStack s,void (*visit)(SElemType)){
SElemType *p = s.base;
if(!s.base)
return 0;
while (p < s.top)
visit(*p++);
cout << "\n";
return 1;
}
void conversion(){
SqStack S;
InitStack(S);
SElemType N, e;
scanf("%d", &N);
while(N){
Push(S, N % 8);
N /= 8;
}
while(!StackEmpty(S)){
Pop(S, e);
cout << e;
}
}
void LineEdit(){
SqStack S;
InitStack(S);
SElemType c;
char ch = getchar();
while (ch != EOF){
while (ch != EOF && ch != '\n'){
switch (ch){
case '#':
Pop(S, c);
break;
case '@':
ClearStack(S);
break;
default:
Push(S, ch);
}
ch = getchar();
}
ClearStack(S);
if (ch != EOF)
ch = getchar();
}
DestoryStack(S);
}
int C = 0;
void move(char x, int n, char z) {
cout << ++C << ". Move disk " << n << " from " << x << " to " << z << "."<<endl;
}
void hanoi(int n, char x, char y, char z) {
if (n == 1)
move(x, 1, z);
else {
hanoi(n - 1, x, z, y);
move(x, n, z);
hanoi(n - 1, y, x, z);
}
}
int main(){
SqStack s;
cout << "InitStack" << endl;
InitStack(s);
cout << "StackEmpty" << endl;
StackEmpty(s) ? cout << "yes\n" : cout << "no\n";
cout << "Push" << endl;
for (int i = 1; i <= 6; i++)
Push(s, i);
cout << "StackTraverse" << endl;
StackTraverse(s, visit);
cout << "StackLength" << endl;
cout << StackLength(s) << endl;
cout << "Pop" << endl;
SElemType e;
Pop(s, e);
cout << e << endl;
StackTraverse(s, visit);
cout << "GetTop" << endl;
GetTop(s, e);
cout << e << endl;
return 0;
}
算法3.3 迷宫求解【顺序栈代码实现】
#include<bits/stdc++.h>
using namespace std;
#define Status int
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int DirectiveType;
#define RANGE 100
#define ROW 10
#define COL 10
#define step ord
typedef struct{
int m, n;
int arr[RANGE][RANGE];
} MazeType;
typedef struct{
int row;
int col;
} PosType;
typedef struct
{
int ord;
PosType seat;
int di;
} SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
Status InitStack(SqStack &s){
s.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!s.base)
exit(_OVERFLOW);
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return 1;
}
Status DestoryStack(SqStack &s){
free(s.base);
s.base = NULL;
s.top = NULL;
s.stacksize = 0;
return 1;
}
Status ClearStack(SqStack &s){
if(!s.base)
return 0;
s.top = s.base;
return 1;
}
Status StackEmpty(SqStack s){
if(s.top==s.base)
return 1;
return 0;
}
int StackLength(SqStack s){
if(!s.base)
return 0;
return (int)(s.top - s.base);
}
Status GetTop(SqStack s,SElemType &e){
if(s.base==s.top)
return 0;
e = *(s.top - 1);
return 1;
}
Status Push(SqStack &s,SElemType e){
if(!s.base)
return 0;
if (s.top - s.base >= s.stacksize){
s.base = (SElemType *)realloc(s.base, (s.stacksize + STACKINCREMENT) * sizeof(SElemType));
if(!s.base)
exit(_OVERFLOW);
s.top = s.base + s.stacksize;
s.stacksize += STACKINCREMENT;
}
*s.top++ = e;
return 1;
}
Status Pop(SqStack &s,SElemType &e){
if(!s.base||s.top==s.base)
return 0;
e = *--s.top;
return 1;
}
Status StackTraverse(SqStack s,void (*visit)(SElemType)){
SElemType *p = s.base;
if(!s.base)
return 0;
while (p < s.top)
visit(*p++);
cout << "\n";
return 1;
}
bool InitMaze(MazeType &maze, int a[ROW][COL], int row, int col)
{
int i, j;
for (i = 1; i<row - 1;i++){
for (j = 1; j<col - 1; j++)
{
maze.arr[i][j] = a[i][j];
}
}
for (j = 0; j<row; j++)
maze.arr[0][j] = maze.arr[row - 1][j] = 1;
for (i = 0; i<col; i++)
maze.arr[i][0] = maze.arr[i][col - 1] = 1;
return true;
}
bool Pass(MazeType maze, PosType curpos){
if (maze.arr[curpos.row][curpos.col]== 0)
return true;
else
return false;
}
bool FootPrint(MazeType &maze, PosType curpos){
maze.arr[curpos.row][curpos.col] = 2;
return true;
}
bool MarkPrint(MazeType &maze, PosType curpos){
maze.arr[curpos.row][curpos.col] = 3;
return true;
}
SElemType CreateSElem(int step, PosType pos, int di){
SElemType e;
e.step = step;
e.seat = pos;
e.di = di;
return e;
}
PosType NextPos(PosType curpos, DirectiveType di){
PosType pos = curpos;
switch (di){
case 1:
pos.col++;
break;
case 2:
pos.row++;
break;
case 3:
pos.col--;
break;
case 4:
pos.row--;
break;
}
return pos;
}
bool PosEqual(PosType pos1, PosType pos2){
if (pos1.row == pos2.row&& pos1.col == pos2.col)
return true;
else
return false;
}
void PrintMaze(MazeType maze, int row, int col){
int i, j;
printf(" ");
for (i = 0; i<col; i++)
printf("%d ", i);
printf("\n");
for (i = 0; i<row; i++){
printf("%d",i);
for (j = 0; j<col; j++){
switch (maze.arr[i][j]){
case 0:
printf(" ");
break;
case 1:
printf("■");
break;
case 2:
printf("*");
break;
case 3:
printf("@");
break;
default:
break;
}
}
printf("\n");
}
}
Status MazePath(MazeType &maze, PosType start, PosType end){
SqStack s;
SElemType e;
InitStack(s);
PosType curpos = start;
int curstep = 1;
do {
if (Pass(maze, curpos)){
FootPrint(maze,curpos);
e = CreateSElem(curstep,curpos, 1);
Push(s, e);
if (PosEqual(curpos,end))
return true;
curpos = NextPos(curpos,1);
return true;
curstep++;
}
else{
if (!StackEmpty(s)){
Pop(s, e);
while (e.di == 4&& !StackEmpty(s)){
MarkPrint(maze,e.seat);
Pop(s, e);
}
if (e.di<4){
e.di++;
Push(s, e);
curpos =NextPos(e.seat, e.di);
}
}
}
} while (!StackEmpty(s));
return 0;
}
算法3.4 表达式求值【链式栈代码实现】
#include<bits/stdc++.h>
using namespace std;
#define SElemType char
#define Status int
typedef char OperandType;
typedef struct SNode {
SElemType data;
struct SNode *next;
} SNode, *LinkStack;
Status InitStack(LinkStack &S) {
S = (LinkStack)malloc(sizeof(SNode));
if(!S)
exit(_OVERFLOW);
S->next = NULL;
return 1;
}
Status DestroyStack(LinkStack &S) {
LinkStack p = S->next, ptmp;
while(p) {
ptmp = p->next;
free(p);
p = ptmp;
}
free(S);
return 1;
}
Status ClearStack(LinkStack &S) {
LinkStack p = S->next, ptmp;
while(p) {
ptmp = p->next;
free(p);
p = ptmp;
}
S->next = NULL;
return 1;
}
Status StackEmpty(LinkStack S) {
if(S->next == NULL)
return 1;
else
return 0;
}
int StackLength(LinkStack S) {
int n = 0;
LinkStack p = S->next;
while(p) {
n++;
p = p->next;
}
return n;
}
Status GetTop(LinkStack S, SElemType &e) {
if ( S->next == NULL )
return 0;
e = S->next->data;
return 1;
}
Status Push(LinkStack &S, SElemType e) {
LinkStack p = (LinkStack)malloc(sizeof(SNode));
p->data = e;
p->next = S->next;
S->next = p;
return 1;
}
Status Pop(LinkStack &S, SElemType &e) {
if (S->next == NULL)
return 0;
e = S->next->data;
LinkStack ptmp = S->next->next;
free(S->next);
S->next = ptmp;
return 1;
}
Status visit(SElemType e) {
printf(" %c\n", e);
return 1;
}
Status StackTraverse(LinkStack S, Status (*visit)(SElemType)) {
if(S->next == NULL){
printf("栈为空!\n");
return 0;
}
for(int i=StackLength(S); i>0; i--){
LinkStack p = S->next;
int j = 1;
while ( p && j<i ) {
p = p->next;
++j;
}
visit(p->data);
}
printf("\n");
return 1;
}
Status StackTraverse_Top(LinkStack S, Status (*pfn_visit)(SElemType)) {
if(S->next == NULL){
printf("栈为空!\n");
return 0;
}
LinkStack p = S->next;
while(p) {
visit(p->data);
p = p->next;
}
printf("\n");
return 1;
}
SElemType Precede(SElemType t1, SElemType t2){
SElemType t;
switch (t1){
case '+':
case '-':
if(t2=='*' || t2=='/' || t2=='(')
t = '<';
else t = '>';
break;
case '*':
case '/':
if(t2 == '(')
t = '<';
else t = '>';
break;
case '(':
if(t2 == ')')
t = '=';
else if(t2 == '#')
return 0;
else t = '<';
break;
case ')':
if(t2 == '(')
return 0;
else t = '>';
break;
case '#':
if(t2 == ')')
return 0;
else if(t2 == '#')
t = '=';
else t = '<';
break;
}
return t;
}
SElemType Operator(SElemType a, SElemType theta, SElemType b){
SElemType ret;
switch(theta){
case '+':
ret = (a-48) + (b-48) + 48;
break;
case '-':
ret = (a-48) - (b-48) + 48;
break;
case '*':
ret = (a-48) * (b-48) + 48;
break;
case '/':
ret = (a-48) / (b-48) + 48;
break;
}
return ret;
}
Status In(SElemType c){
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':
case '=':
return 1;
break;
default:
return 0;
}
}
OperandType ExvaluateExpression()
{
LinkStack OPTR;
LinkStack OPND;
char c, x, theta, a, b, e;
InitStack(OPTR);
InitStack(OPND);
Push(OPTR, '#');
printf("输入表达式:\n");
c = getchar();
GetTop(OPTR, e);
while (c!='#' || e!='#'){
if (!In(c)) {
Push(OPND, c);
c = getchar();
}
else{
GetTop(OPTR, e);
switch (Precede(e, c)){
case '<':
Push(OPTR, c);
c = getchar();
break;
case '=':
Pop(OPTR, x);
c = getchar();
break;
case '>':
Pop(OPTR, theta);
Pop(OPND, b);
Pop(OPND, a);
Push(OPND, Operator(a, theta, b));
break;
}
}
GetTop(OPTR, e);
}
GetTop(OPND, e);
return e;
}
int main(){
char c;
do {
fflush(stdin);
c = ExvaluateExpression();
printf("表达式的值:");
printf("%d\n", c-48);
printf("是否继续(y/n):");
} while(scanf("%s", &c)!=0 && (c=='y' || c=='Y'));
return 0;
}