栈:
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef char SElemType;
typedef int Status;
typedef struct
{
SElemType* base;
SElemType* top;
int stacksize;
}Sqstack;
//顺序栈初始化 栈底=栈顶
int InitStack(Sqstack& S)
{
S.base = new SElemType[MAXSIZE];
if (!S.base) exit(ERROR);
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
int Is_exist(Sqstack S)
{
if (S.base) return ERROR;
return OK;
}
//判断栈是否为空
int StackEmpty(Sqstack S)
{
if (S.top == S.base)
return true;
else
return false;
}
//求顺序栈长度
int StackLength(Sqstack S)
{
return S.top - S.base;
}
//清空顺序栈
int ClearStack(Sqstack S)
{
if (S.base) S.top = S.base;
return OK;
}
//销毁顺序栈
int DestoryStack(Sqstack& S)
{
if (S.base)
{
delete S.base;
S.stacksize = 0;
S.base = S.top = NULL;
}
return OK;
}
//顺序栈入栈
int Push(Sqstack& S, char e)
{
if (S.top - S.base == S.stacksize)
return ERROR;
*S.top++=e; //*S.top=e; S.top++;
return OK;
}
//顺序栈出栈
int Pop(Sqstack& S, char& e)
{
if (S.top == S.base)
return ERROR;
e = *S.top;
S.top--;
return OK;
}
//顺序栈遍历
void printStack(Sqstack S)
{
char* p = S.base;
while (p != S.top)
{
cout << *p << " ";
p++;
}
cout << endl;
}
int main()
{
Sqstack s;
InitStack(s);
if (StackEmpty(s) == 1) cout << "此顺序栈为空!" << endl;
Push(s, 'a');
Push(s, 'b');
Push(s, 'c');
printStack(s);
char a, b;
Pop(s, a); printStack(s);
cout << "长度为:" << StackLength(s) << endl;
Pop(s, b); printStack(s);
cout << "长度为:" << StackLength(s) << endl;
DestoryStack(s);
if(Is_exist(s)==1)
cout << "顺序栈已销毁" << endl;
}
#include<iostream>
#include<string>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef char SElemType;
typedef int Status;
typedef struct StackNode
{
SElemType data;
struct StackNode* next;
}StackNode ,*LinkStack;
//链栈初始化
void InitStack(LinkStack& S)
{
S = NULL;
}
//判断链栈是否为空
int StackEmpet(LinkStack S)
{
if (S == NULL)
return true;
return false;
}
//链栈入栈
int Push(LinkStack& S, char e)
{
LinkStack p=new StackNode;
p->data = e;
p->next = S;
S = p;
return OK;
}
//链栈的出栈
int Pop(LinkStack& S, char& e)
{
LinkStack p;
if (S == NULL) return ERROR;
e = S->data;
p = S;
S = S->next;
delete p;
return OK;
}
//取栈顶元素
char GetTop(LinkStack S)
{
if (S != NULL)return S->data;
}
//求链栈长度
int GetLength(LinkStack S)
{
int length=0;
LinkStack p=S;
while (p != NULL)
{
length++;
p = p->next;
}
return length;
}
void Destory(LinkStack& S)
{
LinkStack p;
while (S)
{
p = S;
S = S->next;
delete p;
}
S = NULL;
}
void Is_exist(LinkStack S)
{
if (!S)
cout << "链表不存在!" << endl;
else
cout << "链表存在!" << endl;
}
int main()
{
LinkStack L;
InitStack(L);
cout << "长度为:" << GetLength(L) << endl;
if (StackEmpet(L) == 1)
cout << "此链栈为空!" << endl;
Push(L, 'a');
Push(L, 'b');
Push(L, 'c');
cout << "栈顶元素为:" << GetTop(L) << endl;
char a;
Pop(L,a);
cout << "取出的栈顶元素为:" << a << endl;
cout << "栈顶元素为:" << GetTop(L) << endl;
cout << "长度为:" << GetLength(L) << endl;
Destory(L);
Is_exist(L);
}
队列
#include<iostream>
#include<string>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef char QElemType;
typedef int Status;
typedef struct
{
QElemType* base;
int front;
int rear;
}SqQueue;
//初始化队列
int InitQueue(SqQueue& Q)
{
Q.base = new QElemType[MAXSIZE];
if (!Q.base) exit(ERROR);
Q.front = Q.rear = 0;
return OK;
}
//求队列长度
int QueueLength(SqQueue Q)
{
return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}
//循环队列入队
int ENQueue(SqQueue& Q, char e)
{
if ((Q.rear + 1) % MAXSIZE == Q.front) return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXSIZE;
return OK;
}
//循环列表出队
int DeQueue(SqQueue& Q, char& e)
{
if (Q.front == Q.rear) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXSIZE;
return OK;
}
//取队头元素
char GetHead(SqQueue Q)
{
if (Q.front != Q.rear) return Q.base[Q.front];
}
//遍历队列
int PrintQueue(SqQueue Q)
{
while (Q.front != Q.rear)
{
cout << Q.base[Q.front]<<" ";
Q.front = (Q.front + 1) % MAXSIZE;
}
cout << endl;
return OK;
}
int main()
{
SqQueue Q;
InitQueue(Q);
cout << "长度为:" << QueueLength(Q)<<endl;
ENQueue(Q, 'a');
ENQueue(Q, 'b');
ENQueue(Q, 'c');
cout<<"队头元素为:"<<GetHead(Q)<<endl;
PrintQueue(Q);
char a;
DeQueue(Q, a);
cout << "出队元素:" << a << endl;
PrintQueue(Q);
}
#include<iostream>
#include<string>
using namespace std;
#include<iostream>
#include<string>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef char QElemType;
typedef int Status;
typedef struct Qnode
{
QElemType data;
struct Qnode* next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//链队列初始化
int InitQueue(LinkQueue& Q)
{
Q.front = Q.rear = new QNode;
if (!Q.front) exit(ERROR);
Q.front->next = NULL;
return OK;
}
//销毁链队列
int DestoryQueue(LinkQueue& Q)
{
QueuePtr p;
while (Q.front)
{
p = Q.front->next;
delete Q.front;
Q.front = p;
}
return OK;
}
int Is_exit(LinkQueue Q)
{
if (Q.front) cout << "存在!" << endl;
else cout << "不存在!"<<endl;
return OK;
}
//将元素e入队
int EnQueue(LinkQueue& Q, char e)
{
QueuePtr p=new QNode;
if (!p) exit(ERROR);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
//链栈出队
int DeQueue(LinkQueue& Q, char& e)
{
if (Q.front == Q.rear) return ERROR;
QueuePtr p = new QNode;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
delete p;
return OK;
}
//链栈头元素
int GetHead(LinkQueue Q, char& e)
{
if (Q.front == Q.rear)return ERROR;
e = Q.front->next->data;
return OK;
}
//遍历链栈
int print(LinkQueue Q)
{
if (Q.front == Q.rear) return ERROR;
QueuePtr p = new QNode;
p = Q.front->next;
while (p)
{
cout << p->data<<" ";
p=p->next;
}
cout << endl;
}
int main()
{
LinkQueue q;
InitQueue(q);
EnQueue(q, 'a');
EnQueue(q, 'b');
EnQueue(q, 'c');
print(q);
char a,b; GetHead(q, a);
cout << a << endl;
DeQueue(q, b);
cout << b<< endl;
print(q);
DestoryQueue(q);
Is_exit(q);
}