#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;
}