Chain Queue-C language version

Source: Internet
Author: User

Source file part: The pointer did not learn the classmate is very difficult to understand ^_^, a bit chaotic, hope to everyone a little help. #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 file section: typedef struct QUEUE{ELEMTYPE data;struct Queue *next;} Lqnode,*lqueue;typedef Struct{lqnode *front; Lqnode *rear;} Deque;void init_queue (Deque *head)//initialize + purge operation = = In fact, the emptying here refers to the node after the head node is discarded {Lqnode *p=null;p= (lqueue) malloc (sizeof (Lqnode )); head->front=p;head->rear=p;p->next=null;} int Empty_queue (Deque *head)//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)//queue operation {Lqueue p=null;p= (lqueue) malloc (sizeof (Lqnode)); if (!p) {printf ("no more CDs !\n "); return;}                 p->data=e;p->next=null;  When inserting, the team first pointer is not required to move the arrow->rear->next=p;arrow->rear=p; RetuRN;} void Dequeue (Deque *arrow,elemtype *e)//outbound operation {Lqnode *p=null;if (Empty_queue (arrow)) {printf ("The current chain queue is empty and the outbound operation cannot be completed!!! \ n "); return;} p=arrow->front->next; (*e) =p->data;arrow->front->next=p->next;printf ("element%d has exited queue!!!                       \ n ", *e); if (Lenght_queue (*arrow) ==0) return; When the last element is dequeue, Arrow->rear does not know where it points to free (p); return;} int Queue_top (Deque *arrow)//return team first element {if (Empty_queue (arrow)) {printf ("The current chain queue is empty, the first element of the team does not exist!!! \ n "); return 0;} printf ("Current team first element is:%d\n", arrow->front->next->data);} void Destroy_queue (Deque *arrow)//The destruction of the chain queue {Lqnode *p=null;if (Empty_queue (arrow)) {printf ("The current chain queue is empty, no destroy operation is required!!! \ 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 succeeded!\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,elemtypee)//modify function {int i=0; Lqnode *p=null;p=arrow->front->next;while (i<index-1) {p=p->next;} p->data=e;printf ("Modified operation completed!\n");} int Insearch_queue (Deque arrow,elemtype e)//Lookup function {lqnode *p=null;int i=1;if (Empty_queue (&arrow)) {printf ("current chain The queue is empty and there are no elements to find!!! \ 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 ("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");p rintf ("\t\t2, new queue element \ n");p rintf ("\ t \T3, return team first element \ n ");p rintf (" \t\t4, element out queue \ n ");p rintf (" \t\t5, find queue element \ n ");p rintf (" \t\t6, modify queue element \ n ");p rintf (" \t\t7, destroy queue \ n ") ;p rintf ("\t\t8, queue Length \ n");p rintf ("\t\t9, print queue element \ n");p rintf ("\t\t10, exit program \ n");p rintf ("Please enter the command you need to complete: \ n");d o{scanf ( "%d", &n); if (n<1| | N&GT;10) printf ("Sorry, the instruction number you entered is invalid, please re-enter!!! \ n ");} while (n<1| | N&GT;10), switch (n) {case 1:init_queue (&head);p rintf ("completed chain queue initialization, 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 Build Operation!\n"), Break;case 2:printf ("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), and if (index) printf (" The element you are looking for is located in the first%d position of the queue!!! \ n ", index); break;case 6:printf (" Enter the queue position of the element you changed: \ n ");d o{scanf ("%d ", &a); if (a<1| | A>lenght_queue (head) printf ("Sorry, the element you entered is not in the area, please re-enter!!! \ n ");} while (a<1| | A>lenght_queue (head));p rintf ("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);p rintf (" 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);}

Chain Queue-C language version

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.