只有基础功能实现 给自己留着以后用
顺序表
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType; //假设线性表中的元素均为整型
typedef struct
{
ElemType *elem; //存储空间基地址
int length; //表中元素的个数
int listsize; //表容量大小
} SqList; //顺序表类型定义
Status ListInsert_Sq(SqList &L, int pos, ElemType e)
{
if (pos <= 0 || pos > L.listsize)
return OVERFLOW;
if (L.length >= L.listsize)
{
L.elem = (ElemType *)realloc(L.elem, sizeof(ElemType) * (LIST_INIT_SIZE + LISTINCREMENT));
if (!L.elem)
return OVERFLOW;
L.listsize += LISTINCREMENT;
}
for (int i = L.length + 1; i > pos; i--)
L.elem[i] = L.elem[i - 1];
L.elem[pos] = e;
L.length++;
return OK;
}
Status ListDelete_Sq(SqList &L, int pos, ElemType &e)
{
if (pos <= 0 || pos > L.listsize)
return OVERFLOW;
e = L.elem[pos];
for (int i = pos; i < L.length; i++)
L.elem[i] = L.elem[i + 1];
L.length--;
return OK;
}
int ListLocate_Sq(SqList L, ElemType e)
{
for (int i = 1; i <= L.length; i++)
if (L.elem[i] == e)
return i;
return ERROR;
}
void ListPrint_Sq(SqList L)
{
for (int i = 1; i <= L.length; i++)
if (i != 1)
printf(" %d", L.elem[i]);
else
printf("%d", L.elem[i]);
}
Status InitList_Sq(SqList &L)
{
//初始化L为一个空的有序顺序表
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem)
exit(OVERFLOW);
L.listsize = LIST_INIT_SIZE;
L.length = 0;
return OK;
}
单链表
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType coeff;
int index;
struct LNode *next;
} LNode, *LinkList;
Status ListCreate(LinkList &L)
{
LNode *rearPtr, *curPtr;
L = (LNode *)malloc(sizeof(LNode));
if (!L)
exit(OVERFLOW);
L->next = NULL;
rearPtr = L;
int n;
scanf("%d", &n);
ElemType val = 0;
for (int i = 0; i < n; i++) //读入n个数据
{
curPtr = (LNode *)malloc(sizeof(LNode));
if (!curPtr)
exit(OVERFLOW);
scanf("%d%d", &curPtr->coeff, &curPtr->index);
curPtr->next = NULL;
rearPtr->next = curPtr;
rearPtr = curPtr;
}
return OK;
}
void ListPrint(LinkList &L)
{
if (!L->next) //零多项式直接输出0 0
{
printf("0 0\n");
return;
}
for (LNode *p = L->next; p != NULL; p = p->next)
if (p->next != NULL)
printf("%d %d ", p->coeff, p->index);
else
printf("%d %d", p->coeff, p->index);
puts("");
}
栈
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define Status int
#define OK 1
#define OVERFLOW -1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 20
typedef char SElemType;
typedef struct Stack{
SElemType* top;
SElemType* base;
int stacksize;
}Stack;
const int N = 1e5 + 10;
Status InitStack(Stack& S)
{
S.base = (SElemType*)malloc(sizeof(SElemType) * STACK_INIT_SIZE);
if(!S.base) exit(OVERFLOW);
S.stacksize = STACK_INIT_SIZE;
S.top = S.base;
return OK;
}
Status Push(Stack& S, SElemType e)
{
if(S.top - S.base == S.stacksize)
{
S.base = (SElemType*)malloc(sizeof(SElemType) * (S.stacksize + STACKINCREMENT));
if(!S.base) exit(OVERFLOW);
S.top = S.stacksize + S.base;
S.stacksize += STACKINCREMENT;
}
*S.top = e;
S.top ++;
return OK;
}
Status Pop(Stack& S)
{
if(S.top == S.base) return ERROR;
S.top--;
return OK;
}
Status Empty(Stack& S)
{
if(S.top == S.base) return OK;
return ERROR;
}
SElemType GetTop(Stack& S)
{
if(S.top == S.base) return ERROR;
return *(S.top - 1);
}
队列
#include <iostream>
#include <queue>
using namespace std;
#define QElemType int
#define OVERFLOW -1
#define ERROR 0
#define OK 1
#define Status int
typedef struct QNode{
QElemType data;
struct QNode* next;
}QNode, *QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
void InitQueue(LinkQueue& q)
{
QNode* p = (QNode*)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->next = NULL;
q.front = p;
q.rear = q.front;
}
void EnQueue(LinkQueue& q, QElemType e)
{
QNode* p = (QNode*)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->data = e;
p->next = NULL;
q.rear->next = p;
q.rear = p;
}
bool QueueEmpty(LinkQueue q)
{
if(q.front == q.rear)
return true;
return false;
}
Status DeQueue(LinkQueue& q)
{
if(QueueEmpty(q)) return ERROR;
QNode* p = q.front;
q.front = q.front->next;
free(p);
p = NULL;
return OK;
}
QElemType GetFront(LinkQueue q)
{
return q.front->next->data;
}
二叉树
typedef int TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
Status CreateBiTree(BiTree &T)
{
TElemType e;
scanf("%d", &e);
if (e == 0)
T = NULL;
else
{
T = (BiTree)malloc(sizeof(BiTNode));
if (!T)
exit(OVERFLOW);
T->data = e;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}
int GetDepthOfBiTree ( BiTree T)
{
if(!T) return 0;
return 1 + std::max(GetDepthOfBiTree(T->lchild), GetDepthOfBiTree(T->rchild));
}
int LeafCount(BiTree T)
{
if(!T) return 0;
else if(!T->lchild && !T->rchild) return 1;
else
return LeafCount(T->lchild) + LeafCount(T->rchild);
}
int main()
{
BiTree T;
int depth, numberOfLeaves;
CreateBiTree(T);
depth = GetDepthOfBiTree(T);
numberOfLeaves = LeafCount(T);
printf("%d %d\n", depth, numberOfLeaves);
}