数据结构——栈

顺序栈的基本操作

SeqStack.h

#include<iostream>
using namespace std;

#define MAXSIZE 100
typedef int ElemType ;
typedef struct SeqStack {
	int *elem;
	int top;
}SeqStack;

/*
	初始化一个栈
*/
void InitStack(SeqStack &s);

/*
	入栈
*/
void Push(SeqStack &s,ElemType e);

/*
	出栈
*/
ElemType Pop(SeqStack &s);

/*
	输出元素
*/
void PrintStack(SeqStack &s);

SeqStack.cpp

#include"SeqStack.h"

/*
	初始化一个栈
*/
void InitStack(SeqStack &s) {
	s.elem=new int[MAXSIZE];
	s.top = -1;
}

/*
	入栈
*/
void Push(SeqStack &s,ElemType e) {
	if (s.top==MAXSIZE-1) {
		exit(-1);
	}
	else {
		s.elem[++s.top] = e;
	}
}

/*
	出栈
*/
ElemType Pop(SeqStack &s) {
	if (s.top==-1) {
		exit(-1);
	}
	else {
		return s.elem[--s.top];
	}
}

/*
	输出栈的元素
*/
void PrintStack(SeqStack &s) {
	while (s.top>-1) {
		cout << s.elem[s.top--]<<"	";
	}
}

test.cpp

#include"SeqStack.h"

void main() {
	SeqStack s;
	InitStack(s);
	Push(s, 1);
	Push(s, 2);
	Push(s, 3);
	Push(s, 4);
	Pop(s);
	PrintStack(s);
}

链栈的基本操作

ListStack.h

#include<iostream>
using namespace std;
typedef int DataType;
typedef struct StackNode {
	DataType data;
	struct StackNode* next;
}StackNode,*LinkStack;

/*
	初始化一个链栈
*/
void InitStack(LinkStack& s);

/*
	入栈
*/
void PushStack(LinkStack s,DataType a);

/*
	出栈
*/
DataType PopStack(LinkStack s);

/*
	输出链栈的元素
*/
void PrintStack(LinkStack s);

ListStack.cpp

#include"ListStack.h"

/*
	初始化一个链栈
*/
void InitStack(LinkStack& s) {
	s = new StackNode;
	s->next = NULL;
}

/*
	入栈
*/
void PushStack(LinkStack s,DataType d) {
	StackNode* ps=new StackNode;
	ps->data = d;
	ps->next = s->next;
	s->next = ps;
}

/*
	出栈
*/
DataType PopStack(LinkStack s) {
	if (s->next)
	{
		StackNode* p = s->next;
		s->next = s->next->next;
		return p->data;
		
	}
	else {
		return NULL;
	}
}

/*
	输出链栈的元素
*/
void PrintStack(LinkStack s) {
	while (s->next) {
		cout << s->next->data << "	";
		s->next = s->next->next;
	}
}

test.h

#include"ListStack.h"

void main() {
	LinkStack s;
	InitStack(s);
	PushStack(s, 1);
	PushStack(s, 2);
	PushStack(s, 3);
	PushStack(s, 4);
	cout<<PopStack(s)<<"\n";
	PrintStack(s);
}

队列的基本操作——循环队列

SeQueue.h

#include<iostream>
using namespace std;

#define MAXSIZE 10
typedef int ElemType;
/*
	顺序队列的数据结构
	elem :定义的队列
	front:队列的头指针,下标从0开始
	rear :队列的尾指针,下标从0开始
*/
typedef struct  SeQueue {
	ElemType *elem;
	int front, rear;
}SeQueue;

/*
	初始化一个队列
*/
void InitQueue(SeQueue& S);

/*
	入队
*/
bool InQueue(SeQueue &S,ElemType e);
/*
	出队
*/
bool OutQueue(SeQueue &S,ElemType &e);

/*
	打印出队列的元素
*/
void printQueue(SeQueue s);

SeQueue.cpp

#include"SeQueue.h"

/*
	初始化一个队列
*/
void InitQueue(SeQueue& S) {
	S.elem = new ElemType[MAXSIZE];
	if (!S.elem)
		exit(-1);
	S.front = S.rear = 0;
}

/*
	入队
*/
bool InQueue(SeQueue &S, ElemType e) {
	if ((S.rear+1)%MAXSIZE==S.front) {
		cout << "队列已满";
		return false;
	}
	else {
		S.elem[S.rear] = e;
		S.rear = (S.rear + 1) % MAXSIZE;
		return true;
	}
}

/*
	出队
*/
bool OutQueue(SeQueue &S, ElemType& e) {
	if (S.front == S.rear) {
		cout << "队列为空";
		return false;
	}
	else {
		e = S.elem[S.front];
		S.front = (S.front + 1) % MAXSIZE;
		return true;
	}
}
/*
	求队列长度
*/
int LongQueue(SeQueue s) {
	if (s.front == s.rear)
		return 0;
	else {
		return (s.rear - s.front + MAXSIZE) % MAXSIZE;
	}
}

/*
	打印出队列的元素
*/
void printQueue(SeQueue s) {
	if (s.front ==s.rear) {
		cout << "这是一个空队列";
	}
	else {
		int longth =LongQueue(s);
		cout << "队列的长度为:" << longth<<"\n";
		int index = s.front;
		for (int i = 0; i < longth;i++) {
			cout << s.elem[index]<<" ";
			index = (index + 1) % MAXSIZE;
		}
	}
}

test.cpp

#include"SeQueue.h"

void main() {
	SeQueue S;
	int e;
	InitQueue(S);
	for(int i=0;i<9;i++){
		InQueue(S,i+1);
	}
	OutQueue(S,e);
	printQueue(S);
	cout << "\n出来的是" << e; 
}

队列的基本操作——链队列

ListQueue.h

#include<iostream>
using namespace std;

typedef int DataType ;
typedef struct QNode {
	DataType data;
	struct QNode* next;
}QNode,*QueuePtr;

typedef struct {
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

/*
	初始化一个队列
*/
void InitQueue(LinkQueue &L);

/*
	入队
*/
void In_Queue(LinkQueue &L,DataType e);

/*
	出队
*/
void Out_Queue(LinkQueue &L);

/*
	打印队列
*/
void PrintQueue(LinkQueue L);

ListQueue.cpp

#include"ListQueue.h"

/*
	初始化一个队列
*/
void InitQueue(LinkQueue &L) {
	L.rear=L.front=new QNode;
	if (!L.front) {
		exit(-1);
	}
	L.front->next = NULL;
}

/*
	入队
*/
void In_Queue(LinkQueue& L, DataType e) {
	QNode *node=new QNode;
	if (!node) {
		cout << "创建结点失败\n";
		exit(-1);
	}
	else {
		node->data = e;
		node->next = NULL;
		L.rear->next = node;
		L.rear = node;
	}
}

/*
	出队
*/
void Out_Queue(LinkQueue& L) {
	if (L.front==L.rear) {
		cout << "队列为空,出失败\n";
		exit(-1);
	}
	else {
		QNode* p = new QNode;
		p = L.front->next;
		L.front->next = p->next;
		if (p==L.rear) {
			L.rear = L.front;
		}
		delete p;
	}
}

/*
	打印队列的元素	
*/
void PrintQueue(LinkQueue L) {
	if (L.front->next==NULL) {
		cout << "队列为空";
		exit(-1);
	}
	else {
		/* 生成一个指针P, 指向第一个元素结点, 接着依次向后遍历,
			当P == L.rear, P指向了最后一个元素,
			则P.next为空,这个就是循环结束的依据*/
		QNode* p = new QNode;
		p=L.front->next;
		while(p){
			cout << p->data<<"\n";
			p=p->next;
		}
	}
}

test.cpp

#include"ListQueue.h"


void main() {
	LinkQueue L;
	InitQueue(L);
	for (int i = 0; i < 5;i++) {
		In_Queue(L,i+1);
	}
	for (int i = 0; i < 2; i++) {
		Out_Queue(L);
	}
	
	PrintQueue(L);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值