栈与队列的操作

一、栈以及相关操作

1.定义及特点

定义:只允许在- -端插入和删除的线性表;
允许插入和删除的- -端称为栈顶(top) ,另一端称为栈底(bottom)。
特点:先进后出
原因:进栈出栈在同一端

2.栈的主要操作

在这里插入图片描述

3.栈的数组表示-----顺序栈

在这里插入图片描述
top叫做栈顶指针 但不是真的指针,它只是指向数组的元素位置 所以叫它指针
在这里插入图片描述
进栈是top先++ 再放入值
出栈是先取出top所指位置的值再top–

4.具体代码

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

#define SIZE 10
enum re_val{MALLOC_OK = 100,MALLOC_NO,CREATE_OK,CREATE_NO,FULL_OK,FULL_NO,EMPTY_OK,EMPTY_NO};

struct stack_node{
	int stack_data[SIZE];

	int top;
};
typedef struct stack_node Stack;

int is_malloc_ok(Stack * stack){
	if(NULL == stack){
		printf("malloc is error!\n");
		return MALLOC_NO;
	}
	else{
		return MALLOC_OK;
	}
}
int create_stack(Stack ** stack){
	*stack = (Stack*)malloc(sizeof(Stack));
	if(MALLOC_NO == is_malloc_ok(*stack)){
		printf("create is error\n");
		return CREATE_OK;
	}
	else{
		return CREATE_NO;
	}
}

void init_stack(Stack * stack){
	stack->top = -1;
	printf("initstack is success\n");
}

int stack_full(Stack * stack){
	if(stack->top >= SIZE){
		return FULL_NO;
	}
	else{
		return FULL_OK;
	}
}

void push(Stack * stack,int num){

	if(FULL_NO == stack_full(stack)){
		printf("stack already is full\n");
	}
	else{
		stack->stack_data[++stack->top] = num;
		printf("push num=%d is success\n",num);
	}
}

int stack_empty(Stack * stack){
	if(stack->top <= -1){
		return EMPTY_OK;
	}
	else{
		return EMPTY_NO;
	}
}
int pop(Stack * stack){
	if(EMPTY_OK == stack_empty(stack)){
		printf("pop:stack is empty!\n");
		return EMPTY_OK;
	}
	else{
		return stack->stack_data[stack->top--];
	}

}

void get_stack_top(Stack * stack){
	if(stack->top <= -1){
		printf("get_stack_top:stack is empty\n");
	}
	else{
		printf("get_stack_top:the top number is:%d\n",stack->stack_data[stack->top]);
	}
}

void empty_stack(Stack * stack){
	if(EMPTY_OK == stack_empty(stack)){
		printf("empty_stack:stack is empty\n");
	}
	else{
		stack->top = -1;
		printf("empty_stack:stack empty success\n");
	}
}

int main(){
	int i;
	Stack * stack = NULL;

	create_stack(&stack);

	init_stack(stack);

	for(i = 0;i < 10;i++){

		push(stack,i+1);

	}
	
	get_stack_top(stack);

	for(i = 0;i < 5;i++){
		
		int num = pop(stack);

		if(num == EMPTY_OK){
			printf("pop fail\n");
		}
		else{
			printf("pop num is:%d\n",num);
		}
	}

	empty_stack(stack);

	free(stack);
}

4.栈的应用举例(表达式在栈中的计算)

在这里插入图片描述
具体代码:

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

#define MAX 100

struct operand
{
    int data[MAX];
    int top;
};

struct operator_ch
{
    int top;
    char data[MAX];
};


typedef struct operand OPND;
typedef struct operator_ch OPCH;

int is_empty_OPND(OPND *stack)
{
	if(stack->top == -1)
	{
		return -1;
	}

	return 0;
}

int is_empty_OPCH(OPCH *stack)
{
	if(stack->top == -1)
	{
		return -1;
	}

	return 0;
}


int get_OPCH_top(OPCH *stack)
{
	if(is_empty_OPCH(stack) == -1)
	{
		return -1;
	}

	return stack->data[stack->top];
}

void push_OPND(OPND *stack, int num)
{
    stack->data[++(stack->top)] = num;
	printf("push num is:%d\n",stack->data[stack->top]);
}

void push_OPCH(OPCH * chstack, char ch)
{
    chstack->data[++(chstack->top)] = ch;
	printf("push CH is:%c\n",chstack->data[chstack->top]);
}

int pop_OPND(OPND *stack)
{
	if(is_empty_OPND(stack) == -1)
	{
		return -1;
	}
    
	int ch = stack->data[stack->top];

	(stack->top)--;

	return ch;
} 

char pop_OPCH(OPCH *chstack)
{
	if(is_empty_OPCH(chstack) == -1)
	{
		return -1;
	}
    
	char ch = chstack->data[chstack->top];

	(chstack->top)--;

	return ch;
} 

int get_priority(char ch)
{
	if(ch == '+' || ch == '-')
	{
		return 1;
	}

	if(ch == '*' || ch == '/')
	{
		return 2;
	}
	
}

int compare_priority(char op_ch, char ch)
{
	if(get_priority(op_ch) > get_priority(ch))
	{
		return 1;
	}
	
	return -1;
}

int count(int a, int b, char ch)
{
    int result;

	switch(ch)
	{
		case '+':
		{
            result = a + b;
			break;
		}

		case '-':
		{
			result = a - b;
			break;
		}
		
		case '*':
		{
			result = a * b;
			break;
		}
		
		case '/':
		{
			result = a / b;
			break;
		}
	}

	return result;
}

void create_stack(OPND ** stack){

	*stack = (OPND*)malloc(sizeof(OPND));

}

void create_chstack(OPCH ** chstack){

	*chstack = (OPCH*)malloc(sizeof(OPCH));
}

void init_OPND_stack(OPND *stack)
{
	stack->top = -1;
}

void init_OPCH_chstack(OPCH *stack)
{
	stack->top = -1;
}


int main()
{
    char ch;
	char op_ch;
	int opr_num;
	int a,b;
	int result;
	OPND * stack = NULL;
	OPCH * chstack = NULL;

	create_stack(&stack);
	create_chstack(&chstack);

	init_OPND_stack(stack);
	init_OPCH_chstack(chstack);

	while((ch = getchar()) != '\n')
	{
        if(ch >= '0' && ch <= '9')
		{
			opr_num = ch -'0';

            push_OPND(stack,opr_num);
		}

		if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')')
		{
			if(ch == '('){
				push_OPCH(chstack,ch);
				continue;
			}
			else if(ch == ')'){
				while(get_OPCH_top(chstack) != '('){
					op_ch = pop_OPCH(chstack);
					
					b = pop_OPND(stack);
					a = pop_OPND(stack);
					result = count(a,b,op_ch);
					push_OPND(stack,result);
				}
				pop_OPCH(chstack);
			}
			else{
				if(get_OPCH_top(chstack) == '('){   
					push_OPCH(chstack,ch);
					continue;
             }

			do{
					op_ch = get_OPCH_top(chstack);
				
					if(op_ch == -1){
						push_OPCH(chstack,ch);
						break;
					}
					else{
						if(compare_priority(op_ch,ch) < 0){
							push_OPCH(chstack,ch);
							break;
						}
						else{
							b = pop_OPND(stack);
							a = pop_OPND(stack);
							op_ch = pop_OPCH(chstack);
							if(a == -1 || b == -1 || op_ch == -1){
								printf("stcak is empty!\n");
							}
							else{
								result = count(a,b,op_ch);
								push_OPND(stack,result);
							}
		
						}
					}

				}while(compare_priority(op_ch,ch) > 0);
			}
		}
	}
		while(is_empty_OPND(stack) != -1 && is_empty_OPCH(chstack) != -1){
			a = pop_OPND(stack);
			b = pop_OPND(stack);
	    	op_ch = pop_OPCH(chstack);
			result = count(b,a,op_ch);
			push_OPND(stack,result);
		}

		printf("result is %d\n",result);

		free(stack);
		free(chstack);
	
	return 0;
}

5.链式栈

创建两个结构体 一个是链表(带next指针)一个是栈内结点(只带各种数据 没有指针)

1.具体代码:

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

#define MAX 20
enum rel{MALLOC_NO,MALLOC_OK,PUSH_NO,PUSH_OK,EMPTY_OK,EMPTY_NO,POP_NO,POP_OK};
struct node{
	int num;
	char name[MAX];

	struct node *next;
};
typedef struct node Node;
typedef Node * Link;

struct stack{
	int num;
	char name[MAX];
};
typedef struct stack Stack;

int create_node(Link * new_node){
	*new_node = (Link)malloc(sizeof(Node));
	if(*new_node == NULL){
		printf("malloc error\n");
		return MALLOC_NO;
	}
	else{
		return MALLOC_OK;
	}
}

void create_stack(Link * stack){

	create_node(stack);

	(*stack)->next = NULL;
}

int push(Link stack,Stack node){
	Link new_node;
	if(MALLOC_NO == create_node(&new_node)){
		printf("push stack is error\n");
		return PUSH_NO;
	}
	else{
		new_node->num = node.num;
		strcpy(new_node->name,node.name);

		new_node->next = stack->next;
		stack->next = new_node;

		printf("push stack name is:%s,push stack num is:%d\n",node.name,node.num);

		return PUSH_NO;
	}
}

int stack_empty(Link stack){
	if(stack->next == NULL){
		printf("stack_empty:stack is empty\n");
		return EMPTY_OK;
	}
	else{
		return EMPTY_NO;
	}
}

int get_stack(Link stack){
	Stack node;
	if(stack->next == NULL){
		printf("get_stack:stack is empty\n");
		return EMPTY_OK;
	}
	else{
		node.num = stack->next->num;
		strcpy(node.name,stack->next->name);
		printf("get_stack top name is%s,num is:%d\n",node.name,node.num);
		return EMPTY_NO;
	}
}

int pop(Link stack){
	Stack node;
	Link p = NULL;
	p = stack->next;
	if(stack_empty(stack) == EMPTY_OK){
		printf("pop error\n");
		return POP_NO;
	}
	else{
		stack->next = p->next;

		node.num = p->num;
		strcpy(node.name,p->name);

		free (p);
		printf("pop node name is:%s,num is:%d\n",node.name,node.num);

		return POP_OK;
	}
	
}


int main(){
	Link stack = NULL;
	Stack node;
	int i;

	create_stack(&stack);

	for(i = 0;i < 5;i++){
		node.num = i+1;
		printf("please input node name:\n");
		scanf("%s",node.name);

		push(stack,node);
	}

	get_stack(stack);

	for(i = 0;i < 5;i++){
		
		pop(stack);
	}

	free(stack);	
	
}

二、队列以及相关操作

1.定义

在这里插入图片描述在这里插入图片描述

2.主要操作

在这里插入图片描述
在这里插入图片描述

3.具体代码(顺序队列)

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

#define MAX 10

enum rel{EMPTY_OK=100,EMPTY_NO,FULL_OK,FULL_NO,PUSH_OK,PUSH_NO};
struct  quece_data{
	int quece[MAX];
	int top;
	int buttom;
};

typedef struct quece_data Quece;

void create_quece(Quece ** quece){
	*quece = (Quece *)malloc(sizeof(struct quece_data));
}	

void init_quece(Quece * quece){
	quece->top = -1;
	quece->buttom = -1;
}

int is_quece_empty(Quece * quece){
	if(quece->top <= quece->buttom){
		printf("quece is empty");
		return EMPTY_OK;
	}
	else{
		return EMPTY_NO;
	}
}

int is_quece_full(Quece * quece){
	if(quece->buttom == -1){
		if(quece->top == MAX - 1){
			printf("quece is full\n");
			return FULL_OK;
		}
	}
	else if(((quece->top -quece->buttom) +1) == MAX){
			printf("quece is full have one add don't insert\n");
			return FULL_OK;
		}
	else{
		return FULL_NO;
	}	
}
int push_quece(Quece * quece,int num){
	int i;
	if(is_quece_full(quece) == FULL_OK){
		printf("push fail\n");
//		exit(FULL_OK);
	}
		if(quece->top < MAX -1){
			quece->quece[++quece->top] = num;
			printf("push num is:%d\n",quece->quece[quece->top]);
		}
		else{
			for(i = quece->buttom;i <= quece->top;i++){
				quece->quece[i - quece->buttom] = quece->quece[i];
			}
			quece->top = quece->top - quece->buttom;
			quece->buttom = 0;
			quece->quece[++quece->top] = num;
		}
		return PUSH_OK;	
}

void pop_quece(Quece * quece){
	int pop_num;
	if(is_quece_empty(quece) == EMPTY_OK){
		printf("pop fail\n");
	}
	else{
		pop_num = quece->quece[++quece->buttom];
		printf("pop_num is:%d\n",pop_num);
	}
}
int main(){
	Quece *quece = NULL;
	int i;

	create_quece(&quece);

	init_quece(quece);

	for(i = 0;i < 10;i++){

		push_quece(quece,i+1);
	}

	for(i = 0;i < 5;i++){

		pop_quece(quece);

	}

	free(quece);
	
}

4.具体代码(顺序循环队列)

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

#define MAX 10

enum rel{EMPTY_OK=100,EMPTY_NO,FULL_OK,FULL_NO,PUSH_OK,PUSH_NO};
struct  quece_data{
	int quece[MAX];
	int top;
	int buttom;
};

typedef struct quece_data Quece;

void create_quece(Quece ** quece){
	*quece = (Quece *)malloc(sizeof(struct quece_data));
}	

void init_quece(Quece * quece){
	quece->top = 0;
	quece->buttom = 0;
}

int is_quece_empty(Quece * quece){
	if(quece->top == quece->buttom){
		printf("quece is empty");
		return EMPTY_OK;
	}
	else{
		return EMPTY_NO;
	}
}

int is_quece_full(Quece * quece){
	if((quece->top + 1)%MAX == quece->buttom){
			printf("quece is full\n");
			return FULL_OK;
	}
	else{
		return FULL_NO;
	}	
}
int push_quece(Quece * quece,int num){
	if(is_quece_full(quece) == FULL_OK){
		printf("push fail\n");
//		exit(FULL_OK);
	}
	else{
		quece->quece[quece->top] = num;
		printf("push num is:%d\n",quece->quece[quece->top]);
		quece->top = (quece->top + 1)%MAX;
		return PUSH_OK;
	}

}

void pop_quece(Quece * quece){
	int pop_num;
	if(is_quece_empty(quece) == EMPTY_OK){
		printf("pop fail\n");
	}
	else{
		pop_num = quece->quece[quece->buttom];
		quece->buttom = (quece->buttom + 1)%MAX;
		printf("pop_num is:%d\n",pop_num);
	}
}
int main(){
	Quece *quece = NULL;
	int i;

	create_quece(&quece);

	init_quece(quece);

	for(i = 0;i < 10;i++){

		push_quece(quece,i+1);
	}

	for(i = 0;i < 5;i++){

		pop_quece(quece);

	}

	free(quece);
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alex、WY

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

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

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

打赏作者

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

抵扣说明:

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

余额充值