数据结构笔记五-1 (20120901)

堆栈和队列1:

程序说明:
1 文件夹:利用数组来进行堆栈;
2 文件夹:利用链表来进行堆栈;
3 文件夹:利用数组来进行队列;
4 文件夹:利用链表来进行队列;


1..函数的现场保护等都是堆栈的事情:
利用栈来进行图的深度优先遍历:

2.堆栈和队列的特点:
不同点:
 堆栈是先入后出;同一端进出
 队列是先出后入;两端进出
 依次输入a,b,c,不考虑出栈的清空,队列只有一种,堆栈有五种。

相同点:
 都是线性表;
 访问都是从端口进行;
队列的上溢现象,假溢出。
 
其他:
1. fflsh(stdin); 清空缓冲区
2. .h文件中都不放置全局变量的定义等等。

 

1文件夹

代码:

*************************main.c******************************

/*
 * wuxiuwen
 *20120901
 *用数组描述模拟堆栈,利用top来判断堆栈是满来还是到底了。
 */
/***************************/
#include"stack.h"
//#define N 5


int main()
{
	//a[-1]=12;   可以访问,但是有可能出错
	//printf("%d\n",a[-1]);
    	int i;
	char c;
	printf("输入1为入栈,输入2为出栈,输入其他退出\n");
    	while(1)
	{
		while(scanf("%d",&i) !=1)
		{
	    		printf("输入错误\n");
		}
		switch(i)
	    	{
			case 1:getchar();
			      // fflush(stdin);
			       scanf("%c",&c);
			       push(c);
			       break;
			case 2:c=pop();
			       printf("%c\n",c);
			       break;
			default: return ;
		}
	}
    	return 0;
}

*************************stack.h******************************
#include<stdio.h>

void push(char);
char pop();

*************************stack.c******************************
#include"stack.h"
#define N 5
int top=-1;
char stack[N]="";

void push(char a)
{
	if(top>=(N-1))
    	{
		printf("栈满\n");
		return ;
	}	 
     	stack[top+1]=a;
	++top;	
}
char pop()
{
	if(top<0)
	{
		printf("栈空\n");
		return ;
	}
	top--;
	return stack[top+1];
}


*************************执行结果****************************

[root@localhost 1]# gcc main.c stack.c
[root@localhost 1]# ./a.out
输入1为入栈,输入2为出栈,输入其他退出
1
a
1
b
1
c
1
d
1
e
1
f
栈满
2
e
2
d
2
c
2
b
2
a
2
栈空

3
[root@localhost 1]#
/**************************************************************/

2文件夹
代码:
************************main.c******************************
/*
 * wuxiuwen
 *20120901
 *用链表描述模拟堆栈
 */
/***************************/
#include"liststack.h"

int main()
{
    	int i;
	char c;
	printf("输入1为入栈,输入2为出栈,输入其他退出\n");
    	while(1)
	{
		scanf("%d",&i);
		switch(i)
	    	{
			case 1:getchar();
			      // fflush(stdin);
			       scanf("%c",&c);
			       push(c);
			       break;
			case 2:c=pop();
			       printf("%c\n",c);
			       break;
			default: 
			       return;
		}
	}
    	return 0;
}

**********************liststack.h****************************
#include<stdio.h>

typedef struct node
{
	char data;
	struct node *next;
}NODE;
void push(char);
char pop();

**********************liststack.c****************************

#include"liststack.h"
#include<stdlib.h>

#define N 5


int n=0;

static NODE *top=NULL;
static NODE *pnew =NULL;
static NODE *stack = NULL;

void push(char a)
{
	if(n >= N)
	{
		printf("stack is full\n");
		return ;
	}
	pnew = (NODE *)malloc(sizeof(NODE));
	if(pnew == NULL)
	{
		printf("内存分配失败\n");
		return ;
	}
	pnew ->data=a;
	pnew ->next=NULL;

	if(top ==NULL)
	    top = stack = pnew;
	else
	    pnew->next=top;
	    top =pnew;
	n++;
}
char pop()
{
	char temp;
	NODE *p=top;
	if(top == NULL)
	{
		printf("栈空\n");
		return ;
	}
	top= top->next;
	temp=p->data;
	free(p);
	n--;
	return temp;
}

***************************执行结果*********************************

[root@localhost 2]# ./a.out
输入1为入栈,输入2为出栈,输入其他退出
1
a
1
b
1
c
1
d
1
e
1
f
stack is full
2
e
2
d
2
c
2
b
2
a
2
栈空

2
栈空

3
[root@localhost 2]#

/*********************************************************************/
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值