栈、链、队列操作的 c代码
来源:互联网 发布:java 线程池 executor 编辑:程序博客网 时间:2024/04/30 22:44
栈操作
//进栈算法
#include "stdio.h"
#define stacksize 100 /*定义stacksize为常数100 */
int push(int s[],int x,int *ptop)
{
int top;
top=*ptop; //ptop是指针变量;*ptop获得实际栈顶指针
if ( top==stacksize ) //栈满
{
printf("overflow/n");
return 0;
}
else
{
s[top]=x;
top++;
*ptop=top; //实际栈顶指针加1,返回到调用函数处
return 1;
}
}
//出栈算法
int pop(int s[],int *py,int *ptop)
{
int top;
top=*ptop; //ptop是指针变量;*ptop获得实际栈顶指针
if (top==0) //栈空
{
printf("stack empty/n");
return 0;
}
else
{
--top;
*py=s[top]; //实际栈顶指针减1,返回到调用函数处
*ptop=top;
return 1;
}
}
int main()
{
static int s[stacksize];
int top=0,result,y;
int i;
result=push(s,11,&top); //将11压进栈中
result=push(s,22,&top); //将22压进栈中
result=push(s,33,&top); //将33压进栈中
printf("top=%d/n",top);
for (i=0;i<3;i++)
{
result=pop(s,&y,&top); //将11从栈中弹出
printf("top=%d,y=%d/n",top,y);
}
}
链栈算法
//进栈算法
#include "stdio.h"
typedef struct node
{
int data;
int n;
struct node *link;
}NODE;
NODE *top,*head;
void push(int x)
{
NODE *temp = (NODE *) malloc(sizeof(NODE));
temp->data=x; //将数据插入节点
temp->link=NULL;
temp->link = top; //调整节点指针
top = temp;
}
int pop(int *py)
{
NODE *temp=top;
if (top == head) //判断是否栈空
{
printf("stach empty!");
return 0;
}
else
{
*py = top->data; //将栈中数据读出
top = top->link; //调整栈指针
free (temp);
return 1;
}
}
int main()
{
int i,b,y;
head=top=(NODE *)malloc(sizeof(NODE));
push(12); //将11压进栈中
push(13); //将11压进栈中
push(14); //将11压进栈中
for( i= 0;i<4;i++)
{
if ( (b=pop(&y) )!=0) //将11从栈中弹出
printf("%d/n",y);
else
break;
}
getch();
}
队列操作
//循环队列中加入一个元素的算法:
//设Q[MAX]表示循环队列
#define MAX 7
#include "stdio.h"
int EnQueue(int *Q,int x,int *pf,int *pr)
{
int front,rear;
front = *pf;
rear=*pr;
if ( (rear+1) % MAX == front ) //判断队满
return 0;
else
{
Q[rear]=x; //将数据存入队列
rear = ( rear+1 ) % MAX; //队尾指针移位
*pr=rear; //保存队尾指针
return 1;
}
}
int DeQueue(int *Q,int *py,int *pf,int *pr)
{
int front,rear;
front=*pf;
rear=*pr;
if (front==rear) //判断队空
return 0;
else
{
*py=Q[front]; //将数据读出
front=(front+1) % MAX; //队头指针移动
*pf=front; //保存队头指针
return 1;
}
}
int main()
{
static int s[MAX];
int rear = 0,front = 0;
int b=0;
EnQueue(s,11,&rear,&front); //入队操作
EnQueue(s,12,&rear,&front); //入队操作
EnQueue(s,13,&rear,&front); //入队操作
DeQueue(s,&b,&rear,&front); //出队操作
printf("%d/n",b);
DeQueue(s,&b,&rear,&front); //出队操作
printf("%d/n",b);
DeQueue(s,&b,&rear,&front); //出队操作
printf("%d/n",b);
}
链队列
#include "stdio.h"
//链队列的类型定义:
typedef struct Qnode
{
int data;
struct Qnode *next;
}Qnode;
Qnode *front; //队头指针
Qnode *rear; //队尾指针
int EnQueue( int *e)
{
Qnode *p;
p=(Qnode *)malloc(sizeof(Qnode));
if(p == NULL)
return 0;
p->data=*e; //将数据加入到队列中
p->next=NULL;
rear->next=p; //调整队尾指针位置,为下一次压栈做准备
rear=p;
return 1;
}
int DeQueue( int *e)
{
Qnode *p;
if(front==rear)
return 0; //队空
p=front->next; //获得出队指针
*e=p->data; //将待出队节点的数据取出
front->next= p->next; //调整对头指针,为下次出队做准备
if(p == rear) //若队头指针追上队尾指针,调整队头指针,此时队空
front = rear;
free(p); //释放已出队数据
return 1;
}
int main()
{
int a = 11;
int b = 0;
rear=front=(Qnode *)malloc(sizeof(Qnode));
EnQueue(&a); //插入队列
printf("%d/n",a);
DeQueue(&b); //弹出队列
printf("%d/n",b);
}
- 栈、链、队列操作的 c代码
- 栈和队列的操作(c)
- C语言链队列的基本操作
- 链队列的基本操作(C++)
- c语言队列基本操作代码
- 队列的c代码实现
- 顺序队列(循环队列)和链队列的C代码实现
- 软件开发实践中的入队列和出队列操作的C代码示例
- C++:利用两个队列实现栈的操作
- 数据结构(C语言版)链队列的基本操作
- C语言实现链队列的基本操作
- C语言数据结构单链队列的操作集
- 数据结构中栈与队列的c语言代码实现
- 单链队列的一个C代码实例
- 链队列的操作
- 链队列的操作
- c实现队列的基本操作
- 队列的操作(C语言实现)
- 该死的shellext32.dll
- 记录我的第一次面试
- c 语言指针的识别
- C# 二维数组 回形输出
- 初次安装SQL Server遇到的问题!
- 栈、链、队列操作的 c代码
- 图片转换2进制
- 程序员怎样学数学
- lrc 歌词解析项目
- VARIANT类型如何与其他字符串类型转换?
- 这样的爱情,你信吗?
- 偶数 素数
- 偶数 素数
- 字符串转换_BSTR/LPSTR/LPWSTR/Char