Programming for queued/out-of-band operations

Source: Internet
Author: User

Idea: The queue is actually a linked list, but the queue also has two special nodes, one point to the team head, one point to the tail of the team. First design the data structure, as follows

struct student *struct linkqueue *struct  student{    int  data;     struct linkqueue{    Pnode first;    Pnode Rear;} queue;

1. The queue is actually a pointer to the tail of the team to move backwards, to determine whether the queues are empty or only one node situation

2. Out of the team is actually the pointer to the head of the team to move backwards

The overall code is as follows:

#include <stdio.h>#include<stdlib.h>typedefstructStudent *Pnode;typedefstructLinkqueue *Pqueue;typedefstructstudent{intdata; Pnode Next;} Node;typedefstructlinkqueue{Pnode First; Pnode Rear;} Queue Pqueue Insert (pqueue link,intnum)    {Pnode p; Pqueue Q=link; P= (Pnode)malloc(sizeof(Node)); P->data=num; if(link==NULL) {printf ("add a first node \ n"); Q= (Pqueue)malloc(sizeof(queue)); Q->first=p; Q->rear=p; Q->rear->next=NULL; returnQ; } q->rear->next=p; Q->rear=p; Q->rear->next=NULL; returnQ;} Pqueue del (pqueue queue) {if(queue==NULL) {printf ("queue is empty"); returnNULL; } pqueue Q=queue;    Pnode temp; Temp=q->First ; if(q->first->next!=NULL) Q->first=q->first->Next; Else{printf ("queue has only one node, delete complete \ n"); returnNULL; }     Free(temp); returnq;}voidprint (pqueue link) {pnode q=link->First ;  while(q!=NULL) {printf ("%d",q->data); Q=q->Next; } printf ("\ n");}intMainvoid) {Pqueue linkqueue=NULL; intflag=0, num;  while(1) {printf ("choose to queue up or out: 1 for the queue, 2 for the team, and 0 for the exit \ n"); scanf ("%d",&flag); if(flag==1) {printf ("Please select a value to queue: \ n"); scanf ("%d",&num); Linkqueue=Insert (Linkqueue,num); printf ("Print queued queue: \ n");        Print (Linkqueue); }        Else if(flag==2) {Linkqueue=del (linkqueue); printf ("queue after print out of team: \ n");        Print (Linkqueue); }        Else             Break; } printf ("Print the last queue: \ n");    Print (Linkqueue); return 0;}

Programming for queued/out-of-band operations

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.