基礎的學生管理系統——增,刪,查,找(非空雙向循環鏈表)C語言Linux實訓作業

list.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "llist.h"

LLIST *llist_creat(int size)
{
	LLIST *new;
	new = malloc(sizeof(LLIST));
	if(new == NULL)
	{
		return NULL;
	}
	new->size = size;
	new->head.prev = new->head.next = &new->head;
	return new;
}
int llist_insert(LLIST *ptr, const void *data, int moden)
{
	struct llist_node_pe *newnode;
	newnode = malloc(sizeof(*newnode) + ptr->size);
	if(newnode == NULL)
	{
		return -1;
	}
	memcpy(newnode->data, data, ptr->size);
	if(moden == LLIST_FORWARD)
	{
		newnode->next = ptr->head.next;
		newnode->prev = &ptr->head;
	}
	else if(moden == LLIST_BACKWARD)
	{
		newnode->next = &ptr->head;
		newnode->prev = ptr->head.prev;
	}
	newnode->next->prev = newnode;
	newnode->prev->next = newnode;
	return 0;
}
void llist_travle(LLIST *ptr, void (*op)(void *))
{
	struct llist_node_pe *cur;
	for(cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
	{
		op(cur->data);
	}
}
static struct llist_node_pe *find_(LLIST *ptr, void *key, int (*cmp)(void *, void *))
{
	struct llist_node_pe *cur;
	for(cur = ptr->head.next; cur != &ptr->head; cur = cur->next)
	{
		if(cmp(key, cur->data) == 0)
		{
			break;
		}
	}
	return cur;
}
void *llist_find(LLIST *ptr, void *key, int (*cmp)(void *, void *))
{
	struct llist_node_pe *node;
	node = find_(ptr, key, cmp);
	if(node == &ptr->head)
	{
		return NULL;
	}
	return node->data;
}
void llist_del(LLIST *ptr, void *key, int (*cmp)(void *, void *))
{
	struct llist_node_pe *node;
	node = find_(ptr, key, cmp);
	if(node == &ptr->head)
	{
		return ;
	}
	node->next->prev = node->prev;
	node->prev->next = node->next;
	free(node);
}
int llist_fetch(LLIST *ptr, void *data,void *key, int (*cmp)(void *, void *))
{
	struct llist_node_pe *node;
	node = find_(ptr, key, cmp);
	if(node == &ptr->head)
	{
		return -1;
	}
	memcpy(data, node->data, ptr->size);
	node->next->prev = node->prev;
	node->prev->next = node->next;
	free(node);
}
void llist_destroy(LLIST *ptr)
{
	struct llist_node_pe *cur, *save;
	for(cur = ptr->head.next; cur != &ptr->head; cur = save)
	{
		save = cur->next;
		free(cur);
	}
	free(ptr);
}




list.h

#ifndef LLIST_H
#define LLIST_H
#define LLIST_FORWARD 1
#define LLIST_BACKWARD 2

struct llist_node_pe
{
	struct llist_node_pe *prev;
	struct llist_node_pe *next;
	char data[0];
};
typedef struct
{
	int size;
	struct llist_node_pe head;
}LLIST;

LLIST *llist_creat(int size);
int llist_insert(LLIST *ptr, const void *data, int moden);
void llist_travle(LLIST *ptr, void (*op)(void *));
void *llist_find(LLIST *ptr, void *key, int (*cmp)(void *, void *));
void llist_del(LLIST *ptr, void *key, int (*cmp)(void *, void *));
int llist_fetch(LLIST *ptr, void *data,void *key, int (*cmp)(void *, void *));
void llist_destroy(LLIST *ptr);


#endif

main.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "queue.h"
#include "llist.h"
#include "main.h"

int a[1050];
int b[1050];
int c[1050];

int i, j, k;

QUEUE *normal=NULL;
QUEUE *vip=NULL;
QUEUE *trade=NULL;

struct line
{
	int flag;
	int num;
};
struct normal
{
	int num;
};
struct vip
{
	int num;
};
struct trade
{
	int num;
};



void normal_create()
{
	normal=queue_create(sizeof(struct normal));
}
void vip_create()
{
	vip=queue_create(sizeof(struct vip));
}
void trade_create()
{
	trade=queue_create(sizeof(struct trade));
}



void add_normal()
{
	struct normal nom;
	nom.num=i;
	queue_enq(normal,&nom);
	printf("類型:普通用戶,序號:%d號\n",nom.num);
	i++;
}
void add_vip()
{
	struct vip vv;
	vv.num=j;
	queue_enq(vip,&vv);
	printf("類型:vip用戶,序號:%d號\n",vv.num);
	j++;
}
void add_trade()
{
	struct trade tra;
	tra.num=k;
	queue_enq(trade,&tra);
	printf("類型:企業用戶,序號:%d號\n",tra.num);
	k++;
}

LLIST *servelist=NULL;

void serve_create()
{
	servelist=llist_creat(sizeof(struct line));
}
void add_serve()
{
	int x;
	printf("請輸入客戶端窗口數量:");
	scanf("%d", &x);
	int y;
	printf("請輸入vip端窗口數量:");
	scanf("%d", &y);
	int z;
	printf("請輸入企業端窗口數量:");
	scanf("%d", &z);
	int t;
	for(t = 1;t <= x; t++)
	{
		struct line l;
		l.flag=0;
		l.num=t;
		llist_insert(servelist,&l,LLIST_BACKWARD);
	}
	for(t = x+1; t<=x+y; t++)
	{
		struct line l;
		l.flag=2;
		l.num=t;
		llist_insert(servelist,&l,LLIST_BACKWARD);
	}
}
void match(void *data)
{
	struct line *p=data;
	struct vip vv;
	struct normal nor;
	struct trade tra;
	if(queue_empty(vip)==1)
	{
		if(p->flag == 0)
		{
			queue_deq(vip,&vv);
			printf("請VIP客戶%d號到%d號窗口\n", vv.num,p->num);
			a[vv.num]=p->num;
			p->flag=1;
		}
	}
	else if(queue_empty(vip)==0&&queue_empty(normal)==1)
	{
		if(p->flag == 0)
		{
			queue_deq(normal,&nor);
			printf("請普通客戶%d號到%d號窗口\n",nor.num,p->num);
			b[nor.num]=p->num;
			p->flag=1;
		}
	}
	if(queue_empty(trade)==1)
	{
		if(p->flag==2)
		{
			queue_deq(trade,&tra);
			printf("請企業客戶%d號到%d號窗口\n", tra.num,p->num);
			c[tra.num]=p->num;
			p->flag=3;
		}
	}
}
void call_serve()
{
	llist_travle(servelist,match);
}


int cmp_normal(void *p, void *q)
{
	int *num=p;
	struct normal *n=q;
	return *num-n->num;
}
int cmp_vip(void *p, void *q)
{
	int *num=p;
	struct vip *n=q;
	return *num-n->num;
}
int cmp_trade(void *p, void *q)
{
	int *num=p;
	struct trade *n=q;
	return *num-n->num;
}
int cmp__(void *q, void *p)
{
	int *key=q;
	struct line *l=p;
	return *key-l->num;
}


void leave()
{
	printf("請選擇您的身份類型\n");
	printf("1.普通用戶\n");
	printf("2.VIP用戶\n");
	printf("3.企業用戶\n");
	int x;
	scanf("%d",&x);
	if(x==1)
	{
		int y;
		printf("請輸入您的號碼");
		scanf("%d",&y);
		struct line *l;
		l=llist_find(servelist,&b[y],cmp__);
		l->flag=0;
	}
	else if(x==2)
	{
		int y;
		printf("請輸入您的號碼");
		scanf("%d",&y);
		struct line *l;
		l=llist_find(servelist,&a[y],cmp__);
		l->flag=0;
	}
	else if(x==3)
	{
		int y;
		printf("請輸入您的號碼");
		scanf("%d",&y);
		struct line *l;
		l=llist_find(servelist,&c[y],cmp__);
		l->flag=0;
	}
	else
	{
		printf("輸入錯誤\n");
	}
}
void quit()
{
	printf("請選擇您的身份類型\n");
	printf("1.普通用戶\n");
	printf("2.VIP用戶\n");
	printf("3.企業用戶\n");
	int x;
	scanf("%d",&x);
	if(x==1)
	{
		int y;
		printf("請輸入您的號碼");
		scanf("%d",&y);
		llist_del(servelist,&y,cmp_normal);
	}
	else if(x==1)
	{
		int y;
		printf("請輸入您的號碼");
		scanf("%d",&y);
		llist_del(servelist,&y,cmp_vip);
	}
	else if(x==3)
	{
		int y;
		printf("請輸入您的號碼");
		scanf("%d",&y);
		llist_del(servelist,&y,cmp_trade);
	}
	else
	{
		printf("輸入錯誤\n");
	}	
}
void menu()
{
	while(1)
	{
		printf("銀行自主系統\n");
		printf("工作人員請按1\n");
		printf("客戶請按2\n");
		printf("退出請按3\n");
		int x;
		scanf("%d", &x);
		system("clear");
		if(x==1)
		{
			printf("1.開放窗口\n");
			printf("2.叫號\n");
			int y;
			scanf("%d",&y);
			if(y==1)
			{
				add_serve();
			}	
			else if(y==2)
			{
				call_serve();
			}
			else
			{
				printf("輸入錯誤\n");
			}
		}
		else if(x==2)
		{
			printf("請按鍵取號\n");
			printf("1.普通用戶\n");
			printf("2.VIP用戶\n");
			printf("3.企業用戶\n");
			printf("4.退出\n");
			int y;
			scanf("%d",&y);
			system("clear");
			if(y==1)
			{
				add_normal();
			}
			else if(y==2)
			{
				add_vip();
			}
			else if(y==3)
			{
				add_trade();
			}
			else if(y==4)
			{
				printf("辦理完業務離開請按1\n");
				printf("未辦理完業務離開請按2\n");
				int z;
				scanf("%d",&z);
				if(z==1)
				{
					leave();
				}
				else if(z==2)
				{
					quit();
				}
				else
				{
					printf("輸入錯誤\n");
				}	
			}
			else
			{
				printf("輸入錯誤\n");
			}
		}
		else if(x==3)
		{
			break;
		}
		else
		{
			printf("輸入錯誤\n");
		}
	}
}
int main()
{
	normal_create();
	vip_create();
	trade_create();
	serve_create();
	i=1;
	j=1;
	k=1;
	memset(a,-1,sizeof(a));
	memset(b,-1,sizeof(b));
	memset(c,-1,sizeof(c));
	menu();
	return 0;
}

main.h

#include "llist.h"
#ifndef MAIN_H
#define MAIN_H

void normal_create();
void vip_create();
void trade_create();
void add_normal();
void add_vip();
void add_trade();
void serve_create();
void add_serve();
void match(void *data);
void call_serve();

int cmp_normal(void *p, void *q);
int cmp_vip(void *p, void *q);
int cmp_trade(void *p, void *q);
int cmp__(void *q, void *p);

void leave();
void quit();

void menu();

#endif

queue.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "queue.h"

QUEUE *queue_create(int size)
{
	return llist_creat(size);
}
void queue_destroy(QUEUE *ptr)
{
	llist_destroy(ptr);
}
int queue_enq(QUEUE *ptr,void *data)
{
	llist_insert(ptr,data,LLIST_BACKWARD);
}
static int always_match( void *p1, void *p2)
{
	return 0;
}
int queue_deq(QUEUE*ptr,void *data)
{
	return llist_fetch(ptr,data,NULL,always_match);
}
int queue_empty(QUEUE*ptr)
{
	if(ptr->head.next==&ptr->head)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

queue.h

#ifndef QUEUE_H
#define QUEUE_H
#include "llist.h"

typedef LLIST QUEUE;

QUEUE *queue_create(int size);
void queue_destroy(QUEUE *);
int queue_enq(QUEUE *,void *data);
int queue_deq(QUEUE*,void *data);
int queue_empty(QUEUE*ptr);

#endif

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章