链表和队列
2016-05-26 13:38
218 查看
单链表
(1) 带头结点:head始终不等于NULL, head->next等于NULL的时候链表为空;
(2) 不带头结点:head等于NULL时,链表为空;
双链表
(1) 单/双链表为空时,head->next==NULL;
循环单链表
(1) 带头结点:为空:head==head->next;
(2) 不带头结点:为空:head==NULL;
循环双链表
(1) 带头结点:为空:head->next==NULL&&head->prior==NULL:
(2) 不带头结点:为空:head==NULL;
静态链表:使用数组来实现;
顺序栈
(1) 定义
(2) 栈空判断
(3) 进栈
(4) 出栈
链栈
(1) 定义
(2) 栈空
(3) 进栈
(4) 出栈
顺序队
(1) 定义
(2) 循环队列
队空:queue.rear == queue.front
队满:(queue.rear+1)%max_size == queue.front
(3) 队空
(4) 入队
(5) 出队
链队
(1) 定义
(2)队空
(3) 入队
(4) 出队
(1) 带头结点:head始终不等于NULL, head->next等于NULL的时候链表为空;
(2) 不带头结点:head等于NULL时,链表为空;
typedef struct LinkNode { int data; struct LinkNode *next; };
双链表
(1) 单/双链表为空时,head->next==NULL;
typedef struct DoubleLinkNode { int data; struct DoubleLinkNode *prior; struct DoubleLinkNode *next; };
循环单链表
(1) 带头结点:为空:head==head->next;
(2) 不带头结点:为空:head==NULL;
循环双链表
(1) 带头结点:为空:head->next==NULL&&head->prior==NULL:
(2) 不带头结点:为空:head==NULL;
静态链表:使用数组来实现;
顺序栈
(1) 定义
typedef struct SequenceStack { int data[SIZE_MAX]; int top; };
(2) 栈空判断
int isEmpty(SequenceStack st) { if (st.top == -1) { return 1; } return 0; }
(3) 进栈
int push(SequenceStack &st, int x) { if (st.top == SIZE_MAX-1) { return 0; } ++(st.top); st.data[st.top] = x; return 1; }
(4) 出栈
int pop(SequenceStack &st, int &x) { if (st.top == -1) { return 0; } x = st.data[st.top]; --(st.top); return 1; { } }
链栈
(1) 定义
typedef struct LinkStack { int data; struct LinkStack *next; };
(2) 栈空
int isEmpty(LinkStack *lst) { if (lst->next == null) { return 1; } return 0; }
(3) 进栈
void push(LinkStack *lst, int x) { LinkStack *p; p = (LinkStack*)malloc(sizeof(LinkStack)); p->next = NULL; //头插法 p->data = x; p->next = lst->next; lst->next = p; }
(4) 出栈
int pop(LinkStack *lst, int &x) { LinkStack *p; if (lst->next == NULL) { return 0; } p = lst->next; x = p->data; lst->next = p->next; free(p); return 1; }
顺序队
(1) 定义
typedef struct SequenceQueue { int data[SIZE_MAX]; int front; int rear; };
(2) 循环队列
队空:queue.rear == queue.front
队满:(queue.rear+1)%max_size == queue.front
(3) 队空
int isEmpty(SequeenceQueue &queue) { if (queue.front == queue.rear) { return 1; } return 0; }
(4) 入队
int enQueue(SequenceQueue &queue, int x) { if ((queue+1)%max_size==queue.front) { return 0; } queue.rear = (queue.rear + 1) % max_size; queue.data[queue.rear] = x; return 1; }
(5) 出队
int deQueue(SequenceQueue &queue, int &x) { if (queue.front == queue.rear) { return 0; } queue.front = (queue.front + 1) % max_size; x = queue.data[queue.front]; return 1; { } }
链队
(1) 定义
typedef struct QueueNode { int data; struct QueueNode *next; }; typedef struct LinkQueue { struct LinkQueue *front; struct LinkQueue *next; };
(2)队空
int isEmpty(LinkQueue *lqueue) { if (lqueue.rear == NULL || lqueue.front == NULL) { return 1; } return 0; }
(3) 入队
void enQueue(LinkQueue *lqueue, int x) { QueueNode *p; p = (QueueNode*)malloc(sizeof(QueueNode)); p->data = x; p->next = NULL; //队列为空,新结点为队首结点,也是队尾结点 if (lqueue->rear == NULL) { lqueue->front = lqueue->rear = p; } else { lqueue->rear->next = p; lqueue->rear = p; } }
(4) 出队
int deQueue(LinkQueue *lqueue, int &x) { QueueNode *p; if (lqueue->rear==NULL) { return 0; } else { p = lqueue->front; } if (lqueue->front == lqueue->rear) { lqueue->front = lqueue->rear = NULL; } else { lqueue->front = lqueue->front->next; } x = p->data; free(p); return 1; { } }
相关文章推荐
- [C/C++]反转链表
- C#实现基于链表的内存记事本实例
- C#模拟链表数据结构的实例解析
- C#线程队列用法实例分析
- 算法系列15天速成 第九天 队列
- C语言单链队列的表示与实现实例详解
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C++利用静态成员或类模板构建链表的方法讲解
- C++实现简单的学生管理系统
- mysql 队列 实现并发读
- C#队列Queue用法实例分析
- C#多线程处理多个队列数据的方法
- C语言循环队列的表示与实现实例详解
- C++循环队列实现模型
- C#内置队列类Queue用法实例
- Array栈方法和队列方法的特点说明
- Linux内核链表实现过程
- C++链表倒序实现方法
- C#通过链表实现队列的方法
- 链表集合