Stack exercises in data structure (c)
# Include <stdlib. h>
# Include <stdio. h>
# Include <malloc. h>
# Include <conio. h>
# Define ERROR 0
# Define OK 1
Typedef struct {
Int OccurTime;
Int NType;
} Event;
Typedef struct {
Int ArrivalTime;
Int Duration;
} QElemType;
Struct LNODE
{
Event data;
Struct LNODE * next;
};
Typedef struct LNODE LNode;
Typedef struct LNODE * LinkList;
Typedef struct LNODE * EvenList;
Typedef struct QNode {
QElemType elem;
Struct QNode * next;
} QNode, * QueuePtr;
Typedef struct
{QueuePtr front;
QueuePtr rear;
} LinkQueue;
EvenList ev;
Event en;
LinkQueue q [5];
QElemType customer;
Int TotalTime, CustomerNum, CloseTime;
Int InitList (EvenList * L)
{
* L = (LNode *) malloc (sizeof (LNode ));
If (! (* L) exit (ERROR );
(* L)-> next = NULL;
Return OK;
}
Int DelFirst (EvenList * L, Event * e)
{LNode * pc, * q;
Pc = * L; q = pc-> next;
Pc-> next = q-> next; * e = q-> data; return OK ;}
Int ListEmpty (LNode L)
{LNode * p;
Int j = 0;
P = L. next;
While (p)
{J ++; break ;}
If (j = 0) return 1;
Else return 0;
}
Int compare (Event a, Event B)
{If (a. OccurTime> B. OccurTime) return 1;
Else if (a. OccurTime = B. OccurTime) return 0;
Else return-1;
}
Int OrderInsert (EvenList * L, Event e, int (* cmp) (Event, Event ))
{LNode * p, * pc;/* Insert the event to the linked list */
P = (LNode *) malloc (sizeof (LNode);/* allocate space */
If (! P) {printf ("not"); return (0 );}
If (ListEmpty (** L ))
{P-> data = e; p-> next = (* L)-> next; (* L)-> next = p ;}
Else {
Switch (cmp (* L)-> next)-> data, e ))
{Case-1: pc = (* L)-> next;
While (pc-> next! = NULL)
{
If (pc-> next)-> data. OccurTime <= e. OccurTime)
Pc = pc-> next;
Else break;
}
P-> data = e; p-> next = pc-> next;/* connect it before it is bigger */
Pc-> next = p; break;
& Nbs
<