Min data Structure C language Bank queueing queue discrete event simulation __ data structure

Source: Internet
Author: User
Tags rand

The following code originates from http://blog.csdn.net/nuaazdh/article/details/7059630 and is slightly modified with "g++-O Bank bank.c" compiled under CentOS.

Every time the system randomly generates 1, the current customer customer's counter is serviced by 2. The current customer and next customer arrival time interval, remember that these 2 points understand the entire code, the book is not very clear. The waiting time for each customer in the bank depends on the departure time of the previous node in the queue, not on its own arrival time + service time.

The code sets the EventList event list to keep the randomly generated customer arrival time and service time, simulating the queuing business.

However, in real life, the customer reaches the time can simulate, but by the service time generally bad simulation. Don't know what the actual scenario is useful for such an event-driven model?

Discrete event simulation, simulation of bank business queues//regardless of customer midway away, customer arrival event random, Business processing time//length of random, select the shortest team line, no longer change team//Author: NUAAZDH//Time: December 10, 2011 08:52:37 #i  Nclude <stdio.h> #include <time.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #define  
TRUE 1 #define FALSE 0 typedef int Status;  typedef struct event{//event type int occurtime;      Event occurs at the moment int ntype;  
The event type, 0 indicates the arrival event, and 1 to 4 indicates that the exit event of the four window struct events *next;  
  
}event,elemtype;    typedef struct{//One-way linked list structure elemtype *head;//head pointer elemtype *tail;//tail pointer int len;  
  
Length}linklist; typedef linklist EventList; Event List typedef struct qelemtype{//queue element int arrivetime;//arrival time int duration;//time required to transact business struct QELEMT  
Ype *next;  
  
}qelemtype;  
  
typedef struct{//Queue Structure Qelemtype *head;//head pointer qelemtype *tail;//tail pointer}linkqueue;  
    Event newevent (int occurt,int ntype);  
    Creates a new event Status Initlist (linklist *l) based on occurtime and ntype values; Initialize Event List Status OrderInsert (LiNklist *l,event e);  
    Inserts the event e into the ordered list L (linklist *l) in chronological order (listempty);  
    Returns true if the linked list L is null or NULL, or False Status Delfirst (linklist *l,elemtype *e);  
    The list L is not empty, deletes its first node, returns with E, returns OK, otherwise returns the error Status Listtraverse (linklist *l);  
    Traverse list Status initqueue (Linkqueue *q);  
    Initialize Queue Q Status emptyqueue (Linkqueue *q);  
    Returns true if the queue Q is empty, otherwise returns false Status Delqueue (linkqueue *q,qelemtype *e);  
    If the queue Q is not empty, the first node out of the team, with E return, and return OK, otherwise return error Status EnQueue (Linkqueue *q,qelemtype e);  
    Node e team Q int queuelength (Linkqueue q);  
    Returns the length of the queue Q, that is, the number of elements Status gethead (linkqueue *q,qelemtype *e);  
    If the queue Q is not empty, return the first node with E, and return OK, otherwise return the error Status Queuetraverse (Linkqueue *q);  
    Traversal queue Q//------------------//int Min (int a[],int n);  
    Returns the subscript of the first minimum value of an array of length n, starting at 1 int shortestqueue ();  
    Gets the number of the shortest queue void openforday ();  
    Initialize operation void customerarrived ();  
    Customer reaches event void Customerdepature ();  
    The customer leaves the event void bank_simulation ();  
Bank queuing simulationvoid Printeventlist ();  
    Output event queue void printqueue ();  
Print current queue//----global variable-----//EventList EV;  
Event en;  
Linkqueue Q[5];  
Qelemtype customer;  
int totaltime,customernum;   
    int closetime=200;//Closing time, that is, business hours//--------------main ()------------------//int main () {bank_simulation ();  
return 0;  
    //--------------Analog queued----------------//void Openforday () {//initialization operation int i;    totaltime=0;  
    customernum=0; Initlist (&ev);//Initialize event queue en.  
    occurtime=0; En.  
    ntype=0;  
    OrderInsert (&ev,en); for (i=1;i<=4;i++) Initqueue (&q[i])//Initialize four window queues}//openforday void customerarrived () {//customer reach  
    pieces int durtime,intertime,i,t;  
    Qelemtype e;  
    ++customernum; Intertime=rand ()%5+1;//time interval in 5 minutes durtime=rand ()%30+1;//business hours in 30 minutes t=en.  
    Occurtime+intertime; if (en. Occurtime<closetime) {//Bank not closed printf ("A New customer arrived At:%d,his durtime=%d,theNext intertime=%d|\n ", en.  
        Occurtime,durtime,intertime)//Next customer reaches time OrderInsert (&ev,newevent (t,0)); I=shortestqueue ();//Shortest queue e.arrivetime=en.  
        Occurtime;  
        E.duration=durtime;  
        EnQueue (&q[i],e); if (Queuelength (Q[i]) ==1) OrderInsert &ev,newevent (en.  
    Occurtime+durtime,i)); }else{printf ("Maxinum exceed!stop,en. Occurtime=%d,intertime=%d\n ", en.
    Occurtime,intertime); } void Customerdepature () {//Customer left event int i=en.  
    Ntype;  
    Delqueue (&q[i],&customer); printf ("A customer leaves at:%d\n", en. Occurtime)//output customer departure time totaltime+=en.  
    Occurtime-customer.arrivetime; if (!  
        Emptyqueue (&q[i])) {gethead (&q[i],&customer); OrderInsert (en. &ev,newevent Occurtime+customer.  
    Duration,i));  
    } void Bank_simulation () {//Bank queuing simulation openforday ();  
    Srand ((unsigned) time (NULL)); while (! Listempty (&ev)) {DElfirst (&ev,&en); 
        printf ("--------action--------------------------\ n"); if (en.  
        ntype==0) customerarrived ();  
        else Customerdepature ();  
        PrintQueue ();
    Printeventlist ();  
printf ("\ntotal time are:%d min,average time is:%g min.\n", TotalTime, (float) totaltime/customernum);  
    } void PrintQueue () {//print current queue int i;  
        for (i=1;i<=4;i++) {printf ("Queue%d have%d customer (s):", I,queuelength (Q[i)));  
    Queuetraverse (&q[i]);  
printf ("\ n");  
    } void Printeventlist () {//Output event queue printf ("Current EventList is:\n");  
Listtraverse (&ev);  
    int Min (int a[],int n) {//returns the subscript of the first minimum value of an array of length n, starting at 0 int i,tmp,ind=0;  
    TMP=A[0];  
            for (i=1;i<n;i++) {if (a[i]<tmp) {tmp=a[i];  
        Ind=i;  
} return IND; int Shortestqueue () {//Gets the number of the shortest queue int i,a[4];  
        for (i=1;i<=4;i++) {a[i-1]=queuelength (q[i]);  
    printf ("Team%d length is%d\n", I,queuelength (Q[i]));  
    Return Min (a,4) +1;//queue numbered from 1}//-----------team and linked list operations--------------//Event newevent (int occurt,int ntype) {  
    Creates a new event, based on Occurtime and ntype values;  
    E.occurtime=occurt;  
    E.ntype=ntype;  
return e;  
    Status initlist (linklist *l) {//Initialize Event list l->head=l->tail= (Elemtype *) malloc (sizeof (elemtype)); if (! L->head) {printf ("Apply for Memory error.")  
        Linklist initialize failed.\n ");  
    Exit (0);  
    } l->head->next=null;  
return OK;  
    Status OrderInsert (linklist *l,event e) {//Insert event E into ordered list L in chronological order, Elemtype *p,*q,*newptr;  
    Newptr= (Elemtype *) malloc (sizeof (elemtype));  
        if (!newptr) {printf ("Apply for memory error,new node can ' t insert intot the eventlist.\n");  
    Exit (0);  
    } *newptr=e; if (True==listempty (L)) {//linked list is empty  
       l->head->next=newptr;  
       l->tail=newptr;  
       l->tail->next=null;  
    return OK;  
    } q=l->head;  
    p=l->head->next;  
        while (p) {//Traverse entire list if (p->occurtime>=newptr->occurtime) break;  
        Q=p;  
    p=p->next;  
    } q->next=newptr;  
    newptr->next=p;  
    if (!p)//Insert position is the tail of the list l->tail=newptr;  
return OK; Status listempty (linklist *l) {//Determine if the list L is empty, returns true for NULL, otherwise returns FALSE if (L->head==l->tail) && (l-&  
    Gt;head!=null)) return TRUE;  
else return FALSE; Status Delfirst (linklist *l,elemtype *e) {//Chain table L is not NULL, delete its first node, return with E, return OK, otherwise return error elemtype *p=l->head-&  
    Gt;next;  
    if (!p) return ERROR;  
    l->head->next=p->next;  
    *e=*p;  
    Free (p);  
    if (l->head->next==null) l->tail=l->head;  
return OK; Status Listtraverse (LinkliSt *l) {//Traverse link list Event *p=l->head->next;  
        if (!p) {printf ("List is empty.\n");  
    return ERROR;  
        while (P!=null) {printf ("Occurtime:%d,event type:%d\n", P->occurtime,p->ntype);  
    p=p->next;  
    printf ("\ n");  
return OK;  
    Status initqueue (Linkqueue *q) {//initialization queue Q q->head=q->tail= (Qelemtype *) malloc (sizeof (Qelemtype)); if (! Q->head) {printf ("Apply for Memory error.")  
        Linkqueue initialize failed.\n ");  
    Exit (0);  
    } q->head->next=null;  
return OK; Status emptyqueue (Linkqueue *q) {//If queue Q is empty, returns True, otherwise returns false if (Q->head==q->tail&&q->hea  
    D!=null) return TRUE;  
else return FALSE; Status delqueue (linkqueue *q,qelemtype *e) {//If queue Q is not empty, first node out team, return with E and return OK; otherwise return error Qelemtype *p=q->hea  
    d->next;  
    if (!p) return ERROR;  
    *e=*p; Q->head->next=p->next;//Correction Team first pointer free (p); if (!  
    Q->head->next)/Team Empty q->tail=q->head;  
return OK;   
    Status EnQueue (Linkqueue *q,qelemtype e) {//node E team Q qelemtype *p= (Qelemtype *) malloc (sizeof (Qelemtype));  
        if (!p) {printf ("Apply for memory error,new element can ' t enqueue.\n");  
    Exit (0);  
    } *p=e;  
    p->next=null;  
q->tail->next=p;//Insert Team Tail q->tail=p;//Modify team tail pointer return OK;  
    The int queuelength (Linkqueue q) {//returns the length of the queue Q, that is, the number of elements int count=0;  
    Qelemtype *p=q.head->next;  
        while (p) {p=p->next;  
    count++;  
return count;  
        Status GetHead (linkqueue *q,qelemtype *e) {//If queue Q is not empty, return its first node with E and return OK, otherwise return error if (Emptyqueue (Q))  
    return ERROR;  
        *e=* (Q->head->next);  
return OK;  
    Status Queuetraverse (Linkqueue *q) {//Traversal queue Q Qelemtype *p=q->head->next;  
 if (!p) {       printf ("--is empty.\n");  
    return ERROR;  
        while (p) {printf ("(%d,%d)", p->arrivetime,p->duration);  
    p=p->next;  
    printf ("\ n");  
return OK;   }


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.