双链表队列和栈的实现与操作
来源:互联网 发布:sql数值 编辑:程序博客网 时间:2024/06/15 21:46
#include <iostream>
using namespace std;
// node definition
struct que_elem {
que_elem* prev_p;
que_elem* next_p;
int data;
que_elem* last_p;
int no_of_elem;
int que_len;
};
//Enque: enqueue a new element to queue tail
bool que_enq_el(que_struct *que_p,
que_elem *el_p)
{
if (que_p->first_p == NULL) // No elements in queue.
{
que_p->first_p = el_p;
que_p->first_p->next_p = NULL;
que_p->first_p->prev_p = NULL;
que_p->last_p = que_p->first_p;
que_p->no_of_elem = 1;
return(true);
}
else if (que_p->no_of_elem < que_p->que_len)
{
que_p->last_p->next_p = el_p;
el_p->prev_p = que_p->last_p;
que_p->last_p = el_p;
que_p->last_p->next_p = NULL;
que_p->no_of_elem++;
return(true);
}
return(false);
}
//Dequeue: dequeue element from queue head
bool que_deq_el(que_struct *que_p,
que_elem **el_p)
{
if (que_p->first_p != NULL) // There are elements in the queue!
{
*el_p = que_p->first_p;
if (que_p->first_p == que_p->last_p) // There is exactly one element in teh queue.
{
que_p->first_p = NULL;
que_p->last_p = NULL;
que_p->no_of_elem = 0;
}
else
{
que_p->first_p = que_p->first_p->next_p;
que_p->first_p->prev_p = NULL;
que_p->no_of_elem--;
}
return(true);
}
return(false);
} /* end que_deq_el */
// extract and cut the link with a specific element
void extract_el_from_queue(que_struct *que_p,
que_elem *el_p)
{
if (que_p->first_p == que_p->last_p) // One element in the queue.
{
que_p->first_p = NULL;
que_p->last_p = NULL;
que_p->no_of_elem = 0;
}
else if (el_p == que_p->first_p)
{
que_p->first_p = que_p->first_p->next_p;
que_p->first_p->prev_p = NULL;
que_p->no_of_elem--;
}
else if (el_p == que_p->last_p)
{
que_p->last_p = que_p->last_p->prev_p;
que_p->last_p->next_p = NULL;
que_p->no_of_elem--;
}
else
{
el_p->prev_p->next_p = el_p->next_p;
el_p->next_p->prev_p = el_p->prev_p;
que_p->no_of_elem--;
}
}
// initial the queue
void initial_queue(que_struct *que_p)
{
que_p->first_p = NULL;
que_p->last_p = NULL;
que_p->que_len = 10;
que_p->no_of_elem = 0;
}
// Clear the queue
void clear_queue(que_struct *que_p)
{
que_elem *tmp1_p, *tmp2_p;
tmp1_p = que_p->first_p;
while (tmp1_p != NULL)
{
tmp2_p = tmp1_p->next_p;
delete tmp1_p;
tmp1_p = tmp2_p;
}
que_p->first_p = NULL;
que_p->last_p = NULL;
que_p->no_of_elem = 0;
}
// Print the queue
void print_queue(que_struct *que_p)
{
que_elem *tmp1_p;
tmp1_p = que_p->first_p;
if (tmp1_p == NULL)
{
cout << "The queue is a empty" << endl;
return;
}
while (tmp1_p != NULL)
{
cout<<"The element's data is "<<tmp1_p->data<<endl;
tmp1_p = tmp1_p->next_p;
}
}
/***********************************************************************
********** it's stack turn*******************************************
************************************************************************/
struct stack_struct
{
que_elem *top_p;
que_elem *bot_p;
int no_of_elem;
int stack_len;
};
// push stack
bool stack_push_el(stack_struct *stack_p,
que_elem *el_p)
{
que_elem *tmp_p;
if (stack_p->top_p == NULL) // No elements in stack.
{
stack_p->top_p = el_p;
stack_p->top_p->next_p = NULL;
stack_p->top_p->prev_p = NULL;
stack_p->bot_p = stack_p->top_p;
stack_p->no_of_elem = 1;
return(true);
}
else if (stack_p->no_of_elem < stack_p->stack_len)
{
stack_p->top_p->prev_p = el_p;
el_p->next_p = stack_p->top_p;
stack_p->top_p = el_p;
stack_p->no_of_elem++;
return(true);
}
return(false);
}
// pop stack
bool stack_pop_el(stack_struct *stack_p,
que_elem **el_p)
{
if (stack_p->top_p != NULL) // There are elements in stack.
{
*el_p = stack_p->top_p;
if (stack_p->top_p == stack_p->bot_p) // One element in stack.
{
stack_p->top_p = NULL;
stack_p->bot_p = NULL;
stack_p->no_of_elem = 0;
}
else
{
stack_p->top_p = stack_p->top_p->next_p;
stack_p->top_p->prev_p = NULL;
stack_p->no_of_elem--;
}
return(true);
}
return(false);
}
{
stack_p->top_p = NULL;
stack_p->bot_p = NULL;
stack_p->no_of_elem--;
stack_p->stack_len = 10;
}
{
que_elem *tmp1_p, *tmp2_p;
tmp1_p = stack_p->top_p;
while (tmp1_p != NULL)
{
tmp2_p = tmp1_p->next_p;
delete tmp1_p;
tmp1_p = tmp2_p;
}
stack_p->top_p = NULL;
stack_p->no_of_elem = 0;
}
//print the stack
void print_stack(stack_struct *stack)
{
que_elem *tmp_p;
tmp_p = stack->top_p;
if (tmp_p == NULL)
{
cout << "The stack is empty" << endl;
}
while (tmp_p != NULL)
{
cout << "stack element data value: " << tmp_p->data<<endl;
tmp_p = tmp_p->next_p;
}
int main(void)
{
cout << "*************queue******************" << endl;
que_struct que;
initial_queue(&que);
print_queue(&que);
int ele_no = 2;
for (int i = 0; i < ele_no;i++)
{
que_elem *elem1_p = new que_elem();
elem1_p->data = i;
que_enq_el(&que,elem1_p);
}
print_queue(&que);
clear_queue(&que);
print_queue(&que);
cout << "************stack*******************" << endl;
stack_struct stack;
initial_stack(&stack);
print_stack(&stack);
que_elem *elem3_p = new que_elem();
elem3_p->data = 8;
stack_push_el(&stack, elem3_p);
print_stack(&stack);
clear_stack(&stack);
print_stack(&stack);
char ch;
cin>>ch;
return 0;
}
using namespace std;
// node definition
struct que_elem {
que_elem* prev_p;
que_elem* next_p;
int data;
};
// queue definition
struct que_struct {
que_elem* last_p;
int no_of_elem;
int que_len;
};
//Enque: enqueue a new element to queue tail
bool que_enq_el(que_struct *que_p,
que_elem *el_p)
{
if (que_p->first_p == NULL) // No elements in queue.
{
que_p->first_p = el_p;
que_p->first_p->next_p = NULL;
que_p->first_p->prev_p = NULL;
que_p->last_p = que_p->first_p;
que_p->no_of_elem = 1;
return(true);
}
else if (que_p->no_of_elem < que_p->que_len)
{
que_p->last_p->next_p = el_p;
el_p->prev_p = que_p->last_p;
que_p->last_p = el_p;
que_p->last_p->next_p = NULL;
que_p->no_of_elem++;
return(true);
}
return(false);
}
//Dequeue: dequeue element from queue head
bool que_deq_el(que_struct *que_p,
que_elem **el_p)
{
if (que_p->first_p != NULL) // There are elements in the queue!
{
*el_p = que_p->first_p;
if (que_p->first_p == que_p->last_p) // There is exactly one element in teh queue.
{
que_p->first_p = NULL;
que_p->last_p = NULL;
que_p->no_of_elem = 0;
}
else
{
que_p->first_p = que_p->first_p->next_p;
que_p->first_p->prev_p = NULL;
que_p->no_of_elem--;
}
return(true);
}
return(false);
} /* end que_deq_el */
// extract and cut the link with a specific element
void extract_el_from_queue(que_struct *que_p,
que_elem *el_p)
{
if (que_p->first_p == que_p->last_p) // One element in the queue.
{
que_p->first_p = NULL;
que_p->last_p = NULL;
que_p->no_of_elem = 0;
}
else if (el_p == que_p->first_p)
{
que_p->first_p = que_p->first_p->next_p;
que_p->first_p->prev_p = NULL;
que_p->no_of_elem--;
}
else if (el_p == que_p->last_p)
{
que_p->last_p = que_p->last_p->prev_p;
que_p->last_p->next_p = NULL;
que_p->no_of_elem--;
}
else
{
el_p->prev_p->next_p = el_p->next_p;
el_p->next_p->prev_p = el_p->prev_p;
que_p->no_of_elem--;
}
}
// initial the queue
void initial_queue(que_struct *que_p)
{
que_p->first_p = NULL;
que_p->last_p = NULL;
que_p->que_len = 10;
que_p->no_of_elem = 0;
}
// Clear the queue
void clear_queue(que_struct *que_p)
{
que_elem *tmp1_p, *tmp2_p;
tmp1_p = que_p->first_p;
while (tmp1_p != NULL)
{
tmp2_p = tmp1_p->next_p;
delete tmp1_p;
tmp1_p = tmp2_p;
}
que_p->first_p = NULL;
que_p->last_p = NULL;
que_p->no_of_elem = 0;
}
// Print the queue
void print_queue(que_struct *que_p)
{
que_elem *tmp1_p;
tmp1_p = que_p->first_p;
if (tmp1_p == NULL)
{
cout << "The queue is a empty" << endl;
return;
}
while (tmp1_p != NULL)
{
cout<<"The element's data is "<<tmp1_p->data<<endl;
tmp1_p = tmp1_p->next_p;
}
}
/***********************************************************************
********** it's stack turn*******************************************
************************************************************************/
struct stack_struct
{
que_elem *top_p;
que_elem *bot_p;
int no_of_elem;
int stack_len;
};
// push stack
bool stack_push_el(stack_struct *stack_p,
que_elem *el_p)
{
que_elem *tmp_p;
if (stack_p->top_p == NULL) // No elements in stack.
{
stack_p->top_p = el_p;
stack_p->top_p->next_p = NULL;
stack_p->top_p->prev_p = NULL;
stack_p->bot_p = stack_p->top_p;
stack_p->no_of_elem = 1;
return(true);
}
else if (stack_p->no_of_elem < stack_p->stack_len)
{
stack_p->top_p->prev_p = el_p;
el_p->next_p = stack_p->top_p;
stack_p->top_p = el_p;
stack_p->no_of_elem++;
return(true);
}
return(false);
}
// pop stack
bool stack_pop_el(stack_struct *stack_p,
que_elem **el_p)
{
if (stack_p->top_p != NULL) // There are elements in stack.
{
*el_p = stack_p->top_p;
if (stack_p->top_p == stack_p->bot_p) // One element in stack.
{
stack_p->top_p = NULL;
stack_p->bot_p = NULL;
stack_p->no_of_elem = 0;
}
else
{
stack_p->top_p = stack_p->top_p->next_p;
stack_p->top_p->prev_p = NULL;
stack_p->no_of_elem--;
}
return(true);
}
return(false);
}
// initial the stack
{
stack_p->top_p = NULL;
stack_p->bot_p = NULL;
stack_p->no_of_elem--;
stack_p->stack_len = 10;
}
//clear the stack
{
que_elem *tmp1_p, *tmp2_p;
tmp1_p = stack_p->top_p;
while (tmp1_p != NULL)
{
tmp2_p = tmp1_p->next_p;
delete tmp1_p;
tmp1_p = tmp2_p;
}
stack_p->top_p = NULL;
stack_p->no_of_elem = 0;
}
//print the stack
void print_stack(stack_struct *stack)
{
que_elem *tmp_p;
tmp_p = stack->top_p;
if (tmp_p == NULL)
{
cout << "The stack is empty" << endl;
}
while (tmp_p != NULL)
{
cout << "stack element data value: " << tmp_p->data<<endl;
tmp_p = tmp_p->next_p;
}
}
int main(void)
{
cout << "*************queue******************" << endl;
que_struct que;
initial_queue(&que);
print_queue(&que);
int ele_no = 2;
for (int i = 0; i < ele_no;i++)
{
que_elem *elem1_p = new que_elem();
elem1_p->data = i;
que_enq_el(&que,elem1_p);
}
print_queue(&que);
clear_queue(&que);
print_queue(&que);
cout << "************stack*******************" << endl;
stack_struct stack;
initial_stack(&stack);
print_stack(&stack);
que_elem *elem3_p = new que_elem();
elem3_p->data = 8;
stack_push_el(&stack, elem3_p);
print_stack(&stack);
clear_stack(&stack);
print_stack(&stack);
char ch;
cin>>ch;
return 0;
}
1 0
- 双链表队列和栈的实现与操作
- 栈和队列的基本操作实现
- 两个栈实现队列的pop与push操作
- 数据结构之 队列的操作与实现
- 链式队列的基本操作与实现
- 队列的实现与基本操作
- 队列的实现与基本操作
- 【算法设计-优先队列】优先队列的实现与操作
- 循环队列的实现和操作
- 7.6 栈与队列-栈的基本操作和应用
- 用栈操作实现队列的操作
- 栈和队列的操作
- 栈和队列的操作
- 实验3:栈和队列的基本操作实现及其应用——顺序队列和链队列
- 用两个栈来实现一个队列,完成队列的add和poll、peek操作。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作
- 用两个栈来实现一个队列,完成队列的Push和Pop操作
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。
- 微信公众号开发流程
- C#Winform利用DataGridView的特性实现字段排序
- JDBC连接sql server数据库的详细步骤和代码
- android studio去掉标题栏
- [Leetcode]21. Merge Two Sorted Lists
- 双链表队列和栈的实现与操作
- 空指针
- 用Laravel创建项目
- android canvas使用line画半园
- 数组指针与指针数组
- CTF比赛中必备的瑞士军刀ctf-tools
- 个人独立游戏总结
- adpcm的压缩和解压缩实现
- 【疑难杂症】-CentOS-yum错误: Cannot retrieve repository metadata (repomd.xml) for repository: