#include <stdio.h> // writter 5396952 이현민
#include <stdlib.h>
#include <malloc.h>
typedef int element;
typedef struct QueueNode{
element item;
struct QueueNode *link;
} QueueNode;
//queue ADT realization
typedef struct {
QueueNode *front;
QueueNode *rear;
//pointer valuable which points each queueNode
}QueueType;
void error(char *message)
{
fprintf(stderr ,"%s\n", message);
exit(1);
}
void init(QueueType *q)
{
q->front = q->rear = NULL;
}
int isEmpty(QueueType *q)//isEmpty
{
return (q->front == NULL);
//if the front is pointing NULL, the queue is empty
//at the same time, it returns current front index
}
void enqueue(QueueType *q, element item)
{
//memory allocation to new node
QueueNode *temp = (QueueNode *)malloc(sizeof(QueueNode));
if(temp == NULL)
{
error("Memory allocation error\n");
exit(1);
}
temp->item = item;
if(isEmpty(q))//if the queue is empty
{
q->front = temp;
q->rear = temp;
}
else
{//the last node in the queue links new node
q->rear->link = temp;
//make the new node to be the last node in the queue
q->rear= temp;
}
}
element dequeue(QueueType *q)
{
QueueNode *temp = (QueueNode *)malloc(sizeof(QueueNode));
if(temp == NULL)
{
error("Memory allocation error\n");
exit(1);
}
//when the queue is already empty
if(isEmpty(q))
{
//if isEmpty function returns true == the queue is empty
error("The queue is empty\n");
exit(1);
}
temp->item = q->front->item;
if(q->front == q->rear)//if the target node is the only node in the queue
{
q->front = q->rear = NULL;
}
else{
q->front = q->front->link;
}
return temp->item;
}
element peek(QueueType *q)
{
if(isEmpty(q))
{
//if isEmpty function returns true == the queue is empty
error("The queue is empty\n");
}
else {
return q->front->item;
}
}
void main()
{
QueueType q;
init(&q);
enqueue(&q, 0);
enqueue(&q, q.front->item + 1);
for(int i = 0; i < 10; i++)
{
enqueue(&q, (q.front->item + q.rear->item) );
dequeue(&q);
printf("Fib n%d : %d\n",i, q.rear->item);
}
//printf("dequeue () = %d \n", dequeue(&q));
//printf("dequeue () = %d \n", dequeue(&q));
}
#include <stdlib.h>
#include <malloc.h>
typedef int element;
typedef struct QueueNode{
element item;
struct QueueNode *link;
} QueueNode;
//queue ADT realization
typedef struct {
QueueNode *front;
QueueNode *rear;
//pointer valuable which points each queueNode
}QueueType;
void error(char *message)
{
fprintf(stderr ,"%s\n", message);
exit(1);
}
void init(QueueType *q)
{
q->front = q->rear = NULL;
}
int isEmpty(QueueType *q)//isEmpty
{
return (q->front == NULL);
//if the front is pointing NULL, the queue is empty
//at the same time, it returns current front index
}
void enqueue(QueueType *q, element item)
{
//memory allocation to new node
QueueNode *temp = (QueueNode *)malloc(sizeof(QueueNode));
if(temp == NULL)
{
error("Memory allocation error\n");
exit(1);
}
temp->item = item;
if(isEmpty(q))//if the queue is empty
{
q->front = temp;
q->rear = temp;
}
else
{//the last node in the queue links new node
q->rear->link = temp;
//make the new node to be the last node in the queue
q->rear= temp;
}
}
element dequeue(QueueType *q)
{
QueueNode *temp = (QueueNode *)malloc(sizeof(QueueNode));
if(temp == NULL)
{
error("Memory allocation error\n");
exit(1);
}
//when the queue is already empty
if(isEmpty(q))
{
//if isEmpty function returns true == the queue is empty
error("The queue is empty\n");
exit(1);
}
temp->item = q->front->item;
if(q->front == q->rear)//if the target node is the only node in the queue
{
q->front = q->rear = NULL;
}
else{
q->front = q->front->link;
}
return temp->item;
}
element peek(QueueType *q)
{
if(isEmpty(q))
{
//if isEmpty function returns true == the queue is empty
error("The queue is empty\n");
}
else {
return q->front->item;
}
}
void main()
{
QueueType q;
init(&q);
enqueue(&q, 0);
enqueue(&q, q.front->item + 1);
for(int i = 0; i < 10; i++)
{
enqueue(&q, (q.front->item + q.rear->item) );
dequeue(&q);
printf("Fib n%d : %d\n",i, q.rear->item);
}
//printf("dequeue () = %d \n", dequeue(&q));
//printf("dequeue () = %d \n", dequeue(&q));
}
댓글
댓글 쓰기