栈的实现
顺序存储实现栈
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];//数组
int top;
}SqStack;
void InitStack(SqStack &S)
{
S.top = -1;//代表栈为空
}
bool StackEmpty(SqStack S)
{
if (-1 == S.top)
{
return true;
}
return false;
}
bool Push(SqStack& S, ElemType x)
{
if (S.top == MaxSize - 1)
{
return false;//栈满了
}
S.data[++S.top] = x;
return true;//返回true就是入栈成功
}
//获取栈顶元素
bool GetTop(SqStack S, ElemType &x)
{
if (StackEmpty(S))//栈为空
{
return false;
}
x = S.data[S.top];
return true;
}
bool Pop(SqStack& S, ElemType& x)
{
if (StackEmpty(S))//栈为空
{
return false;
}
x = S.data[S.top--];//等价于x = S.data[S.top];再做 S.top--;
return true;
}
int main()
{
SqStack S;
bool flag;
ElemType m;//存储拿出来的栈顶元素的
InitStack(S);//初始化
flag = StackEmpty(S);
if (flag)
{
printf("栈是空的\n");
}
Push(S, 3);//入栈元素3
Push(S, 4);//入栈元素4
Push(S, 5);
flag = GetTop(S, m);//获取栈顶元素,但是S.top值不变
if (flag)
{
printf("获取栈顶元素为 %d\n", m);
}
flag = Pop(S, m);//弹出栈顶元素
if (flag)
{
printf("弹出元素为 %d\n", m);
}
return 0;
}
链式存储实现栈
队列
循环队列
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 5
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];//数组,存储MaxSize-1个元素
int front, rear;//队列头 队列尾
}SqQueue;
void InitQueue(SqQueue& Q)
{
Q.front = Q.rear = 0;
}
bool isEmpty(SqQueue Q)
{
if (Q.front == Q.rear)
{
return true;
}
return false;
}
bool EnQueue(SqQueue& Q,ElemType x)
{
if ((Q.rear + 1) % MaxSize == Q.front)
{
return false;//队列满了
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;//向后移动一格
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x)
{
if (Q.front == Q.rear)//队列为空
{
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
int main()
{
SqQueue Q;
bool ret;//存储返回值
ElemType element;//存储出队元素
InitQueue(Q);//初始化循环队列
ret = isEmpty(Q);
if (ret)
{
printf("队列为空\n");
}
else {
printf("队列不为空\n");
}
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
ret = EnQueue(Q, 6);
ret = EnQueue(Q, 7);
if (ret)
{
printf("入队成功\n");
}
else {
printf("入队失败\n");
}
ret = DeQueue(Q, element);
if (ret)
{
printf("出队成功,元素值为 %d\n", element);
}
else {
printf("出队失败\n");
}
ret = DeQueue(Q, element);
if (ret)
{
printf("出队成功,元素值为 %d\n", element);
}
else {
printf("出队失败\n");
}
return 0;
}
队列的链式存储
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LinkNode{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;//链表头 链表尾
}LinkQueue;//先进先出
void InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));//头和尾指向同一个结点
Q.front->next=NULL;
}
bool IsEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
return true;
else
return false;
}
//入队,尾部插入法
void EnQueue(LinkQueue &Q,ElemType x)
{
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=x;s->next=NULL;
Q.rear->next=s;//rear始终指向尾部
Q.rear=s;
}
//出队 头部删除法
bool DeQueue(LinkQueue &Q,ElemType &x)
{
if(Q.front==Q.rear) return false;//队列为空
LinkNode *p=Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据
x=p->data;
Q.front->next=p->next;//断链
if(Q.rear==p)//删除的是最后一个元素
Q.rear=Q.front;//队列置为空
free(p);
return true;
}
//《王道C督学营》课程
//王道考研数据结构 3.2.3 队列的链式存储
//头部删除法,尾部插入法
int main()
{
LinkQueue Q;
bool ret;
ElemType element;//存储出队元素
InitQueue(Q);
EnQueue(Q,3);
EnQueue(Q,4);
EnQueue(Q,5);
EnQueue(Q,6);
EnQueue(Q,7);
ret=DeQueue(Q,element);
if(ret)
{
printf("出队成功,元素值为 %d\n",element);
}else{
printf("出队失败\n");
}
}
斐波那契数列
#include <stdio.h>
#include <stdlib.h>
//Fib是递归函数
int Fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return Fib(n-1)+Fib(n-2);
}
//王道数据结构 斐波那契数列
//递归 函数调用自身
//0 1 1 2 3 5
//f(n)=f(n-1)+f(n-2)
//考研不是很重要,了解即可
int main()
{
int num;
while(scanf("%d",&num)!=EOF)
{
printf("Fib(%d) = %d\n",num,Fib(num));
}
}
//题目 n个台阶,每次只能上1个台阶,或者2个台阶,n个台阶,有多少种走法