链表、栈、队列的基本操作
原创
©著作权归作者所有:来自51CTO博客作者liangliang凉凉的原创作品,请联系作者获取转载授权,否则将追究法律责任
栈的基本操作
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define MaxSize 100
typedef int ElemType;
//定义栈_顺序栈
struct Stack
{
ElemType* top;
ElemType* base;
int stacksize;
};
int IsFull(Stack s);
int IsEmpty(Stack s);
//初始化
int InitStack(Stack& s)
{
s.base = new ElemType[MaxSize];
if (!s.base)
return ERROR;
s.top = s.base;
s.stacksize = MaxSize;
return OK;
}
//入栈
int PushbackStack(Stack& s, ElemType e)
{
//判断栈是否满
if (IsFull(s))
return ERROR;
*(s.top) = e;//error
s.top++;
return OK;
}
//出栈
int Popback(Stack& s, ElemType& e)
{
//判断栈是否空
if (IsEmpty(s))
return ERROR;
s.top--;
return OK;
}
//判满
int IsFull(Stack s)
{
if (s.top - s.base == s.stacksize)
return 1;
return 0;
}
//判空
int IsEmpty(Stack s)
{
if (s.base == s.top)
return 1;
return 0;
}
//获取栈顶元素
ElemType StackTop(Stack s)
{
//判断栈是否空
if (IsEmpty(s))
return ERROR;
return *(s.top - 1);
}
//清除栈
int ClearStack(Stack& s)
{
s.top = s.base;
return OK;
}
int Destroy(Stack& s)
{
delete s.base;
s.top = s.base = NULL;
return OK;
}
链表的基本操作
#include <iostream>
#include<cassert>
#include <iostream>
#include <string>
#include <cassert>
#include<iomanip>
using namespace std;
//图书
typedef struct Book
{
string no;
string name;
double price;
}ElemType;
//链表
struct LNode
{
//数据域
ElemType data;
LNode* next;
};
//初始化
void InitList(LNode *& L)
{
//创建头节点
L= new LNode;
L->next = NULL;//头结点的指针域置空
}
//尾插法创建链表
void CreateList_B(LNode*& L, int n)
{
LNode* p = L;
for (int i = 0; i < n; i++)
{
//创建临时节点
LNode* tmp = new LNode;
cin >> tmp->data;
//插入
p->next = tmp;
tmp->next = NULL;
//更新P
p = p->next;
}
}
//删除某节点
LNode* DeleteNode(LNode*& L, LNode* pnode)
{
LNode* tmp = L->next;
if (tmp == pnode)
{
L->next = pnode->next;
delete pnode;
return L->next;
}
//找到pnode的前一个结点——tmp
while (tmp->next != pnode)
{
tmp = tmp->next;
}
tmp->next = pnode->next;
delete pnode;
return tmp->next;
}
//遍历--计算长度
int TraversalList(LNode *& L)
{
LNode* tmp = L->next;
int count = 0;
while (tmp)
{
tmp = tmp->next;
count++;
}
return count;
}
//尾插
int PushbackList(LNode*& L, ElemType e)
{
LNode* tmp = L;//error
int count = 0;
while (tmp->next)//tmp指向最后一个节点
{
tmp = tmp->next;
}
LNode *pnode=new LNode;
pnode->data = e;
pnode->next = NULL;
//插入
tmp->next = pnode;
return 1;
}
//打印链表
void PrintList(LNode*& L)
{
LNode* tmp = L->next;
while (tmp)
{
//打印
cout << tmp->data.no <<" " << tmp->data.name <<" " << fixed << setprecision(2) << tmp->data.price << endl;
tmp = tmp->next;
}
}
队列的基本操作
#define Maxsize 10
bool visited[MVNum];
//队列
#include<stdlib.h>
#include<math.h>
typedef char ElemType;
typedef struct
{
ElemType* arr;
int front;
int rear;
} CircularQueue;
//判空
bool QueueIsEmpty(CircularQueue* Q);
//判满
bool QueueIsFull(CircularQueue* Q);
//构造队列
int QueueCreate(CircularQueue& Q);
//插入
bool QueueEnQueue(CircularQueue* Q, int value);
//求队列长度
int QueueLeength(CircularQueue* Q);
//删除
bool QueueDeQueue(CircularQueue* Q);
//获取队头元素
int QueueFront(CircularQueue* Q);
//获取队尾元素
int myCircularQueueRear(CircularQueue* Q);
//队列的销毁
void QueueDelete(CircularQueue* Q);
//循环队列:用数组实现效率较高
// 关键是判空、判满的条件
bool QueueIsEmpty(CircularQueue* obj);
bool QueueIsFull(CircularQueue* obj);
//构造队列
int QueueCreate(CircularQueue &Q)
{
ElemType *p = new ElemType[Maxsize];
if (!p)
exit(-1);
Q.arr = p;
Q.front = Q.rear = 0;
return 0;
}
//求队列长度
int QueueLeength(CircularQueue* Q)
{
return Q->rear - Q->front + Maxsize;
}
//插入
bool QueueEnQueue(CircularQueue* Q, int value) {
//判满
if (QueueIsFull(Q))
return false;
Q->arr[Q->rear] = value;
//更新
Q->rear++;
Q->rear %= Maxsize;
return true;
}
//删除
bool QueueDeQueue(CircularQueue* Q) {
//判空
if (QueueIsEmpty(Q))
return false;
Q->front++;
Q->front %= Maxsize;
return true;
}
//获取队头元素
int QueueFront(CircularQueue* Q) {
if (QueueIsEmpty(Q))
return -1;
return Q->arr[Q->front];
}
//获取队尾元素
int myCircularQueueRear(CircularQueue* Q) {
if (QueueIsEmpty(Q))
return -1;
//return obj->arr[obj->rear];error,因为有空一个
return Q->arr[--Q->rear%=Maxsize];
}
bool QueueIsEmpty(CircularQueue* Q) {
if (Q->front == Q->rear)
return true;
return false;
}
//判满
bool QueueIsFull(CircularQueue* obj) {
if ((obj->rear + 1) % Maxsize== obj->front)
return true;
return false;
}
//队列的销毁
void QueueDelete(CircularQueue* Q)
{
delete[]Q->arr;
Q->arr = NULL;
}