现在的位置: 首页 > 综合 > 正文

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

2013年08月30日 ⁄ 综合 ⁄ 共 2605字 ⁄ 字号 评论关闭

堆栈和队列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]#

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

抱歉!评论已关闭.