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

本文通过两个示例介绍如何使用数组和链表实现队列数据结构,并演示了基本的队列操作,包括入队和出队。展示了队列满和队列空的状态处理。

3 文件夹:

代码:

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

/*
 * wuxiuwen
 *20120901
 *用数组描述队列
 */
/***************************/
#include"arrqueue.h"


int main()
{
    	int i;
	char c;
	printf("输入1为入栈,输入2为出栈,输入其他退出\n");
    	while(1)
	{
		while(scanf("%d",&i) != 1)
		    	continue;
		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;
}

*************************arrqueue.h*********************************

#include<stdio.h>

void push(char);
char pop();

*************************arrqueue.c*********************************

#include"arrqueue.h"
#define N 5

int top=-1;
char queue[N];
int rear =-1;
int head=0;


void push(char a)
{
	if(rear == head+4)
	{
		printf("队列满\n");
		return ;
	}
	++rear;
	queue[rear%5]=a;
}
char pop()
{
	char temp;
    	if(rear<head)
	{
		printf("队列空\n");
		rear = -1;
		head = 0;
		return ;
	}
	temp = queue[head%5];
	head++;
	return temp;
}

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

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


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

 

4文件夹

代码:

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

/*
 * wuxiuwen
 *20120901
 *用链表描述堆栈
 */
/***************************/
#include"listqueue.h"
//#define N 5


int main()
{
	//a[-1]=12;   可以访问,但是有可能出错
	//printf("%d\n",a[-1]);
    	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;
}

*************************arrqueue.h********************************* 

#include<stdio.h>

typedef struct node
{
            char data;
	            struct node * next;
}NODE;


void push(char);
char pop();

*************************arrqueue.c*********************************
 

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

#define N 5  
//队列的长度

static NODE *head =NULL;
static NODE *rear=NULL;
static int n=0;
NODE *getnode(char c)
{
	NODE *pnew=NULL;
	pnew=(NODE*)malloc(sizeof(NODE));
	if(pnew ==NULL)
	{
		printf("内存分配失败\n");
		return;
	}
	pnew->data=c;
	pnew->next=NULL;
}
void push(char a)
{
	NODE *pnew=NULL;
	if(n>=N)
	{
		printf("队列满\n");
		return ;
	}
	pnew=getnode(a);
	if(head ==NULL)
	{
		head=rear=pnew;
	}
	else
	{
		rear->next=pnew;
		rear=pnew;
	}
	n++;
}
char pop()
{
	char temp;
	NODE *p=NULL;
	if(head ==NULL)
	{
		printf("队列空");
		return ;
	}
	temp=head->data;
	p=head;
	head=head->next;
	if(head ==NULL)   //队列为空的后的操作
	    	rear =NULL;
	free(p);
	return temp;
}

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

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

**********************************************************************

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值