目录
1.栈的定义与特点
2.队列的定义与特点
3.案例引入
4.栈的表示和操作的实现
1.顺序栈的表示
代码示例:
2.顺序栈的初始化
代码示例:
3.判断栈是否为空
代码示例:
4.求顺序栈长度
代码示例:
5.清空顺序栈
代码示例:
6.销毁顺序栈
代码示例:
7.顺序栈的入栈
代码示例:
8.顺序栈的出栈
代码示例:
5.链栈的表示和实现
代码示例:
1.链栈的初始化
代码示例:
2.判断链栈是否为空
代码示例:
3.链栈的入栈
代码示例:
4.链栈的出栈
代码示例:
5.取栈顶元素
代码示例:
6.栈与递归
1.递归问题的求法
2.递归的定义
3.递归的优缺点
7.队列的表示和操作
1.队列的抽象数据类型定义
2.队列的顺序表示和实现
代码示例:
3.解决假上溢的办法——循环队列
4.循环队列的类型定义
代码示例:
5.队列的初始化
代码示例:
6.求队列长度
代码示例:
7循环队列入队
代码示例:
8.循环队列出队
代码示例:
9.取队头元素
代码示例:
8.链队列
1.链队的类型定义
代码示例:
2.链队初始化
代码示例:
3.销毁链队列
代码示例:
4.将元素e入队
代码示例:
5.链队列出队
代码示例:
6.求链队列的队头元素
代码示例:
9.总的代码
1.栈的定义与特点
2.队列的定义与特点
3.案例引入
4.栈的表示和操作的实现
1.顺序栈的表示
代码示例:
#define maxsize 100;
typedef struct
{int *base;int *top;int stacksize;
}sqstack;
2.顺序栈的初始化
代码示例:
int initstack(sqstack &s)
{s.base = new int[100];s.top = s.base;s.stacksize = 100;return 1;
}
3.判断栈是否为空
代码示例:
int stackempty(sqstack &s)
{if(s.top == s.base) return true;else return false;
}
4.求顺序栈长度
代码示例:
int stacklength(sqstack &s)
{return s.top - s.base;
}
5.清空顺序栈
代码示例:
int clearstack(sqstack &s)
{if(s.base != NULL) s.top = s.base;return 1;
}
6.销毁顺序栈
代码示例:
int destorystack(sqstack &s)
{if(s.base != NULL){delete s.base;s.stacksize = 0;s.base = s.top = NULL;}return 1;
}
7.顺序栈的入栈
代码示例:
int push(sqstack &s,int e)
{if(s.top - s.base == s.stacksize)return 0;*s.top = e;s.top++;return 1;
}
8.顺序栈的出栈
代码示例:
int pop(sqstack &s,int &e)
{if(s.top == s.base) return 0;s.top--;e = *s.top;return 1;
}
5.链栈的表示和实现
代码示例:
typedef struct stacknode
{int data;struct stacknode *next;
}stacknode,*linkstack;
linkstack s;
1.链栈的初始化
代码示例:
void initlinkstack(linkstack &s)
{s = NULL;
}
2.判断链栈是否为空
代码示例:
int stackempty(linkstack s)
{if(s == NULL) return false;else return true;
}
3.链栈的入栈
代码示例:
int push(linkstack &s,int e)
{stacknode *p;p = new stacknode;p -> data = e;p -> next = s;s = p;return 1;
}
4.链栈的出栈
代码示例:
int pop(linkstack &s,int &e)
{if(s == NULL) return 0;e = s -> data;stacknode *p;p = s;s = s -> next;delete p;return 1;
}
5.取栈顶元素
代码示例:
int gettop(stacknode *s)
{if(s != NULL) return s -> data;
}
6.栈与递归
1.递归问题的求法
2.递归的定义
3.递归的优缺点
7.队列的表示和操作
1.队列的抽象数据类型定义
2.队列的顺序表示和实现
代码示例:
#define maxqsize = 100
typedef struct
{int *base;int front;int rear;
}sqqueue;
3.解决假上溢的办法——循环队列
4.循环队列的类型定义
代码示例:
#define maxqsize = 100
typedef struct
{int *base;int front;int rear;
}sqqueue;
5.队列的初始化
代码示例:
int initqueue(sqqueue &q)
{q.base = new int[100];q.front = q.rear = 0;return 1;
}
6.求队列长度
代码示例:
int queuelength(sqqueue &q)
{return ((q.rear - q.front + maxqsize) % maxqsize);
}
7循环队列入队
代码示例:
int enqueue(sqqueue &q,int e)
{if((q.rear + 1) % maxqsize == q.front) return 0;q.base[q.rear] = e;q.rear = (q.rear + 1) % maxqsize;return 1;
}
8.循环队列出队
代码示例:
int dequeue(sqqueue &q,int &e)
{if(q.front == q.rear) return 0;e = q.base[q.front];q.front = (q.front + 1) % maxqsize;return 1;
}
9.取队头元素
代码示例:
int gethead(sqqueue &q)
{if(q.front != q.rear)return q.base[q.front];
}
8.链队列
1.链队的类型定义
代码示例:
typedef struct qnode
{int data;struct qnode *next;
}qnode,*queueptr;typedef struct
{queueptr front;queueptr rear;
}linkqueue;
2.链队初始化
代码示例:
int lnitqueue(linkqueue &q)
{q.front = q.rear = new qnode;q.front -> next = NULL;return 1;
}
3.销毁链队列
代码示例:
int destoryqueue(linkqueue &q)
{while(q.front){queueptr p;p = q.front -> next;delete q.front;q.front = p;}return 1;
}
4.将元素e入队
代码示例:
int enqueue(linkqueue &q,int e)
{queueptr p;p = new qnode;p -> data = e;p -> next = NULL;q.rear -> next = p;q.rear = p;return 1;
}
5.链队列出队
代码示例:
int dequeue(linkqueue &q,int &e)
{if(q.front == q.rear) return 0;queueptr p;p = q.front -> next;e = p -> data;q.front -> next = p -> next;if(q.rear == p) q.rear = q.front;delete p;return 1;
}
6.求链队列的队头元素
代码示例:
int gethead(linkqueue q,int &e)
{if(q.front == q.rear) return 0;e = q.front -> next -> data;return 1;
}
9.总的代码
#include<bits/stdc++.h>
using namespace std;#define maxsize 100;
typedef struct
{int *base;int *top;int stacksize;
}sqstack;int initstack(sqstack &s)
{s.base = new int[100];s.top = s.base;s.stacksize = 100;return 1;
}int stackempty(sqstack &s)
{if(s.top == s.base) return true;else return false;
}int stacklength(sqstack &s)
{return s.top - s.base;
}int clearstack(sqstack &s)
{if(s.base != NULL) s.top = s.base;return 1;
}int destorystack(sqstack &s)
{if(s.base != NULL){delete s.base;s.stacksize = 0;s.base = s.top = NULL;}return 1;
}int push(sqstack &s,int e)
{if(s.top - s.base == s.stacksize)return 0;*s.top = e;s.top++;return 1;
}int pop(sqstack &s,int &e)
{if(s.top == s.base) return 0;s.top--;e = *s.top;return 1;
}typedef struct stacknode
{int data;struct stacknode *next;
}stacknode,*linkstack;
linkstack s;void initlinkstack(linkstack &s)
{s = NULL;
}int stackempty(linkstack s)
{if(s == NULL) return false;else return true;
}int push(linkstack &s,int e)
{stacknode *p;p = new stacknode;p -> data = e;p -> next = s;s = p;return 1;
}int pop(linkstack &s,int &e)
{if(s == NULL) return 0;e = s -> data;stacknode *p;p = s;s = s -> next;delete p;return 1;
}int gettop(stacknode *s)
{if(s != NULL) return s -> data;
}#define maxqsize = 100
typedef struct
{int *base;int front;int rear;
}sqqueue;int initqueue(sqqueue &q)
{q.base = new int[100];q.front = q.rear = 0;return 1;
}int queuelength(sqqueue &q)
{return ((q.rear - q.front + maxqsize) % maxqsize);
}int enqueue(sqqueue &q,int e)
{if((q.rear + 1) % maxqsize == q.front) return 0;q.base[q.rear] = e;q.rear = (q.rear + 1) % maxqsize;return 1;
}int dequeue(sqqueue &q,int &e)
{if(q.front == q.rear) return 0;e = q.base[q.front];q.front = (q.front + 1) % maxqsize;return 1;
}int gethead(sqqueue &q)
{if(q.front != q.rear)return q.base[q.front];
}typedef struct qnode
{int data;struct qnode *next;
}qnode,*queueptr;typedef struct
{queueptr front;queueptr rear;
}linkqueue;int lnitqueue(linkqueue &q)
{q.front = q.rear = new qnode;q.front -> next = NULL;return 1;
}int destoryqueue(linkqueue &q)
{while(q.front){queueptr p;p = q.front -> next;delete q.front;q.front = p;}return 1;
}int enqueue(linkqueue &q,int e)
{queueptr p;p = new qnode;p -> data = e;p -> next = NULL;q.rear -> next = p;q.rear = p;return 1;
}int dequeue(linkqueue &q,int &e)
{if(q.front == q.rear) return 0;queueptr p;p = q.front -> next;e = p -> data;q.front -> next = p -> next;if(q.rear == p) q.rear = q.front;delete p;return 1;
}int gethead(linkqueue q,int &e)
{if(q.front == q.rear) return 0;e = q.front -> next -> data;return 1;
}int main(){return 0;
}