栈和队列(顺序和链表)

1.顺序栈

#include <stdio.h>
#include <stdlib.h>
#define EMPTY -1
const int m=1000;
int push(int s[],int *top,int x);
int pop(int s[],int *top,int *x);

int main()
{
	int s[m];
	int *top=(int *)malloc(sizeof(int)); 
	*top=EMPTY;
	printf("0-结束,1-进栈,2-出栈\n");
	int a;
	printf("输入:\n");
	scanf("%d",&a);
	while(a!=0)
	{
		if(a==1)
		{
			int x;
			printf("进栈数:\n");
			scanf("%d:",&x);
			push(s,top,x);
			if(push(s,top,x)==0)
				printf("栈满\n");
			if(push(s,top,x)==1)
				printf("进栈成功\n");
		}
		if(a==2)
		{
			int *y=(int *)malloc(sizeof(int));
			pop(s,top,y);
			if(pop(s,top,y)==0)
				printf("栈空\n");
			if(pop(s,top,y)==1)
			{
				printf("出栈成功\n");
				printf("出栈数:%d\n",*y);
			}
		} 
		if(a==0)
			break;
		printf("输入:\n");	
		scanf("%d:",&a);
	}
}

int push(int s[],int *top,int x)
{
	if(*top==m-1)
	{
		return 0;
	}
	s[++(*top)]=x;
	return 1;
}

int pop(int s[],int *top,int *x)
{
	if (*top==EMPTY)
	{
		return 0;
	}
	*x=s[(*top)--];
	return 1;
}

2.链式栈

#include <stdio.h>
#include <stdlib.h> 

typedef struct linkednode
{
	int data;
	struct linkednode *next;
}snode,*ptr;

ptr push(ptr top,int x);
ptr pop(ptr top,int *z);
int shuchu(ptr top);

int main()
{
	ptr top;
	top=(ptr)malloc(sizeof(snode));
	top->next=NULL;
	int x;
	printf("0-结束,1-进栈,2-出栈,3-输出\n");
	printf("输入:");
	scanf("%d",&x);
	while(x!=0)
	{
		if(x==1)
		{
			int y;
			printf("进栈值:\n");
			scanf("%d",&y);
			top=push(top,y);
		}
		if(x==2)
		{
			int *z;
			z=(int *)malloc(sizeof(int));
			top=pop(top,z);
		}
		if(x==3)
		{
			shuchu(top);
		}
		printf("输入:");
		scanf("%d",&x);
	}
}

ptr push(ptr top,int x)
{
	ptr p; 
	p=(ptr)malloc(sizeof(snode));
	if(p==NULL)
	{
		
		printf("申请失败\n");
		return top;
	}
	printf("进栈成功\n");
	p->data=x; 
	p->next=top;
	top=p;
	return p;
}

ptr pop(ptr top,int *z)
{
	ptr p;
	if(top->next==NULL)
	{
		return top;
		printf("出栈失败");
	}
	*z=top->data;
	p=top;
	top=top->next;
	free(p);
	printf("出栈成功\n");
	printf("出栈数:%d\n",*z);
	return top;
}

int shuchu(ptr top)
{
	while(top->next!=NULL)
	{
		printf("%d ",top->data);
		top=top->next;	
	}	
}

3.顺序队列

#include <stdio.h>
#define MaxSize 5 
#define True 1
#define False 0

typedef struct
{
	int data[MaxSize];
	int first,last;
}SeqQueue;

void Init_SeqQueue(SeqQueue *Q);
int Empty_SeqQueue(SeqQueue Q);
void In_SeqQueue(SeqQueue *Q,int x);
int Out_SeqQueue(SeqQueue *Q);

int main()
{
	int i,temp,x;
	SeqQueue Q;
	Init_SeqQueue(&Q);
	printf("入队列:\n");
	printf("请依次输入元素入队(-1表示结束):");
	while(1)
	{
		scanf("%d",&x);
		if(x!=-1)
			In_SeqQueue(&Q,x);   //从键盘输入元素,并依次入队列
		else break;
	} 
	printf("\n出队列:\n");
	while(Empty_SeqQueue(Q)==False)  //如果队列不为空 
	{
		temp=Out_SeqQueue(&Q);    //依次出队列 
		printf("%d ",temp);       //并输出 
	}
	return 0;
}

void Init_SeqQueue(SeqQueue *Q)//创建一个空队列
{
	Q->first=0;    //0或者-1均可,差别在于空和满时的判断条件 
	Q->last=0;
}

int Empty_SeqQueue(SeqQueue Q)  //判断队空
{
	if(Q.last==0)
	 	return True;
	else
	 	return False;
}

void In_SeqQueue(SeqQueue *Q,int x)   //将x入队列
{
	if(Q->last+Q->first==MaxSize)
	{
		printf("队列已满,无法入队\n");	
	} 
	else
	{
		Q->data[Q->last+Q->first]=x;
		Q->last++;
	}
}

int Out_SeqQueue(SeqQueue *Q)  //出队列
{
	if(Q->first==Q->first+Q->last)
	{
		printf("队列已空,无法出队\n");
		return -1; 
	}
	else
	{
		int x=Q->data[Q->first];
		Q->first++;
		Q->last--;
		return x;
	}
}

4.链式队列

#include <stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0

typedef int Status;
typedef int ElemType; 
typedef struct LNode
{
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;

typedef struct
{
	LinkList first,last; /* 队头、队尾指针 */
}LinkQueue;
 /* 带头结点的链队列的基本操作 */

Status InitQueue(LinkQueue *Q); 
Status List(LinkList L);
int QueueLenth(LinkQueue Q);
Status QueueInsert(LinkQueue *Q,ElemType e);
Status QueueDelete(LinkQueue *Q,ElemType *e);

int main()
{
	int x;
	LinkQueue Q;
	InitQueue(&Q);
	QueueInsert(&Q,1);QueueInsert(&Q,2);QueueInsert(&Q,3);
  	List(Q.first);
  	QueueDelete(&Q,&x);
	printf("删除: %d 长度: %d\n",x,QueueLenth(Q));
  	QueueDelete(&Q,&x);QueueDelete(&Q,&x);
  	printf("删除: %d 长度: %d\n",x,QueueLenth(Q));
  	return 0;
}

Status InitQueue(LinkQueue *Q)/* 构造一个空队列Q */
{ 
	LinkList p;
   	p=(LNode*)malloc(sizeof(LNode)); 
   	p->next=NULL;
   	(*Q).last=(*Q).first=p;
   	return OK;
}

Status List(LinkList L)/*输出队列值*/ 
{
	LinkList p;
  	if(!L) return ERROR;
  	p=L->next;
  	while(p)
  	{
    	printf(" %d",p->data);
    	p=p->next;
  	}
 	printf("\n");
	return OK;
}

int QueueLenth(LinkQueue Q)//队列长度 
{
  int n=0;
  LinkList p;
  if(Q.last==Q.first)
    return 0;
  p=Q.first->next;
  while(p)
  {
    n++;
    p=p->next;
  }
  return n;
}

Status QueueInsert(LinkQueue *Q,ElemType e)//链队列的插入操作
{ 
	LNode *s;
	s=(LNode *)malloc(sizeof(LNode));
	s->data=e;
	s->next=NULL;
	Q->last->next=s;
	Q->last=s;
	return OK;
} 

Status QueueDelete(LinkQueue *Q,ElemType *e) //链队列的删除操作
{ 
	LNode *p;
	if(Q->last==Q->first)
	return ERROR;
	else
	{
    	p=Q->first->next;
    	*e=p->data;
    	Q->first->next=p->next;
  		if(Q->last==p)
  		{
			Q->last=Q->first;
		}
		free(p);
  		return OK;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

超级无敌暴龙战士xin

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

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

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

打赏作者

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

抵扣说明:

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

余额充值