一.链栈
//链栈的初始化(带头节点)
bool initiationStack(LinkStack &S) {
S = (LinkStackNode*)malloc(sizeof(LinkStackNode));
if (!S) { return false; }
S->link = NULL;
return true;
}
//链栈的初始化(不带头节点)
bool initiationStackNoHead(LinkStack& S) {
S = NULL;
return true;
}
//判空操作(带头节点)
bool isEmptyS(LinkStack S) {
if (S->link == NULL) {
return true;
}
return false;
}
//判空操作(不带头节点)
bool isEmptySNoHead(LinkStack S) {
if (S == NULL) {
return true;
}
return false;
}
//元素入栈操作(带头节点)
bool pushStack(LinkStack S,ElemType x) {
LinkStackNode* p = (LinkStackNode*)malloc(sizeof(LinkStackNode));
if (!p) { return false; }
p->data = x;
p->link = S->link;
S->link = p;
return true;
}
//元素入栈操作(不带头节点)
bool pushStackNoHead(LinkStack S, ElemType x) {
LinkStackNode* p = (LinkStack)malloc(sizeof(LinkStackNode));
if (!p) { return false; }
p->data = x;
if (S == NULL) {
p = S;
p->link = NULL;
}
else {
p->link = S;//易错
S = p;
}
return true;
}
//元素出栈操作(带头节点)
bool popStack(LinkStack S, ElemType& e) {
if (S->link == NULL) {
return false;
}
LinkStack p = S->link;
e = p->data;
S->link = p->link;
free(p);
return true;
}
//元素出栈操作(不带头节点)
bool popStackNoHead(LinkStack S, ElemType& e) {
if (S == NULL) {
return false;
}
LinkStackNode* p = S;
e = p->data;
S = p->link;
free(p);
return true;
}
//读栈顶元素(带头节点)
ElemType GetTop(LinkStack S) {
if (S->link == NULL) {
return false;
}
LinkStack p = S->link;
return p->data;
}
//读栈顶元素(不带头节点)
ElemType GetTopNoHead(LinkStack S) {
if (S == NULL) {
return false;
}
LinkStackNode* p = S;
return p->data;
}
二.链队
//链队列的初始化(带头节点)
void initQueue(LinkQueue &Q) {
Q.front = Q.rear = (QueueNode*)malloc(sizeof(QueueNode));
Q.front->link = NULL;
}
//链队列的初始化(不带头节点)
void initQueueNoHead(LinkQueue& Q) {
Q.front = NULL;
Q.rear = NULL;
}
//链队列判空操作(带头节点)
bool isEmptyQueue(LinkQueue& Q) {
if (Q.front == Q.rear) { return true; }
return false;
}
//链队列判空操作(不带头节点)
bool isEmptyQueueNoHead(LinkQueue& Q) {
if (Q.front == NULL) {
return true;
}
return false;
}
//元素入队操作(带头节点)
bool EnQueue(LinkQueue& Q, ElemType x) {
QueueNode* s = (QueueNode*)malloc(sizeof(QueueNode));
if (!s) { return false; }
s->data = x;
s->link = NULL;
Q.rear->link = s;
Q.rear = s;
return true;
}
//元素入队操作(不带头节点)
bool EnQueueNoHead(LinkQueue& Q, ElemType x) {
QueueNode* s = (QueueNode*)malloc(sizeof(QueueNode));
if (!s) { return false; }
s->data = x;
if (Q.front== NULL) {
Q.front = s;
Q.rear = s;
Q.front->link = NULL;
}
else {
Q.rear->link = s;
Q.rear = s;
}
return true;
}
//队头元素出队(带头节点)
bool DeQueue(LinkQueue& Q, ElemType& e) {
if (Q.front == Q.rear) { return false; }
QueueNode* p = Q.front->link;
e = p->data;
Q.front->link = p->link;
if (Q.rear == p) {
Q.rear == Q.front;
}
free(p);
return true;
}
//队头元素出队(不带头节点)
bool DeQueueNoHead(LinkQueue& Q, ElemType& e) {
if (Q.front == NULL) {
return false;
}
QueueNode* p = Q.front;
e = p->data;
Q.front = p->link;
if (p == Q.rear) {
Q.rear = Q.front = NULL;//易错
}
free(p);
return true;
}
三.结构体定义
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
#define MAXSIZE 40
typedef struct QueueNode {
ElemType data;
struct QueueNode* link;
}QueueNode;
typedef struct LinkQueue {
QueueNode* rear;
QueueNode* front;
}LinkQueue;
typedef struct LinkStack {
ElemType data;
struct LinkStack* link;
}LinkStackNode,*LinkStack;
typedef struct LinkNode {
ElemType data;
struct LinkNode* link;
}*Linklist,LinkNode;
typedef struct DinkNode {
ElemType data;
struct DinkNode* Link, * prior;
}DinkNode,*DLinkList;
void printWithNoLeader(Linklist L) {
Linklist p = L;
while (p) {
printf("%3d", p->data);
p = p->link;
}
}
void print(Linklist L) {
Linklist p = L->link;
while (p) {
printf("%3d", p->data);
p = p->link;
}
}