数据结构---栈和队列

刘佳瑜*,王越 *, 黄扬* , 张钊*

(淮北师范大学计算机科学与技术学院,安徽 淮北)

*These authors contributed to the work equllly and should be regarded as co-first authors.

目录

栈的逻辑结构

栈的基本操作

顺序栈

共享栈

链栈

队列的逻辑结构和基本操作 

队列的顺序实现

队列的链式存储

双端队列

栈在递归中的应用


栈的逻辑结构


 栈的基本操作


顺序栈

代码实现:

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5
typedef int ElemType;  
typedef struct
{ ElemType data[MaxSize];
  int top; //栈顶指针 
}SqStack;
//栈的初始化 
void InitSqStack(SqStack &S)
{
	S.top=-1;
 } 
//判栈空
bool StackEmpty(SqStack S)
{
	if(S.top==-1)
	    return true;
	else
	    return false;
}
//入栈的操作
bool Push(SqStack &S,ElemType x) 
{
	if(S.top==MaxSize-1)
	    return false;
    S.top++;
    S.data[S.top]=x;
}
//出栈的操作
bool Pop(SqStack &S,ElemType &x) 
{
	if(S.top==-1)
	    return false;
    x=S.data[S.top];
    S.top--;
}
//打印出栈所有的元素 
void print(SqStack &S) 
{
	int i=0; 
	printf("此时栈中的所有元素:\n");
	for(i=0;i<=S.top;i++)
	    printf("%d ",S.data[i]);
	printf("\n");
}
int main()
{
	SqStack S;
	int x=0; 
	InitSqStack(S);
	printf("入栈0,1,2,3,4:\n");
	Push(S,0);
	Push(S,1);
	Push(S,2);
	Push(S,3);
	Push(S,4);
	print(S); 
	printf("执行一次出栈:\n");
	Pop(S,x);
	print(S); 
}


 共享栈

#include<stdio.h>
#define MaxSize 10
typedef struct()
{
	int data[MaxSize];
	int top0;
	int top1;
}
void InitStack()
{
	S.top0=-1;
	S.top1=MaxSize;
}

链栈

代码实现: 

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5
typedef int ElemType;  
typedef struct Node
{ ElemType data;
  struct Node *next; 
}StackNode,*LinkStack;

//栈的初始化 
void InitSqStack(LinkStack &top)
{
    top=NULL;
 } 
//栈判空
int StackEmpty(LinkStack top) 
{
	if(top==NULL)
	   return true;
	else
	   return false;
}
//进栈操作
void Push(LinkStack &top,ElemType x) 
{
	LinkStack s;
	s=(LinkStack)malloc(sizeof(StackNode));
	s->data=x;
	s->next=top;
	top=s;
}
//出栈操作
int Pop(LinkStack &top,ElemType &x) 
{
	if(StackEmpty(top))
	   return false;
	LinkStack p;
	p=(LinkStack)malloc(sizeof(StackNode));
	x=top->data;
	p=top;
	top=top->next;
	free(p);
	return true;
}
void print(LinkStack top)
{
	LinkStack p;
	top=top;
	printf("链栈中的元素:\n");
	while(top!=NULL) 
	{
		printf("%d ",top->data);
		top=top->next; 
	}
}
int main()
{
    LinkStack top;
    InitSqStack(top);
    int x=0;
    printf("链栈的相关操作:\n");
	printf("进栈0,1,2,3,4:\n");
	Push(top,0);
	Push(top,1);
	Push(top,2);
	Push(top,3);
	Push(top,4);
	print(top); 
	printf("\n");
	printf("执行一次出栈:\n");
	Pop(top,x);
	print(top); 
}


队列的逻辑结构和基本操作 


队列的顺序实现

队列的定义和初始化

#define MaxSize 10
typedef struct
{
  int data[MaxSize];
  int front,rear;
}SqQueue;
void InitSqQueue(SqQueue &S)
{
  int i;
  S.front=S.rear=0;
}
int main() 
{
  SqQueue S;
  InitSqQueue(S);
  return 0;
}

  队列的入队和出队

bool EnQueue(SqQueue &S,int e)
{
  if((Q.rear+1)%MaxSize==Q.front)
      return false;
  Q.data[Q.rear]=e;
  Q.rear=(Q.rear+1)%MaxSize;
}
bool DeQueue(SqQueue &S,int &e)
{
  if(Q.rear+1==Q.front)
      return false;
  e=Q.data[Q.front];
  Q.front=(Q.front+1)%MaxSize;
}

求队列的长度

int Length(SqQueue &S)
{
  int length;
  length=(Q.rear+MaxSize-Q.front)%MaxSize;
  return length;
}

 全部代码

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 6

typedef int ElemType;  
typedef struct 
{ ElemType data[MaxSize];
  int front,rear;
}SqQueue;
//初始化 
void InitSqQueue(SqQueue &Q) 
{
	Q.front=Q.rear=0; 
}
//判空操作
bool isEmpty(SqQueue Q) 
{
	if(Q.front==Q.rear)
	    return true;
	else
	    return false;
}
//判满操作
bool isFull(SqQueue Q) 
{
	if((Q.rear+1)%MaxSize==Q.front)
	    return true;
	else
	    return false;
}
//入队操作
int EnQueue(SqQueue &Q,ElemType x) 
{    
    if(isFull(Q))
        return false;
	Q.data[Q.rear]=x;
	Q.rear=(Q.rear+1)%MaxSize;
}
//出队操作
int DeQueue(SqQueue &Q,ElemType &x) 
{    
    if(isEmpty(Q))
        return false;
	x=Q.data[Q.front];
	Q.front=(Q.front+1)%MaxSize;
}
//打印元素
void print(SqQueue Q,int l) 
{
	int i; 
	printf("此时队中的所有元素:\n");
	for(i=Q.front;i<Q.rear;i++)
	    printf("%d ",Q.data[i]);
	printf("\n");
}
//计算元素的个数
int  QueueLength(SqQueue Q) 
{
	return ((Q.rear+MaxSize-Q.front)%MaxSize);
}
int main()
{
    SqQueue Q;
    InitSqQueue(Q);
    int x=0,l1; 
	printf("入队0,1,2,3,4:\n");
	EnQueue(Q,0);
	EnQueue(Q,1);
	EnQueue(Q,2);
	EnQueue(Q,3);
	EnQueue(Q,4);
	l1=QueueLength(Q);
	print(Q,l1); 
	printf("执行一次出队:\n");
	DeQueue(Q,x);
	l1=QueueLength(Q);
	print(Q,l1); 
	return 0; 
}

 队列的链式存储

初始化 

       Q是一个结构体类型(LinkQueue),里面有两个元素,一个是front指针,一个是rear指针,这两个指针均指向,一个结构体类型(LinkNode)。

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
typedef struct LinkNode
{
  int data;
  struct LinkNode *next;
}LinkNode;
typedef struct LinkQueue
{
  LinkNode *rear,*front;
}LinkQueue;
void InitQueue(LinkQueue &Q)
{
  Q.front=Q.rear=(LinkNode *)malloc(sizeof(LinkNode));
  Q.front->next=NULL;
}
int main() 
{
  LinkQueue Q;
  InitQueue(Q);
  return 0;
}

入队

void EnQueue(LinkQueue &Q,int x)
{
  LinkNode *s;
  s=(LinkNode *)malloc(sizeof(LinkNode));
  s->data=x;
  s->next=NULL;
  Q.rear->next=s;
  Q.rear=s;
}

出队

void DeQueue(LinkQueue &Q,int &x)
{
  LinkNOde *p=Q.front->next;
  x=p->data;
  Q.front->next=p->next;
  free(p);
}

双端队列

双端队列可以分成两种一种是插入受限的双端队列,一种是删除受限的双端队列。


栈在递归中的应用

n=4,时,return Fib(3)+Fib(2), 执行到Fib(3)跳转函数执行。

n=3,时,return Fib(2)+Fib(1), 执行到Fib(2)跳转函数执行。

n=2,时,return Fib(1)+Fib(0), 执行到Fib(1)跳转函数执行。

n=1,时,return 1。

跳转到return Fib(1)【返回值1的位置】+Fib(0),执行到Fib(0)跳转函数执行。

n=0,时,return 0。

跳转到n=2,时,return Fib(1)【返回值1的位置】+Fib(0)【返回值2的位置】=1。

跳转到n=3,时,return Fib(2)【返回值2的位置】+Fib(1),执行到Fib(1)跳转函数执行。

Institutional Review Board Statement: Not applicable.

Informed Consent Statement: Not applicable.

Data Availability Statement: Not applicable.

Author Contributions:All authors participated in the assisting performance study and approved the paper.

Conflicts of Interest: The authors declare no conflict of interest.

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卿云阁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值