运用链表和队列实现停车场的一些简易功能

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<string.h>


#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20
#define N 10
#define len sizeof(QNode)


typedef int Status;
typedef int QElemType;


typedef struct QNode//定义一个结构体
{
 char name[N];
 char numb[MAXSIZE];
 QElemType VIP;
 struct QNode *next;
}QNode,*QueuePtr;


typedef struct
{
 QueuePtr front,rear;
}LinkQueue;




LinkQueue* CreatQueue()//创建队列
{
 LinkQueue *Q;
 Q = (LinkQueue *)malloc(sizeof(LinkQueue));
 QueuePtr p1 = (QueuePtr)malloc(len);
 QueuePtr p2 = (QueuePtr)malloc(len);
 int i = 1;
 p1->next = NULL;
 Q->front = p2;
 while(i<5)//利用循环建立有10个结构体的队列
 {
   printf("请输入车主姓名:\n");
   scanf("%s",p1->name);
   getchar();
   printf("请输入车牌号:\n");
   scanf("%s",p1->numb);
   getchar();
   printf("请输入VIP等级:\n");
   scanf("%d",&p1->VIP);
   getchar();
   Q->rear = p1;
   i++;
    p2->next = p1;//结链
   p2 = p1;
   p1 = (QueuePtr)malloc(len);//建立新的结构体
   p2->next = NULL; 
 }
 return Q;
}


Status Print1(QueuePtr front)//打印信息
{
 QueuePtr p;
 p = front->next;
 if(front != NULL)
 {
   while(p != NULL)
   {
     printf("车主姓名:%s,车牌号:%s,VIP等级:%d\n",p->name,p->numb,p->VIP);
     p = p->next;
   }
 }
}


QNode* LinkList(QueuePtr front)//创建链表
{
 QueuePtr p2 = NULL;
 QueuePtr p = NULL;


 p = front->next;


 QueuePtr p1 = (QueuePtr)malloc(len);
 QueuePtr head = (QueuePtr)malloc(len);


 QueuePtr p3 = NULL;
 QueuePtr p4 = NULL;
 QueuePtr p5;
 QueuePtr p6;
 QueuePtr p7;
 head->next = NULL;
 p2 = head;
 
 while(p != NULL)//利用循环复制队列的值
 {
   strcpy(p1->name,p->name);
   strcpy(p1->numb,p->numb);
   p1->VIP = p->VIP;
   p1->next = NULL;
   p = p->next;
   p2->next = p1;
   p2 = p1;
   p1 = (QueuePtr)malloc(len);
 }
 p2 -> next = NULL;


 p3 =  head->next;//简易冒泡排序
 p5 = head;
 while(p3->next != NULL)//p3是大循环
 {
   p6 = p3;
   p4 = p3->next;
   while(p4 != NULL)//p4是小循环
   {
     if(p3->VIP < p4->VIP )
     {
       if(p3 == p6)//当p3与p4相邻时
       {
         p5->next = p4;
         p3->next = p4->next;
         p4->next = p3;
         p6 = p5->next;
    }
       else//交换p3与p4节点的位置
       {
         p5->next = p3->next;
         p6->next = p4->next;
         p3->next = p6->next;
         p6->next = p3;
         p4->next = p5->next;
         p5->next = p4;
       }
       p4 = p3;//使p3与p4指向对方的节点
       p3 = p5->next;
    }
    p4 = p4->next;
     p6 = p6->next;
   }
   p3 = p3->next;
   p5 = p5->next;
 }  
 return head;
}
/*void Select(QueuePtr head)//选择排序
{
 int i = 0;
 QueuePtr p1 = NULL,p2 = NULL,p3 = NULL,p4 = NULL,p5 = NULL;p6 = head->next;
 p2 = head;
 while(p2->next != NULL)//外层寻环
 {
   p1 = p2;
   p3 = p2->next;
   p4 = p2->next;
   for(;p3 != NULL;p3 = p3->next,p1 = p1->next)//p3指针遍历数组
   {
     if(p3->VIP > p4->VIP)//当p3的VIP大时p4记录p3的位置,p5纪录p3的上一位
     {
    p4 = p3;
       p5 = p1;
     }
   }
   if(p4 != p2->next)//当p4和p2->next不指向同一位置时交换
   {
     p5->next = p4->next;
     p4->next = p2->next;
     p2->next = p4;
   }
   p2 = p2->next;
   p2->place = ++i;//分配车位
 }
 while(p6 != NULL)
 {
   printf("%s,%s,%d\n",p->name,p->numb,p->VIP);
   p6 = p6->next;

 }

}*/

int Print2(QueuePtr head)
{
 QueuePtr p = head->next; 
 while(p != NULL)
 {
   printf("车主姓名:%s,车牌号:%s,VIP等级:%d\n",p->name,p->numb,p->VIP);
   p = p->next;
 }
}


int visit(QueuePtr head)
{
 QueuePtr p = head->next;
 QueuePtr q = NULL;
 q = (QueuePtr)malloc(len);
 printf("选择你所要查询的车主的姓名:\n");
 scanf("%s",q->name);
 getchar();
 while(strcmp(q->name,p->name) != 0)
 {
   p = p->next;
   if(p == NULL)
     break;
 }
 if(p == NULL)
 printf("查无此人!");
 else
 printf("%s,%s,%d\n",p->name,p->numb,p->VIP);
}


QueuePtr de(QueuePtr head)
{
 QueuePtr p = head->next;
 QueuePtr q = NULL;
 q = (QueuePtr)malloc(len);
 printf("选择你所要删除的车主的姓名:\n");

 scanf("%s",q->name);

 getchar();

 while(strcmp(q->name,p->name) != 0)

 {
   p = p->next;
   if(p == NULL)
     break;
 }
 if(p == NULL)
 printf("查无此人!");
 else
 {
   strcpy(p->name,"0");
   strcpy(p->numb,"0");
   p->VIP = 0;
 }
 return head;
}


int main()
{
 LinkQueue *Q;
 QueuePtr head;
 int i;
 while(1)
 {
   printf("欢迎使用停车场功能!\n");
   printf("请选择您所需要的功能:1.停10辆车 2.查看出停车场时的车辆信息 3.查看停车场车位信息 4.查询车主信息 5.删除车辆信息 ");
   scanf("%d",&i);
   getchar();
   switch(i)
   {
     case 0:exit(0);
     case 1:Q = CreatQueue();head = LinkList(Q->front);break;
     case 2:Print1(Q->front);break;
     case 3:Print2(head);break;
     case 4:visit(head);break;
     case 5:de(head);break;
   }
 }
 return OK;
}

猜你喜欢

转载自blog.csdn.net/chenwenjie666/article/details/79202849