C implements chain queues
- 2020-06-07 04:56:16
- OfStack
This paper shares the specific code of C language chain queue for your reference, the specific content is as follows
Source file: if you haven't learned the pointer well, it's hard to understand ^_^. It's a bit messy. I hope it will be helpful to you.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef int Elemtype;
#include"LQueue.h"
int main()
{
Deque head;
instruction(head);
return 0;
}
Header section:
typedef struct Queue
{
Elemtype data;
struct Queue *next;
}LQnode,*LQueue;
typedef struct
{
LQnode *front;
LQnode *rear;
}Deque;
void Init_queue(Deque *head) // Initialize the + Empty the operation == In this case, to clear means to discard the node after the head node
{
LQnode *p=NULL;
p=(LQueue)malloc(sizeof(LQnode));
head->front=p;
head->rear=p;
p->next=NULL;
}
int Empty_queue(Deque *head) // Sentenced to empty
{
if(head->front->next==head->rear->next)
return 1;
return 0;
}
int Lenght_queue(Deque arrow)
{
LQnode *p=NULL;
int len=0;
p=arrow.front->next;
while(p)
{
len++;
p=p->next;
}
return len;
}
void Enqueue(Deque *arrow,Elemtype e) // The operation of entrance
{
LQueue p=NULL;
p=(LQueue)malloc(sizeof(LQnode));
if(!p)
{
printf(" No more memory units have been allocated !\n");
return ;
}
p->data=e;
p->next=NULL; // When inserting, the first pointer does not need to be moved
arrow->rear->next=p;
arrow->rear=p;
return ;
}
void Dequeue(Deque *arrow,Elemtype *e) // The team operation
{
LQnode *p=NULL;
if(Empty_queue(arrow))
{
printf(" The current chain queue is empty and cannot be queued !!!\n");
return ;
}
p=arrow->front->next;
(*e)=p->data;
arrow->front->next=p->next;
printf(" The element %d Dequeued !!!\n",*e);
if(Lenght_queue(*arrow)==0)
return ; // When the last 1 So once I get these guys out, arrow->rear I don't know where it's pointing
free(p);
return ;
}
int Queue_top(Deque *arrow) // Returns the header element
{
if(Empty_queue(arrow))
{
printf(" The current chain queue is empty. The queue header element does not exist !!!\n");
return 0;
}
printf(" The current team header element is: %d\n",arrow->front->next->data);
}
void Destroy_queue(Deque *arrow) // Destruction of chain queues
{
LQnode *p=NULL;
if(Empty_queue(arrow))
{
printf(" The current chain queue is empty, no need to complete the destroy operation !!!\n");
return ;
}
while(arrow->front->next)
{
p=arrow->front->next;
arrow->front->next=p->next;
if(Lenght_queue(*arrow)==0)
break;
free(p);
}
printf(" Destruction of success !\n");
return ;
}
void Print_queue(Deque arrow)
{
LQnode *p=NULL;
p=arrow.front->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void Modify_queue(Deque *arrow,Elemtype index,Elemtype e) // Modify the function
{
int i=0;
LQnode *p=NULL;
p=arrow->front->next;
while(i<index-1)
{
p=p->next;
}
p->data=e;
printf(" The modification action has been completed !\n");
}
int Insearch_queue(Deque arrow,Elemtype e) // Lookup function
{
LQnode *p=NULL;
int i=1;
if(Empty_queue(&arrow))
{
printf(" The current chain queue is empty and there are no elements to look up !!!\n");
return 0;
}
p=arrow.front->next;
while(p!=NULL)
{
if(e==p->data)
{
return i;
break;
}
i++;
p=p->next;
}
if(p==NULL)
printf(" The lookup failed. No element exists in the queue !\n");
return 0;
}
void instruction(Deque head)
{
int n,m,t,a,b,len1,index;
printf("\t\t1 , queue initialization \n");
printf("\t\t2 , new queue elements \n");
printf("\t\t3 , returns the header element \n");
printf("\t\t4 Element out of queue \n");
printf("\t\t5 , looking for queue elements \n");
printf("\t\t6 , modify the queue elements \n");
printf("\t\t7 , destroy the queue \n");
printf("\t\t8 , the length of the queue \n");
printf("\t\t9 , print queue elements \n");
printf("\t\t10 , exit the program \n");
printf(" Please enter the instructions you need to complete :\n");
do{
scanf("%d",&n);
if(n<1||n>10)
printf(" Sorry, the instruction number you entered is invalid, please re-enter it !!!\n");
}while(n<1||n>10);
switch(n)
{
case 1:
Init_queue(&head);
printf(" Chain queue initialization has been completed , Please enter the number of elements you want to add !\n");
scanf("%d",&n);
while(n--)
{
int x;
scanf("%d",&x);
Enqueue(&head,x);
}
printf(" Complete the team building operation !\n");
break;
case 2:
printf(" Please enter the number of elements you want to add !\n");
scanf("%d",&n);
while(n--)
{
int x;
scanf("%d",&x);
Enqueue(&head,x);
}
printf(" Add success !\n");
break;
case 3:
Queue_top(&head);
break;
case 4:
Dequeue(&head,&t);
break;
case 5:
printf(" Please enter the element you are looking for :\n");
scanf("%d",&m);
index=Insearch_queue(head,m);
if(index)
printf(" The element you are looking for is at the beginning of the queue %d A position !!!\n",index);
break;
case 6:
printf(" Please enter the element queue location that you changed :\n");
do{
scanf("%d",&a);
if(a<1||a>Lenght_queue(head))
printf(" Sorry, the location of the element you entered is not in the region, please re-enter !!!\n");
}while(a<1||a>Lenght_queue(head));
printf(" Please enter the modified value :\n");
scanf("%d",&b);
Modify_queue(&head,a,b);
break;
case 7:
Destroy_queue(&head);
break;
case 8:
len1=Lenght_queue(head);
printf(" The current chain queue length is :%d\n",len1);
break;
case 9:
Print_queue(head);
break;
case 10:
return;
default:
instruction(head);
break;
}
instruction(head);
}