数据结构 四、栈

一、定义

栈是限定仅在表尾进行插入和删除的线性表

二、栈的顺序存储结构

typedef struct
{
	selemtype data[maxsize];
	int top;//栈顶指针
}sqstack;

栈的顺序基本操作:
**(1)**进栈操作

status push(sqstack *s,selemtype e)
{
	if(s->top==maxsize-1)
	{
		rerurn error;
	}
	s->top++;
	s->data[s->rop]=e;
	return ok;
}

**(2)**出栈操作

status pop(sqstack *s,selemtype *e)
{
	if(s->top==-1)
	{
	return erreo;
	}
	*e=s->data[s->top];
	s->top--;
	return ok;
}

三、栈的链式存储结构

typedef struct stacknode
{
	selemtype data;
	struct stacknode *next;
}stacknode,*linkstackptr;

typedef struct linkstack
{
	linkstackptr top;
	int count;
}linkstack;

**(1)**进栈操作

status push(linkstack *s,selemtype e)
{
	linkstackptr s=(linkstackptr)malloc (sizeof(stacknode));
	s->data=e;
	s->next=s->top;
	s->top=s;
	s->count++;
	return ok;
}

**(2)**出栈操作

status pop(linkstack *s,selemtype *e)
{
	linkstackptr p;
	if(stackempty(*s))//为空
	{
		return error;
	}
	*e=s->top->data;
	p=s->top;
	s->top=s->top->next;//头结点为下一个节点
	free(p);
	s->count--;
	return ok;
}
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页