_datastructure_c_impl: Chain-loop queue with only the tail pointer

Source: Internet
Author: User

_datastructure_c_impl: #include <stdio.h> #include <stdlib.h> #include <string.h>typedef Char datatype;typedef struct snode{//chained stack node type definition DataType data;struct snode *next;} lsnode;typedef struct qnode{//only the chain loop queue type definition for the tail pointer datatype data;struct qnode *next;} lqnode,*linkqueue;//the chain stack of lead nodes initialize void Initstack (Lsnode **head) {if (*head= (lsnode) malloc (sizeof (Lsnode))) ==null) { printf ("Allocation node unsuccessful"); exit (-1);} else{(*head)->next=null;}} Determines whether the leading node-chaining stack is empty. Returns 1 if the stack is empty, otherwise returns 0int Stackempty (Lsnode *head) {if (head->next==null) return 1;else return 0;} Chained stacks into the stack. The stack successfully returns 1, otherwise exits int pushstack (Lsnode *head,datatype e) {Lsnode *s;if ((s= (Lsnode) malloc (sizeof (Lsnode))) ==null)// Allocates space for the node, fails to exit the program and returns -1exit ( -1); else{s->data=e;//assigns the element value to the node's data field s->next=head->next;//inserts the node into the top of the stack head->next= S;return 1;}} Chain stack out stack, you need to determine whether the stack is empty. The stack successfully returns 1, otherwise returns 0int Popstack (Lsnode *head,datatype *e) {lsnode *s= (Lsnode *) malloc (sizeof (Lsnode)), if (!s) exit ( -1); s= head->next;//Pointer s points to the top node of the stack if (Stackempty (head))//Determine if the stack is empty return 0;else{head->nextThe pointer to the =s->next;//head node points to the second node position *e=s->data;//the node element to the stack is assigned to Efree (s);//release the node space to be out of the stack return 1;}} To initialize the chain loop queue of the lead node to an empty queue, the pointer to the head node is pointed to the header node void Initqueue (Linkqueue *rear) {if (*rear= (lqnode*) malloc (sizeof (lqnode)) = = NULL) exit (-1);//If the application node space fails to exit else (*rear)->next=*rear;//the tail pointer points to the head node}//determine if the chain queue is empty, the queue is empty returns 1, otherwise returns 0int Queueempty ( Linkqueue rear) {if (rear->next==rear) return 1;elsereturn 0;} Insert element e into the chain queue, insert successfully returns 1int Enterqueue (Linkqueue *rear,datatype e) {lqnode *s;s= (Lqnode *) malloc (sizeof (Lqnode));// Request a node for the element that will be queued if (!s) exit ( -1); s->data=e;//assigns the element value to the node's data field s->next= (*rear)->next;//inserts the new node into the chain queue (*rear)- >next=s;*rear=s;//change the tail pointer return 1;} Delete the team header element in the chained queue and assign the element to E, delete successfully returns 1, otherwise return 0int deletequeue (linkqueue *rear,datatype *e) {Lqnode *f,*p;if (*rear== (*rear)- >next)//To determine if the chain queue is empty return 0;else{f= (*rear)->next;//the pointer F points to the head node p=f->next;//so that the pointer p points to the node to be deleted before the queue is removed (p= =*rear) {//Handle only one node in the queue *rear= (*rear)->next;//the pointer rear point to the head node (*rear)->next=*rear;} else{f->next=p->next;//the head node points to the next node to be out of the queue}*e=p->data;//The team head element value is assigned to Efree (p);//release pointer p points to the node return 1;}} void Main () {Linkqueue lqueue1,lqueue2;/* defines the chain loop queue */lsnode *lstack1,*lstack2;/* defines the chain stack */char str1[]= "XYZAZYX";/* The return character sequence 1*/char str2[]= "Xyzbzyx";/* back character sequence 2*/char q1,s1,q2,s2;int i;initqueue (&lqueue1);/* Initialize the chain loop queue 1*/initqueue ( &AMP;LQUEUE2);/* Initializes the chain Loop queue 2*/initstack (&AMP;LSTACK1);/* Initializes the chained stack 1*/initstack (&AMP;LSTACK2);/* Initializes the chained stack 2*/for (i=0;i <strlen (STR1); i++) {enterqueue (&lqueue1,str1[i]);/* Queue character sequence 1 in turn */enterqueue (&lqueue2,str2[i]);/* The character sequence 2 is queued */pushstack (Lstack1,str1[i]),/*////* sequence 1 into the stack */pushstack (Lstack2,str2[i]),/* The character sequence 2 into the stack */}printf ("character sequence 1:\ n ");p rintf (" queue sequence out of stack sequence \ n "); Stackempty (LSTACK1)//* Determine if stack 1 is an empty */{deletequeue (&AMP;LQUEUE1,&AMP;Q1);/* character sequence out of the queue and assign the outbound element to Q*/popstack (LStack1, &AMP;S1);/* character sequence out of the stack and assign the stack element to s*/printf ("%5c", Q1);/* Output character sequence 1*/printf ("%10c\n", S1); if (Q1!=S1)/* Determine if character sequence 1 is a palindrome */{ printf ("character sequence 1 is not a palindrome!") "); return;}} printf ("Character sequence 1 is a palindrome!") \ n ");p rintf (" character sequence 2:\n ");p rintf (" queue out sequence out of stack sequence \ n "); Stackempty (LSTACK2)//* Determine if stack 2 is empty */{deletequeue (&AMP;LQUEUE2,&AMP;Q2)The/* character sequence is sequentially out of the queue and assigns the team element to Q*/popstack (LSTACK2,&AMP;S2), the/* character sequence out of the stack, and assigns the stack element to s*/printf ("%5c", Q2);/* Output character sequence 2*/printf ("% 10c\n ", S2), if (Q2!=S2)/* Determines whether the character sequence 2 is a palindrome */{printf (" character sequence 2 is not a palindrome!) \ n "); return;}} printf ("Character sequence 2 is a palindrome!") \ n "); system (" Pause ");}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

_datastructure_c_impl: Chain-loop queue with only the tail pointer

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: info-contact@alibabacloud.com 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.