栈的基本操作
#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;
}
}
标签:tmp,return,LNode,int,next,链表,pnode,基本操作
From: https://blog.51cto.com/u_15805257/7969135