#include <stdio.h>
#define SIZE sizeof(LINKQUEUE)
#define LISTSIZE sizeof(LINKQUEUELIST)
#define PRINT_ITEM "%d "
#define SCANF_ITEM "%d"
typedef int ELEMTYPE;
const ELEMTYPE QUEUE_NULL = -9999;//定义当队列为NULL时,返回的数据元素的值
typedef struct qnode //定义链队列结构
{
ELEMTYPE data;
struct qnode * next;
}LINKQUEUE;
typedef struct
{
LINKQUEUE * front,*rear;
int length;
}LINKQUEUELIST;
typedef enum
{
true = 1,
false = 0
} bool;
LINKQUEUE * INITQUEUE(); //初始化链队列中的一个数据项
LINKQUEUE * CREATE(ELEMTYPE);//创建一个节点
LINKQUEUELIST * INITQUEUELIST();//初始化链队列
void ADDQUEUE(LINKQUEUELIST *,ELEMTYPE);//将元素插入到链队列中
ELEMTYPE GET_FRONT(LINKQUEUELIST *);//获取队头元素值
bool EMPTY(LINKQUEUELIST *);//判断链队列是否为空
int CURRENT_SIZE(LINKQUEUELIST *);//当前链队列的元素个数
bool CLEAR(LINKQUEUELIST *); //清除队列
ELEMTYPE DELQUEUE(LINKQUEUELIST *); //删除队列头的元素,返回删除元素的值
void menu();//菜单
void print_enter(); //打印回车
void print_tab(); //打印tab
void print_str(char *); //打印字符串
void print_info(char *); //打印信息
void print_menu_item(char *,char *); //打印菜单项
int select_menu_item();
void queue_add(LINKQUEUELIST *);
void queue_del(LINKQUEUELIST *);
void queue_clear(LINKQUEUELIST *);
void queue_empty(LINKQUEUELIST *);
void queue_get_front(LINKQUEUELIST *);
void queue_count(LINKQUEUELIST *);
ELEMTYPE add_input();
void print_queue_null();
void menu_method(LINKQUEUELIST *,void (* fun)(LINKQUEUELIST *));
int main(int argc, char *argv[])
{
int sel_item;
LINKQUEUELIST * queue = INITQUEUELIST(); //初始化链队列
menu();
do
{
sel_item = select_menu_item();
switch(sel_item)
{
case -1:
break;
case 0:
menu();
break;
case 1:
menu_method(queue,queue_add);
break;
case 2:
menu_method(queue,queue_del);
break;
case 3:
menu_method(queue,queue_clear);
break;
case 4:
menu_method(queue,queue_empty);
break;
case 5:
menu_method(queue,queue_get_front);
break;
case 6:
menu_method(queue,queue_count);
break;
default:
menu();
break;
}
}while(sel_item != -1);
return 0;
}
LINKQUEUE * INITQUEUE()
{
LINKQUEUE * result = (LINKQUEUE *)malloc(SIZE);
return result;
}
LINKQUEUE * CREATE(ELEMTYPE data)
{
LINKQUEUE * result = INITQUEUE();
result->data = data;
return result;
}
LINKQUEUELIST * INITQUEUELIST()
{
LINKQUEUELIST * result = (LINKQUEUELIST *)malloc(LISTSIZE);
result->front = INITQUEUE();
(result->front)->next = NULL;
result->rear = result->front;
result->length = 0;
return result;
}
void ADDQUEUE(LINKQUEUELIST * queue,ELEMTYPE data)
{
LINKQUEUE * item = CREATE(data);
(queue->rear)->next = item;
queue->rear = (queue->rear)->next;
(queue->rear)->next = NULL;
queue->length++;
}
ELEMTYPE GET_FRONT(LINKQUEUELIST * queue)
{
ELEMTYPE result = QUEUE_NULL;
if (EMPTY(queue) == false)
{
result = (queue->front)->next->data;
}
return result;
}
bool EMPTY(LINKQUEUELIST * queue)
{
bool result = false;
if (queue->front == queue->rear)
{
result = true;
}
return result;
}
int CURRENT_SIZE(LINKQUEUELIST * queue)
{
return queue->length;
}
bool CLEAR(LINKQUEUELIST * queue) //依次将front指向元素销毁即可
{
bool result = false;
if (EMPTY(queue) == false)
{
do
{
DELQUEUE(queue);
}while((queue->front)->next != NULL);
result = true;
}
return result;
}
ELEMTYPE DELQUEUE(LINKQUEUELIST * queue)
{
ELEMTYPE result = QUEUE_NULL;
LINKQUEUE * del;
if (EMPTY(queue) == false)
{
del = (queue->front)->next;
(queue->front)->next = del->next;
if (del->next == NULL)
{
queue->rear = queue->front;
}
result = del->data;
queue->length--;
free(del);
}
return result;
}
void menu()
{
print_enter();
print_tab();print_info("link queue manage system");print_enter();
print_menu_item("1","add element to queue");
print_menu_item("2","delete front element");
print_menu_item("3","clear queue");
print_menu_item("4","judge queue is empty");
print_menu_item("5","get queue front elment value");
print_menu_item("6","get queue element count");
print_menu_item("0","return menu");
print_menu_item("-1","exit system");
}
void print_enter()
{
printf("\n");
}
void print_tab()
{
printf("\t");
}
void print_str(char * ch)
{
while(*ch)
{
printf("%c",*ch++);
}
}
void print_info(char * str)
{
print_tab();
print_str(str);
print_enter();
}
void print_menu_item(char * num,char * desc)
{
print_tab();
print_str(num);
print_tab();
print_str(desc);
print_enter();
}
int select_menu_item()
{
int result;
print_tab();
print_str("select menu:");
scanf("%d",&result);
return result;
}
ELEMTYPE add_input()
{
ELEMTYPE result;
print_tab();
print_str("input data:");
scanf(SCANF_ITEM,&result);
return result;
}
void queue_add(LINKQUEUELIST * queue)
{
ELEMTYPE data = add_input();
ADDQUEUE(queue,data);
print_info("add element is ok.");
}
void queue_del(LINKQUEUELIST * queue)
{
ELEMTYPE result = DELQUEUE(queue);
if (result != QUEUE_NULL)
{
print_tab();
printf("delete element ");
printf(PRINT_ITEM,result);
printf(" is ok.");
print_enter();
}
else
{
print_queue_null();
}
}
void queue_clear(LINKQUEUELIST * queue)
{
bool result = CLEAR(queue);
if (result == true)
{
print_info("clear queue is ok.");
}
else
{
print_queue_null();
}
}
void queue_empty(LINKQUEUELIST * queue)
{
bool result = EMPTY(queue);
if (result == true)
{
print_info("queue is empty");
}
else
{
print_info("queue is not empty");
}
}
void queue_get_front(LINKQUEUELIST * queue)
{
ELEMTYPE result = GET_FRONT(queue);
if (result != QUEUE_NULL)
{
print_tab();
printf("the front elemet is ");
printf(PRINT_ITEM,result);
printf(".");
print_enter();
}
else
{
print_queue_null();
}
}
void queue_count(LINKQUEUELIST * queue)
{
int result = CURRENT_SIZE(queue);
print_tab();
printf("the count is : %d",result);
print_enter();
}
void print_queue_null()
{
print_info("error:queue is null.");
}
void menu_method(LINKQUEUELIST * queue,void (* fun)(LINKQUEUELIST * src_queue))
{
(* fun)(queue);
}
|