加油💕❤🤞
链表
单链表
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
Status InitLinkList(LinkList &H){
LinkList head;
head = (LinkList)malloc(sizeof(LNode));
head->next = NULL;
}
void Print_LinkList(LinkList &H){
LNode *p;
p = H->next;
while(p!= NULL){
printf("%d", p->data);
p = p->next;
}
}
栈
顺序栈
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}Sqstack;
Status initStack(Sqstack &S){
S.base = new SElemType[stack_INIT_SIZE];
S.top = S.base;
S.stacksize = stack_INIT_SIZE;
return OK;
}
Status push(Sqstack &S, SElemType x){
if(S.stacksize <= (S.top - S.base)){
S.base = (SElemType *)realloc(S.base, ((S.stacksize + stack_INIT_SIZE)));
if(S.base != NULL){
return ERROR;
}
S.top = S.base + S.stacksize;
S.stacksize = S.stacksize + stack_INIT_SIZE;
}
*(S.top++) = x;
return TRUE;
}
Status pop(Sqstack &S, SElemType &e){
if(S.base < S.top){
S.top--;
e = *S.top;
return e;
}
return ERROR;
}
Status print_stack(Sqstack S){
SElemType *p = S.base;
while(p < S.top){
printf("%c", *p);
p++;
}
return OK;
}
循环队列
typedef struct{
int *base;
int front; //队头
int rear; //队尾
}SqQueue;
void printQ(SqQueue Q){
int i;
i = Q.front;
while(i!= Q.rear){
printf("%d(%d)", Q.base[i], i);
i = (i+1) % N;
}
printf("\n");
}
void InitQ(SqQueue &Q, int N){
Q.base = new int[N];
Q.front = Q.rear = 0;
}
void AddQ(SqQueue &Q, int x){
if((Q.rear +1)%N == Q.front){
printf("Queue Full\n");
}else{
Q.rear = (Q.rear) % N;
Q.base[Q.rear] = x;
Q.rear++;
}
}
Status DeleteQ(SqQueue &Q, int &e){
if(Q.front == Q.rear){
printf("Queue Empty\n");
return ERROR;
}
e = Q.base[(Q.front)%N];
Q.front = (Q.front + 1) % N;
return OK;
}
链队列
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct Queue{
QueuePtr front; //队首
QueuePtr rear; //队尾
}LinkQueue;
Status InitLinkQueue(LinkQueue &Q){
Q.front = Q.rear = NULL;
return OK;
}
Status EnLinkQueue(LinkQueue &Q, QElemType e){
QueuePtr s;
s = new QNode;
if(QueueEmpty(Q)){
s->next = NULL;
s->data = e;
Q.rear = Q.front = s;
}else{
s->data = e;
s->next = NULL;
Q.rear->next = s;
Q.rear = s;
}
}
Status DeLinkQueue(LinkQueue &e, QElemType &e){
if(QueueEmpty(Q)){
return ERROR;
}
e = Q.front->data;
Q.front = Q.front->next;
return OK;
}
Status QueueEmpty(LinkQueue Q){
if(Q.front== NULL || Q.rear == NULL){
return OK;
}
return ERROR;
}
树
哈夫曼树
typedef struct{
int weight; //结点的权重
int parent, lchild, rchild; //结点的双亲,左孩子,右孩子下标
}HTNode, *HuffmanTree;
//选择权值最小的两棵树
void Select(HuffmanTree HT, int len, int &s1, int &s2){
int i, min1 = 32767, min2 = 32767;
for(i = 0; i <= len; i++){
if(HT[i].weight < min1 && HT[i].parent == 0){
s2= s1;
min2 = min1;
min1 = HT[i].weight;
s1 = i;
}else if(HT[i].w)
}
}