栈和队列的练习

栈的练习


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

typedef struct NODE
{
     int data;
     struct NODE *next;
     
}node,*pnode;
typedef struct STACK
{
	pnode ptop;
	pnode pbottom;
}stack,*pstack;


void initstack(stack *ps)
{
	ps->ptop=(node *)malloc(sizeof(node));
	if(ps->ptop==NULL)
	{
		printf("动态内存分配失败\n");
		exit(-1);
	}
	else
	{
		ps->pbottom=ps->ptop;
		ps->ptop->next=NULL;
	}
}

void pushstack(stack *ps,int val)
{
	pnode pnew=(node*)malloc(sizeof(node));
	pnew->data=val;
	pnew->next=ps->ptop;
	ps->ptop=pnew;
}

void traverse(stack *ps)
{
	node *p=ps->ptop;
	while(p != ps->pbottom)
	{
		printf("%d ",p->data);
		p=p->next;
	}
}

bool empty(stack *ps)
{
	if(ps->ptop==ps->pbottom)
	{
		return true;
	}
    else
    {
    	return false;
    }

}

bool popstack(stack *ps,int *pval)
{
	if(empty(ps))
	{
		return false;
	}
	else
	{
		node *r=ps->ptop;
		ps->ptop=r->next;
		*pval=r->data;
		free(r);
		return true;
	}
}

bool clearstack(stack *ps)
{
	if(empty(ps))
	{
		return true;
	}
	else
	{
		node *p=ps->ptop;
		node *q=NULL;
		while(p!=ps->pbottom)
		{
			q=p->next;
			free(p);
			p=q;
		}
		ps->ptop=ps->pbottom;
	}
}

int main(){
    int val;
    stack s;
    initstack(&s);
    pushstack(&s,1);
    pushstack(&s,2);
    pushstack(&s,3);
    pushstack(&s,4);
    pushstack(&s,5);
    pushstack(&s,6);
    traverse(&s);
    if(popstack(&s,&val))
    {
    	printf("\n出栈成功%d\n",val);
    }
    else
    {
    	printf("\n出栈失败\n");
    }
    traverse(&s);
    clearstack(&s);
    traverse(&s);

    return 0;

}

队列练习

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

typedef struct QUEUE
{
	int *pbase;
	int front;
	int rear;
}Queue;

void intiQueue(Queue *);
bool en_Queue(Queue *,int);
void traverse_Queue(Queue *);
bool full_Queue(Queue *);
bool out_Queue(Queue *,int *);
bool emput_Queue(Queue *);
int main()
{
	int val;
    Queue Q;
    intiQueue(&Q);
    en_Queue(&Q,1);
    en_Queue(&Q,2);
    en_Queue(&Q,3);
    en_Queue(&Q,4);
    en_Queue(&Q,5);
    traverse_Queue(&Q);
    if(en_Queue(&Q,6))
    {
    	printf("入队成功\n");
    }
    else
    {
    	printf("队列已满,不能入列\n");
    }

    printf("\n出队操作\n");
    if(out_Queue(&Q,&val))
    {
    	printf("元素%d出队成功\n", val);
    }
    else
    {
    	printf("元素出队失败\n");
    }
    traverse_Queue(&Q);
    return 0;

}

void intiQueue(Queue *pQ)
{
	pQ->front=0;
	pQ->rear=0;
	pQ->pbase=(int *)malloc(sizeof(int)*6);
}

bool full_Queue(Queue *pQ)
{
	if((pQ->rear+1)%6==pQ->front)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool en_Queue(Queue *pQ,int val)
{
	if(full_Queue(pQ))
	{
		return false;
	}
	else
	{
		pQ->pbase[pQ->rear]=val;
		pQ->rear=(pQ->rear+1)%6;
		return true;
	}
}

void traverse_Queue(Queue *pQ)
{
	int i=pQ->front;
	while(i!=pQ->rear)
	{
		printf("%d ",pQ->pbase[i]);
		i=(i+1)%6;
	}
	printf("\n");
}

bool emput_Queue(Queue *pQ)
{
	if(pQ->front==pQ->rear)
	{
		return true;
	}
	else
	{
		return false;
	}
}


bool out_Queue(Queue *pQ,int *pval)
{
	if(emput_Queue(pQ))
	{
		return false;

	}
	else
	{
		*pval=pQ->pbase[pQ->front];
		pQ->front=(pQ->front+1)%6;
		return true;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值