栈和队列

栈的代码实现:

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

#define MAXSIZE 10
#define ERROR 0
#define OK 1
typedef int SElemType;
typedef struct
{
	SElemType data[MAXSIZE];
	int top;
}pStack;

void InitStack(pStack *p)
{
	int i = 0;
	p->top = -1;
	while(i < MAXSIZE)
	{
		p->data[i++] = 0;
	}
}

int Push(pStack *p,SElemType e)
{
	if(p->top == MAXSIZE - 1)
		return ERROR;
	p->top++;
	p->data[p->top] = e;
	return OK;
}

int Pop(pStack *p,SElemType *e)
{
	if(p->top == -1)
		return ERROR;
	*e = p->data[p->top--];
	return OK;
}

void Print(pStack *p , int i , int j)
{
	for(i ; i<=j ; i++)
		printf(" data[%d] = %d ",i,p->data[i]);
	printf("\n");
}

main(int argc,char *argv[])
{
	int i;
	pStack stack;
	SElemType e;
	InitStack(&stack);
	if(argc == 1)
	{
		printf("need input something such as ./main ***\n");
	}
	for(i=1 ; i<argc ; i++)
	if(Push(&stack , atoi(argv[i])))
	{
		printf("Push %d success\n",atoi(argv[i]));
	}
	
	Print(&stack , 0 , 9);
	
	if(Pop(&stack , &e))
	{
		printf("the Pop value is %d\n", e);
	}
	return ;
}

两栈共享空间的代码实现:

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

#define OK 1
#define ERROR 0
#define MAXSIZE 10
typedef enum{
	error = ERROR,
	ok = OK
}Status;
typedef int SElemType;
typedef struct 
{
	SElemType data[MAXSIZE];
	int head_top;
	int tail_top;
}ShareStack;

void InitShareStack(ShareStack *SS)
{
	int i=0;
	while(i < MAXSIZE)
	{
		SS->data[i++] = 0;	
	}
	SS->head_top = -1;
	SS->tail_top = MAXSIZE;
}

void PrintShareStack(ShareStack *SS)
{
	int i=0;
	while(i < MAXSIZE)
	{
		printf("SS->data[%d] = %d\n", i , SS->data[i]);	
		i++;
	}
}
Status Push(ShareStack *SS , SElemType e , int StackNum)
{
	if(SS->head_top + 1 == SS->tail_top)
	{
		printf("Push failed , ShareStack is full\n");
		return error;	
	}
	if(StackNum == 1)
		SS->data[++SS->head_top] = e;
	else if(StackNum == 2)
		SS->data[--SS->tail_top] = e;
	return ok;
}

Status Pop(ShareStack *SS , SElemType *e , int StackNum)
{
	if(SS->head_top == -1 ||  SS->tail_top == MAXSIZE)
	{
		printf("Pop failed , ShareStack is empty\n");
		return error;	
	}
	if(StackNum == 1)
		*e = SS->data[SS->head_top++];
	else if(StackNum == 2)
		*e = SS->data[SS->tail_top--];
	return ok;
}
main()
{
	SElemType Input,Output;
	int i,num;
	ShareStack *MySS;
	MySS =(ShareStack *)malloc(sizeof(ShareStack));
	InitShareStack(MySS);
	for(i=0;i<5;i++)
	{
		scanf("%d",&Input);
		scanf("%d",&num);
		if(Push(MySS , Input , num))
		{
			printf("%d Push Stack %d successed\n",Input,num);	
		}
	}
	printf("input finish\n");	

	PrintShareStack(MySS);

	if(Pop(MySS , &Output , 2))
	{
		printf("the Stack2 pop = %d\n",Output);
	}
	if(Pop(MySS , &Output , 1))
	{
		printf("the Stack1 pop = %d\n",Output);
	}
	PrintShareStack(MySS);
}

循环队列的代码实现:

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

#define OK 1
#define ERROR 0
#define MAXSIZE 5
typedef int QElemType;
typedef struct
{
	QElemType data[MAXSIZE];
	int rear;
	int front;
}Queue;

void InitQueue(Queue *Q)
{
	int i;
	for(i=0 ; i < MAXSIZE ; i++)
		Q->data[i] = 0;
	Q->rear = 0;
	Q->front = 0;
}
void PrintQueue(Queue *Q)
{
	int i;
	for(i=0 ; i < MAXSIZE ; i++)
	{
		printf("Q->data[%d] = %d\n",i,Q->data[i]);
	}
}

int EnQueue(Queue *Q , QElemType e)
{
	if((Q->rear+1) % MAXSIZE == Q->front)
	{
		return ERROR;	
	}
	Q->data[Q->rear] = e;
	Q->rear = (Q->rear+1)%MAXSIZE;
	return OK;
}
int DeQueue(Queue *Q , QElemType *e)
{
	if(Q->rear == Q->front)
	{
		return ERROR;
	}
	*e = Q->data[Q->front];
	Q->front=(Q->front+1)%MAXSIZE;
	return OK;
}
main(int argc ,char *argv[])
{
	Queue *MyQ;
	MyQ = (Queue *)malloc(sizeof(Queue));
	InitQueue(MyQ);
	int i;
	if(argc == 1)
	{
		printf("input something with ./main ****\n");
	}
	for(i=1 ; i < argc ; i++)
	if(EnQueue(MyQ , atoi(argv[i])))
	{
		printf("argv[%d] EnQueue successed,rear = %d\n",i,MyQ->rear);
	}
	PrintQueue(MyQ);
	QElemType output;
	DeQueue(MyQ , &output);
	printf("DeQueue = %d, front = %d\n",output,MyQ->front);
	PrintQueue(MyQ);
	for(i=1 ; i < 2 ; i++)
	if(EnQueue(MyQ , atoi(argv[i])))
	{
		printf("argv[%d] EnQueue successed,rear = %d\n",i,MyQ->rear);
	}
	PrintQueue(MyQ);
	return;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值