栈与队列

记得初始化

Stack *s;后要malloc动态分配空间,负责会报错:Program received signal SIGSEGV, Segmentation fault.
空指针

#include<stdio.h>
#define MAX 100
typedef struct Stack{
	int data[MAX];
	int top;
}Stack;
void initStack(Stack &s){
	s.top=0;
}
bool isEmpty(Stack s){
	if(s.top==0){
		return true;
	}
	return false;
}
bool push(Stack& s,int d){
	if(s.top==MAX){
		return false;
	}
	s.data[s.top]=d;
	s.top++;
	return true;
}
bool pop(Stack& s,int &d){
	if(isEmpty(s)){
		return false;
	}
	s.top--;
	d=s.data[s.top];
	return true;
}
bool getTop(Stack s,int &d){
	if(s.top==0){
		return false;
	}
	d=s.data[s.top-1];
	return true;
}
int main(){
	return 0;
}

队列:

循环队列:(牺牲一个单元来区分队空和队满法)

q.front == q.rear为队空
(q.rear+1)%MAX == q.front 为队满

#include<stdio.h>
#define MAX 100
typedef struct Stack{
	int data[MAX];
	int top;
}Stack;
void initStack(Stack &s){
	s.top=0;
}
bool isEmpty(Stack s){
	if(s.top==0){
		return true;
	}
	return false;
}
bool push(Stack& s,int d){
	if(s.top==MAX){
		return false;
	}
	s.data[s.top]=d;
	s.top++;
	return true;
}
bool pop(Stack& s,int &d){
	if(isEmpty(s)){
		return false;
	}
	s.top--;
	d=s.data[s.top];
	return true;
}
bool getTop(Stack s,int &d){
	if(s.top==0){
		return false;
	}
	d=s.data[s.top-1];
	return true;
}
typedef struct SqQueue{
	int data[MAX];
	int front,rear;
}SqQueue;
void initQueue(SqQueue &q){
	q.front=q.rear=0;
} 
bool enQueue(SqQueue &q,int d){
	if((q.rear+1)%MAX==q.front){
		return false;
	}
	q.data[q.rear]=d;
	q.rear=(q.rear+1)%MAX;
	return true;
}
bool deQueue(SqQueue &q,int &d){
	if(q.front==q.rear){
		return false;
	}
	d=q.data[q.front];
	q.front=(q.front+1)%MAX;
	return true;
}
bool isEmpty(SqQueue q){
	if(q.front==q.rear){
		return true;
	}
	return false;
}
int main(){
	SqQueue q;
	initQueue(q);
	enQueue(q,0);
	enQueue(q,1);
	int a=-1;
	deQueue(q,a);
	enQueue(q,2);
	while(!isEmpty(q)){
		deQueue(q,a);
		printf("%d\n",a);
	}
	return 0;
}

链式队列:

记得初始化啊亲,debug了半天,才发现原来是因为少写了个initQueue(q),我快哭了.

#include<stdio.h>
#include<stdlib.h>
typedef struct LinkNode
{
	int data;
	struct LinkNode* next;
}LinkNode;
typedef struct LinkQueue 
{
	LinkNode *front,*rear;
}LinkQueue;
void initQueue(LinkQueue& q)
{
	q.front=q.rear=(LinkNode*)malloc(sizeof(LinkNode));
	q.front->next=NULL;
}
bool isEmpty(LinkQueue q)
{
	if(q.front==q.rear)
		return true;
	return false;
}
void enQueue(LinkQueue &q,int x)
{
	
	LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
	s->data=x;
	s->next=NULL;
	q.rear->next=s;
	q.rear=s;
}
bool deQueue(LinkQueue &q,int &x)
{
	if(q.front==q.rear)
	{
		return false;
	}
	//LinkNode* p=(LinkNode*)malloc(sizeof(LinkNode));
	LinkNode* p;
	p=q.front->next;
	x=p->data;
	q.front->next=p->next;
	if(q.rear==p)
		free(p);
	return true;

}
int main()
{
	
	int x=3;
	LinkQueue q;
	initQueue(q);
	if(isEmpty(q))
		printf("NULL\n");
	enQueue(q,x);
	if(deQueue(q,x))
		printf("%d",x);
	printf("%d",x);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值