使用ADT描述一个抽象数据类型
只规定了使用和声明 不说明实现方式
将大小为0的表为空表(empty list)
表数据称 前一位为前驱 下一位为后继
链表
逻辑概念 分块存储 一个表结构中的 存储着下一个链表的内存地址
ADT 分为两块 逻辑方式 和实现方式
链表头文件声明
#pragma once
#include <iostream>
struct Node;
typedef struct Node* PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
typedef int ElemenType;
List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Position P, List L);
Position Find(ElemenType X, List L);
void Delete(ElemenType X, List);
Position FindPrevious(ElemenType X, List L);
void Insert(ElemenType X, List L, Position);
void DeleteList(List L);
Position Header(List L);
Position First(List L);
struct Node
{
ElemenType Element;
Position Next;
};
#include "linkList.h"
List MakeEmpty(List L)
{
return List();
}
int IsEmpty(List L)
{
return L->Next == NULL; //判断Next 是否等于NULL判断是否为空
}
int IsLast(Position P, List L)
{
return P->Next ==NULL; //判断是否为最后一个
}
Position Find(ElemenType X, List L) //返回存储X的List
{
Position P;
P = L->Next;
while (P !=NULL && P->Element != X)
{
P = P->Next;
}
return P;
}
void Delete(ElemenType X, List L) //删除链表
{
Position P, TmpCell;
P = FindPrevious(X, L);
if (!IsLast(P, L)) {
TmpCell = P->Next;
P->Next = TmpCell->Next;
free(TmpCell);
}
}
Position FindPrevious(ElemenType X, List L) //获取前一个链表
{
Position P;
P = L;
while (P->Next != NULL && P->Next->Element != X)
{
P = P->Next;
}
return P;
}
void Insert(ElemenType X, List L, Position P)
{
Position TmpCell;
TmpCell = (Position)malloc(sizeof(struct Node));
if (TmpCell == NULL) {
printf("out Of space");
return;
}
TmpCell->Element = X;
TmpCell->Next = P -> Next;
P->Next = NULL;
}
void DeleteList(List L)
{
}
Position Header(List L)
{
return Position();
}
Position First(List L)
{
return Position();
}
栈
栈
链表实现
#pragma once
#include <iostream>
struct Node;
typedef struct Node* PtrToNode;
typedef PtrToNode Stack;
int IsEmpty(Stack S);
Stack CreateStack(void);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(int X, Stack S);
int Top(Stack S);
void Pop(Stack S);
struct Node
{
int Element;
PtrToNode Next;
};
#include "MyStack.h"
int IsEmpty(Stack S)
{
return S->Next == NULL;
}
Stack CreateStack(void)
{
Stack S = (Stack)malloc(sizeof(struct Node));
if (S == NULL)
{
printf("Stack Failed to initialize memory");
return NULL;
}
S->Next = NULL;
return S;
}
void DisposeStack(Stack S)
{
if (S->Next==NULL)
{
printf("%d", S->Element);
return;
}
Stack P = S;
DisposeStack(S->Next);
printf("%d", P->Element);
return;
}
void MakeEmpty(Stack S)
{
S->Next = NULL;
return;
}
void Push(int X, Stack S)
{
Stack P = S;
while (P->Next!= NULL)
{
P = P->Next;
}
Stack tmp = CreateStack();
tmp->Element = X;
P->Next = tmp;
return;
}
int Top(Stack S)
{
Stack P = S;
while (P->Next!=NULL)
{
P = P->Next;
}
return P->Element;
}
void Pop(Stack S)
{
Stack P = S;
while (P->Next->Next != NULL)
{
P = P->Next;
}
free(P->Next);
P->Next = NULL;
return;
}
数组表示
#pragma once
#include <iostream>
struct StackRecord;
typedef struct StackRecord *Stack;
int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElements);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(int X, Stack S);
int Top(Stack S);
void Pop(Stack S);
int TopAndPop(Stack S);
struct StackRecord {
int Capacity;
int TopOfStack;
int* Array;
};
#include "MyStackArray.h"
int IsEmpty(Stack S)
{
return S->TopOfStack == -1;
}
int IsFull(Stack S)
{
return S->TopOfStack == S->Capacity;
}
Stack CreateStack(int MaxElements)
{
Stack s;
if (5 < MaxElements) {
printf("stack size is too small. \n");
return NULL;
};
s = (Stack)malloc(sizeof(Stack));
if (s == NULL) {
printf("Out of space! \n");
return NULL;
}
s->Array = (int*)malloc(sizeof(int) * 20);
if (s->Array == NULL)
{
printf("Out of space! \n");
return NULL;
}
return s;
}
void DisposeStack(Stack S)
{
}
void MakeEmpty(Stack S)
{
S->TopOfStack = -1;
}
void Push(int X, Stack S)
{
if (IsFull(S))
{
printf("full stack!");
}
else {
S->Array[++S->TopOfStack] = X;
}
}
int Top(Stack S)
{
return 0;
}
void Pop(Stack S)
{
if (IsEmpty(S))
{
printf("empty stack\n");
}
else {
S->TopOfStack--;
}
}
int TopAndPop(Stack S)
{
return 0;
}
迷宫算法
// DataStructure.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "MyStackArray.h"
bool MazeExit(int* currentPos, Stack RecordStack);
char maze[21][21] = {
"11111111111111111111",
"10100000000001110001",
"10111110111111000101",
"10100000100000010101",
"10111010111111010101",
"10001010100001010101",
"10101011111101010101",
"10101000000101010101",
"10101111111111010101",
"10101000000000010101",
"10101111111111010101",
"10100000000000010101",
"10111111111111010101",
"10100000000001010101",
"10111111111111010101",
"10100000000001010101",
"10111111111111010101",
"10100000000001010101",
"10111111111111110103",
"11111111111111111111"
};
bool IsSame(int* x1, int* x2) {
if (x1[0] == x2[0] && x1[1] == x2[1])
{
return true;
}
return false;
}
bool MazeExit(int* currentPos, Stack RecordStack) {
if (maze[currentPos[0]][currentPos[1]] == '3')
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 21; j++) {
if (maze[i][j] == '5')
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
std::cout << maze[i][j];
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else
{
std::cout << maze[i][j];
}
}
std::cout << "\n";
}
return true;
}
if (maze[currentPos[0] - 1][currentPos[1]] == '0' || maze[currentPos[0] - 1][currentPos[1]] == '3') //上
{
int destinationArray[2] = { currentPos[0],currentPos[1] };
destinationArray[0] -= 1;
Push(destinationArray, RecordStack);
}
if (maze[currentPos[0]][currentPos[1] + 1] == '0' || maze[currentPos[0]][currentPos[1] + 1] == '3') //右
{
int destinationArray[2] = { currentPos[0],currentPos[1] };
destinationArray[1] += 1;
Push(destinationArray, RecordStack);
}
//std::cout << (maze[currentPos[0]][currentPos[1] - 1] == '3') << std::endl;
if (maze[currentPos[0] + 1][currentPos[1]] == '0'|| maze[currentPos[0] + 1][currentPos[1]] == '3') //下
{
int destinationArray[2] = { currentPos[0],currentPos[1] };
destinationArray[0] += 1;
Push(destinationArray, RecordStack);
}
if (maze[currentPos[0]][currentPos[1] - 1] == '0' || maze[currentPos[0]][currentPos[1] - 1] == '3') //左
{
int destinationArray[2] = { currentPos[0],currentPos[1] };
destinationArray[1] -= 1;
Push(destinationArray, RecordStack);
}
maze[currentPos[0]][currentPos[1]] = '5';
while (!IsEmpty(RecordStack))
{
int* temp = Pop(RecordStack);
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 21; j++)
{
std::cout << maze[i][j];
}
printf("\n");
}
std::cout << "X:" << temp[0] << "\t" << "Y:" << temp[1] << std::endl;
if (MazeExit(temp, RecordStack)) {
break;
};
}
/*return true;*/
}
int main()
{
Stack s = CreateStack(30);
int currentPos[2] = { 1,1 };
MazeExit(currentPos, s);
}
#pragma once
#include <iostream>
#include <algorithm>
#include <windows.h>
struct StackRecord;
typedef struct StackRecord *Stack;
int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElements);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(int* X, Stack S);
int* Top(Stack S);
int* Pop(Stack S);
int TopAndPop(Stack S);
struct StackRecord {
int Capacity;
int TopOfStack;
int** Array;
};
#include "MyStackArray.h"
int IsEmpty(Stack S)
{
return S->TopOfStack == -1;
}
int IsFull(Stack S)
{
return S->TopOfStack == S->Capacity;
}
Stack CreateStack(int MaxElements)
{
Stack s;
s = (Stack)malloc(sizeof(Stack));
if (s == NULL) {
printf("Out of space! \n");
return NULL;
}
s->Array = (int**)malloc(sizeof(int*) * 20);
if (s->Array == NULL)
{
printf("Out of space! \n");
return NULL;
}
MakeEmpty(s);
return s;
}
void DisposeStack(Stack S)
{
}
void MakeEmpty(Stack S)
{
S->TopOfStack = -1;
}
void Push(int* X, Stack S)
{
if (IsFull(S))
{
printf("full stack!");
}
else {
S->Array[++S->TopOfStack] = X;
}
}
int* Top(Stack S)
{
return S->Array[S->TopOfStack];
}
int* Pop(Stack S)
{
if (IsEmpty(S))
{
printf("empty stack\n");
}
else {
int* temp = S->Array[S->TopOfStack];
S->TopOfStack--;
return temp;
}
}
int TopAndPop(Stack S)
{
return 0;
}