顺序栈、链栈基本操作
来源:互联网 发布:八戒商标查询软件 编辑:程序博客网 时间:2024/06/06 02:48
// Stack.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
//*
#define STACK_MAX_SIZE 7
int StackData[STACK_MAX_SIZE] = {'A','B','C','D','E','F','G'};
//*/
/*
#define STACK_MAX_SIZE 14
int StackData[STACK_MAX_SIZE] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N'};
*/
/*顺序栈类型定义*/
typedef struct
{
int data[STACK_MAX_SIZE];
int top;
}sqStack;
/*链栈类型定义*/
struct LinkStack
{
int data;
struct LinkStack *link;
};
typedef struct LinkStack lkStack;
/*顺序栈基本操作函数定义部分*/
void sqInitialize(sqStack *stack)
{
stack->top = 0;
}
int sqIsEmpty(sqStack *stack)
{
if(stack->top == 0)
return(1);
else
return(0);
}
int sqIsFull(sqStack *stack)
{
if(stack->top >= STACK_MAX_SIZE)
return(1);
else
return(0);
}
int sqPush(sqStack *stack,int data)
{
if(sqIsFull(stack))
{
printf("堆栈已満!/n");
return(0);
}
else
{
stack->data[stack->top++] = data;
return(1);
}
}
int sqPop(sqStack *stack,int *p)
{
if(sqIsEmpty(stack))
{
printf("堆栈已空!/n");
return(0);
}
else
{
*p = stack->data[--stack->top];
return(1);
}
}
int sqGetTop(sqStack *stack,int *p)
{
if(sqIsEmpty(stack))
{
printf("堆栈已空!/n");
return(0);
}
else
{
*p = stack->data[stack->top];
return(1);
}
}
void sqWriteValue(sqStack *stack,int array[],int n)
{
int i;
for(i=0;i<n;i++)
if(sqPush(stack,array[i])) printf("Stack[%d] = %c/t",stack->top-1,array[i]);
}
void sqPrintValue(sqStack *stack)
{
int StackData,i;
i=0;
while(sqPop(stack,&StackData))
printf("Stack[%d] = %c/t",stack->top,StackData);
printf("/n");
}
/*链栈基本操作函数定义部分*/
void lkInitialize(lkStack *top)
{
top = NULL;
}
int lkIsEmpty(lkStack *top)
{
if(top == NULL)
return(1);
else
return(0);
}
int lkPush(lkStack *top,int data)
{
lkStack *q;
q = (lkStack *)malloc(sizeof(lkStack));
if(q==NULL)
return(0);
else
{
q->data = data;
q->link = NULL;
top->link = q;
top = q;
return(1);
}
int lkPop(struct LinkStack *stack,int *p)
{
struct LinkStack *q;
if(lkIsEmpty(stack))
{
printf("堆栈已空!/n");
return(0);
}
else
{
q = stack;
*p = stack->data;
stack = stack->next;
//free(q);
return(1);
}
}
int lkGetTop(struct LinkStack *stack,int *p)
{
if(lkIsEmpty(stack))
{
printf("堆栈已空!/n");
return(0);
}
else
{
*p = stack->data;
return(1);
}
}
void lkWriteValue(struct LinkStack *stack,int n)
{
int i;
for(i=0;i<n;i++)
if(lkPush(stack,i*10)) printf("Stack[%d] = %d/t",i,i*10);
}
void lkPrintValue(struct LinkStack *stack,int n)
{
int StackData,i;
i=0;
for(i=0;i<n;i++)
{
lkPop(stack,&StackData);
printf("Stack[%d] = %d/t",i++,StackData);
}
printf("/n");
}
int main(int argc, char* argv[])
{
sqStack *stack1;
struct LinkStack *stack2;
//顺序栈处理部分
sqInitialize(stack1);
printf("/n顺序栈[入栈]数据........./n");
sqWriteValue(stack1,StackData,STACK_MAX_SIZE);
printf("/n顺序栈[出栈]数据........./n");
sqPrintValue(stack1);
/*
//链栈处理部分
lkInitialize(stack2);
printf("/n链栈入栈数据........./n");
lkWriteValue(stack2,10);
printf("/n链栈出栈数据........./n");
lkPrintValue(stack2,10);
*/
return 0;
}
- 顺序栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈、链栈基本操作
- 顺序栈与链栈基本操作
- 顺序栈的基本操作
- 计算机工程专业,如何找paper?
- 大学生的艰难 无奈 呵呵
- 国际象棋“皇后”问题的回溯算法
- 天气预报的代码
- 插花问题的“动态规划法”算法
- 顺序栈、链栈基本操作
- 顺序队列基本操作
- 二叉树创建及遍历算法(递归及非递归)
- 二叉树的创建、前序遍历、中序遍历、后序遍历
- Source引擎 光能传递和凹凸贴图的革命性结合(转载)
- 我的任务:脚本语言,GUI,地图编辑器
- $_SERVER
- asp.net 2.0根据域名查ip C#版
- asp.net 2.0教程 Visual Studio2005的安装