C---队列,栈的实现

栈和队列很早以前就知道有这东西了,兜兜转转也终于走到了这一步,都不难理解,总的来说可以看成//链表头插法和尾插法的变相使用//,但实现起来也有很多要注意的点

一:栈(后进先出,只有一个操作点)

栈的实现:顺序存储结构和链式存储结构

1:顺序栈:使用数组实现

#include<stdio.h>  //输出为:0,1,2,3,4,5,6,7,8,9
#include<stdlib.h>//9,8,7,6,5
#define maxsize 100//4
#define True 1
#define False 0
typedef struct
{
	int data[maxsize];
	int top;
}seqstact;
seqstact *create()//建立一个空栈 
{
	seqstact *s;
	s=(seqstact *)malloc(sizeof(seqstact));
	s->top=-1;//栈顶为空 
	return s;
}
int push(seqstact *s,int e)
{
	if(s->top==maxsize-1)//判断是否到达栈顶 
	{
		return False;
	}
	s->top++;
	s->data[s->top]=e;
	return True;
}
int pop(seqstact *s,int *e)
{
	if(s->top==-1)//判断是否到达栈底 
		return False;
		*e=s->data[s->top];
		s->top--;
		return True;
}
int gettop(seqstact *s,int *e)//得到此时栈顶元素 
{
	if(s->top==-1)
	{
		return False;
	}
	else
	{
		*e=s->data[s->top];
		return True;
	}
}
int main(void)
{
	seqstact *s;
	int x,y,z,i,result;
	s=create();
	printf("%d\n",s->top);
	for(i=0;i<10;i++)
	{
		result=push(s,i);
		if(result==True)	printf("%d ",i);
	}
	printf("\n");
	for(i=0;i<5;i++)
	{
		x=i;
		result=pop(s,&x);
		if(result==True) printf("%d ",x);
	}
	printf("\n");
	printf("%d",s->data[s->top]);
}

2:链栈:同链表相似,不同的是,链栈只能操作头节点后的节点,既栈顶。

#include<stdio.h> 
#include<stdlib.h>
#define ok 1
#define error 0
typedef struct node//声明栈的数据结构 
{
	int data;
	struct node *next;
}lsnode;
typedef struct
{
	lsnode *top;//栈顶 
	
	int size;//栈的大小 
}lstack;
lstack *create()//创建一个空栈 
{
	lstack *s;
	s=(lstack *)malloc(sizeof(lstack));
	s->top=(lsnode *)malloc(sizeof(lsnode));
	s->top->next=NULL; 
	s->size=0;
	return s;
}
int push(lstack *s,int e)
{
	lsnode *newtop;
	newtop=(lsnode *)malloc(sizeof(lsnode));
	newtop->data=e;
	if(s->size==0)//使用头插法实现栈 ,第一个进栈的需要特殊处理 
	{
		s->top->data=e;
		s->size++;
		free(newtop);
	}
	else
	{
		newtop->next=s->top;
		s->top=newtop;
		s->size++;
	}
	return ok;
}
int pop(lstack *s)//栈的输出 
{
	lsnode *p;
	int e;
	if(s->size==0)
	{
		return error;
	}
	else
	{
		p=s->top;
		e=p->data;
		s->top=s->top->next;
		free(p);
		return e;
	}
}
int reach_top(lstack *s)//输出此时的栈顶 
{
	int e;
	e=s->top->data;
	return e;
}
int main(void)
{
	lstack *s;
	int i,result,x;
	s=create();
	for(i=0;i<10;i++)
	{
		result=push(s,i);
		if(result==ok)	printf("%d\t",i);
	}
	for(i=0;i<5;i++)
	{
		x=pop(s);
		printf("%d\t",x);
	}
	printf("\n");
	x=reach_top(s);
	printf("%d\n",x);
}

注:在这里并没有考虑满栈情况,只要malloc还可以申请到空间,就不会出现满栈情况。

二:队列(先进先出,有头尾两个操作方向,首部插入,尾部删除)

1.线性存储(主要采用取余思想)

队空:队尾队头相等

队满:对尾+1==队头

#include<stdio.h>
#include<stdlib.h>
#define True 1
#define False 0
#define MAXSIZE 10
typedef struct
{
	int data[10];
	int top,end;
}queue;
queue *create()
{
	queue *ql;
	ql=(queue *)malloc(sizeof(queue));
	ql->end=ql->top=MAXSIZE-1;
	return ql;
}
int inqueue(queue *ql,int x)
{
	if((ql->end+1)%MAXSIZE==ql->top)
		return False;
	ql->end=(ql->end+1)%MAXSIZE;
	ql->data[ql->end]=x;
		return True; 
}
int empty(queue *ql)
{
	if(ql->end==ql->top)
		return False;
	return True;
}
int quit(queue *ql,int *e)
{
	if(empty(ql)){
		ql->top=(ql->top+1)%MAXSIZE;
		*e=ql->data[ql->top];
		return True;
	}
	return False;
}
int main(void)
{
	queue *ql;
	ql=create();
	int x,y,i,result;
	for(i=0;i<12;i++)
	{
		scanf("%d",&x);
		result=inqueue(ql,x);
		if(result==1)	printf("%d ",x);
	}
	printf("\n");
	for(i=0;i<12;i++)
	{
		result=quit(ql,&y);
		if(result==1)	printf("%d ",y);
	}
} 

2.链队

#include<stdio.h>
#include<stdlib.h>
#define True 1
#define False 0
typedef struct node
{
	int data;
	struct node *next;
}NODE;
typedef struct
{
	NODE *top;
	NODE *end;
}queue;
queue *create(){//初始化 
	queue *p;
	p=(queue *)malloc(sizeof(queue));
	p->top=p->end=(NODE *)malloc(sizeof(NODE));
	p->top->next=NULL;//确定指针指向 
	return p;
}
int IN(queue *ql,int e)//入队,尾插法 
{
	NODE *p;
	p->data=e;
	p->next=NULL;
	ql->end->next=p;//和栈不同,这里是尾部移动 ,此外,q->end无初值 
	ql->end=p;
	return True;
}
int empty(queue *ql)//判断是否为空 
{
	if(ql->end==ql->top)//当队尾和队头指向同一个地址时为空
		return True;
	return False; 
}
int push(queue *ql,int e)//出队 
{
	NODE *p;
	if(!empty(ql)){//有了这一步,就不用 if(ql->top->next==ql->end->next)
			return False;
		e=ql->top->next->data;
		p=ql->top->next;
		ql->top->next=p->next;
		free(p);
		return True;
	}
	else
		return False;
}
int main(void)
{
	queue *ql;
	ql=create();
	int x,y,i,result;
	for(i=0;i<5;i++)
	{
		scanf("%d",&x);
		result=IN(ql,x);
		if(result==1)
		printf("%d ",x);
	}
	printf("\n");
	for(i=0;i<5;i++)
	{
		result=push(ql,y);
		if(result)	printf("%d",y);
	}
}

猜你喜欢

转载自blog.csdn.net/printfxgd/article/details/80171056