Linked list를 이용한
Queue 테스트 코드
/*************************************************************************/
/* */
/* Kernel Queue : Queue using Double linked list */
/* program by juseok.yun */
/* 2010.05.11 */
/*************************************************************************/
#include <stdio.h>
// Double queue structur
typedef struct _dnode {
char title[32];
char name[32];
struct _dnode *prev;
struct _dnode *next;
} DNODE;
// Make QUEUE Pointer
DNODE *queue_head=NULL, *queue_tail=NULL;
// initialize Queue
void init_queue(void)
{
queue_head = (DNODE*)malloc(sizeof(DNODE));
queue_tail = (DNODE*)malloc(sizeof(DNODE));
queue_head->prev = queue_head;
queue_head->next = queue_tail;
queue_tail->prev = queue_head;
queue_tail->next = queue_tail;
}
// Clear Queue
void clear_queue(void)
{
DNODE *t_list = NULL;
DNODE *s_list = NULL;
t_list = queue_head->next;
while (t_list != queue_tail)
{
s_list = t_list;
t_list = t_list->next;
free(s_list);
}
queue_head->next = queue_tail;
queue_tail->prev = queue_head;
}
// Insert Data
// DNODE* member input
void enqueue(DNODE* member)
{
DNODE* t_list = NULL;
// if ( queue_head == NULL )
// {
// init_queue();
// }
// else
{
t_list = (DNODE*)malloc(sizeof(DNODE));
memcpy(t_list->title,member->title,sizeof(member->title));
memcpy(t_list->name,member->name,sizeof(member->name));
queue_tail->prev->next = t_list;
t_list->prev = queue_tail->prev;
queue_tail->prev = t_list;
t_list->next = queue_tail;
}
}
// Get Data De-queue
// DNODE* member output
DNODE* dequeue()
{
DNODE* t_list = NULL;
if ( queue_tail == NULL )
{
printf("Queue underflow.\n");
return -1;
}
else
{
t_list = queue_head->next;
if (t_list == queue_tail)
{
printf("\n Queue underflow.");
return -1;
}
queue_head->next = t_list->next;
t_list->next->prev = queue_head;
return t_list;
}
}
// View data in queue
int view_queue()
{
int i=0;
DNODE* s_list = NULL;
if ( queue_head == NULL )
{
return -1;
}
s_list = queue_head->next;
while( s_list != queue_tail)
{
i++;
printf("[%3d] Name : %s\t\t Title : %s\n",i,s_list->name,s_list->title);
s_list = s_list->next;
}
// print the last
// don't need to display
// i++;
// printf("[%3d] Name : %s\t\t Title : %s\n",i,s_list->name,s_list->title);
return 1;
}
main()
{
DNODE* t_list;
char t_name[32]={0,};
char t_title[32]={0,};
int ans=0;
init_queue();
while(ans != 4)
{
ans=0;
printf("Queue Test Program\n");
printf(" 1. Enqueue\n");
printf(" 2. Dequeue\n");
printf(" 3. View Queue\n");
printf(" 4. Finish\n\n==> ");
scanf("%d",&ans);
switch(ans)
{
case 1:
printf("Input Name(32byte) : ");
scanf("%s",t_name);
printf("Input Title (32byte) : ");
scanf("%s",t_title);
t_list=(DNODE*)malloc(sizeof(DNODE));
memcpy(t_list->name,t_name,sizeof(t_name));
memcpy(t_list->title,t_title,sizeof(t_title));
enqueue(t_list);
break;
case 2:
t_list = dequeue();
if ( t_list != NULL )
{
printf("Name : %s\n",t_list->name);
printf("Title : %s\n",t_list->title);
free(t_list);
}
else
{
printf("There is no data\n");
}
break;
case 3:
if ( view_queue() == -1 )
{
printf("There is no data\n");
}
break;
case 4:
break;
default:
break;
}
}
clear_queue();
}
/* */
/* Kernel Queue : Queue using Double linked list */
/* program by juseok.yun */
/* 2010.05.11 */
/*************************************************************************/
#include <stdio.h>
// Double queue structur
typedef struct _dnode {
char title[32];
char name[32];
struct _dnode *prev;
struct _dnode *next;
} DNODE;
// Make QUEUE Pointer
DNODE *queue_head=NULL, *queue_tail=NULL;
// initialize Queue
void init_queue(void)
{
queue_head = (DNODE*)malloc(sizeof(DNODE));
queue_tail = (DNODE*)malloc(sizeof(DNODE));
queue_head->prev = queue_head;
queue_head->next = queue_tail;
queue_tail->prev = queue_head;
queue_tail->next = queue_tail;
}
// Clear Queue
void clear_queue(void)
{
DNODE *t_list = NULL;
DNODE *s_list = NULL;
t_list = queue_head->next;
while (t_list != queue_tail)
{
s_list = t_list;
t_list = t_list->next;
free(s_list);
}
queue_head->next = queue_tail;
queue_tail->prev = queue_head;
}
// Insert Data
// DNODE* member input
void enqueue(DNODE* member)
{
DNODE* t_list = NULL;
// if ( queue_head == NULL )
// {
// init_queue();
// }
// else
{
t_list = (DNODE*)malloc(sizeof(DNODE));
memcpy(t_list->title,member->title,sizeof(member->title));
memcpy(t_list->name,member->name,sizeof(member->name));
queue_tail->prev->next = t_list;
t_list->prev = queue_tail->prev;
queue_tail->prev = t_list;
t_list->next = queue_tail;
}
}
// Get Data De-queue
// DNODE* member output
DNODE* dequeue()
{
DNODE* t_list = NULL;
if ( queue_tail == NULL )
{
printf("Queue underflow.\n");
return -1;
}
else
{
t_list = queue_head->next;
if (t_list == queue_tail)
{
printf("\n Queue underflow.");
return -1;
}
queue_head->next = t_list->next;
t_list->next->prev = queue_head;
return t_list;
}
}
// View data in queue
int view_queue()
{
int i=0;
DNODE* s_list = NULL;
if ( queue_head == NULL )
{
return -1;
}
s_list = queue_head->next;
while( s_list != queue_tail)
{
i++;
printf("[%3d] Name : %s\t\t Title : %s\n",i,s_list->name,s_list->title);
s_list = s_list->next;
}
// print the last
// don't need to display
// i++;
// printf("[%3d] Name : %s\t\t Title : %s\n",i,s_list->name,s_list->title);
return 1;
}
main()
{
DNODE* t_list;
char t_name[32]={0,};
char t_title[32]={0,};
int ans=0;
init_queue();
while(ans != 4)
{
ans=0;
printf("Queue Test Program\n");
printf(" 1. Enqueue\n");
printf(" 2. Dequeue\n");
printf(" 3. View Queue\n");
printf(" 4. Finish\n\n==> ");
scanf("%d",&ans);
switch(ans)
{
case 1:
printf("Input Name(32byte) : ");
scanf("%s",t_name);
printf("Input Title (32byte) : ");
scanf("%s",t_title);
t_list=(DNODE*)malloc(sizeof(DNODE));
memcpy(t_list->name,t_name,sizeof(t_name));
memcpy(t_list->title,t_title,sizeof(t_title));
enqueue(t_list);
break;
case 2:
t_list = dequeue();
if ( t_list != NULL )
{
printf("Name : %s\n",t_list->name);
printf("Title : %s\n",t_list->title);
free(t_list);
}
else
{
printf("There is no data\n");
}
break;
case 3:
if ( view_queue() == -1 )
{
printf("There is no data\n");
}
break;
case 4:
break;
default:
break;
}
}
clear_queue();
}
gcc -o queue queue.c
$> ./queue
Queue Test Program
1. Enqueue
2. Dequeue
3. View Queue
4. Finish
이렇게 실행됨..