Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368659
  • 博文数量: 62
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:04
文章分类

全部博文(62)

文章存档

2014年(1)

2013年(61)

分类: C/C++

2013-11-17 19:37:28

原文地址:停车场管理的算法 作者:1jjk

其实真的很久没有好好学数据结构了,需要好好的学习学习了。
下面是“停车场管理算法”,虽然比较初级,但是做一做毕竟还是会有收获的,例如熟悉一下,下面把代码扔出来吧:
 
 


/*
name: 停车厂管理算法————数据结构
written by: 1jjk
email:lingjiujianke@gmail.com
*/

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
/*include header file*/
#define stacksize 10
#define sqstack_maxsize 30
/*define*/
typedef struct sqstack
{
 int data[stacksize];
 int top;
}sqstacktp;
typedef struct linked_queue
{
 int data;
 struct linked_queue * next;
}lqueuetp;
typedef struct queueptr
{
 lqueuetp *front, *rear;
}queuetp;
/*typedef new struct*/
/*init a stack*/
int initstack(sqstacktp *sq)
{
 sq->top=0;
 return 1;
}
/*push in stack*/
int push(sqstacktp *sq,int x)
{
 if(sq->top == sqstack_maxsize)
 {
  return 0;
 }
 else
 {
  sq->top++;
  sq->data[sq->top]=x;
  return 1;
 }
}
/*pop from stack*/
int pop(sqstacktp *sq, int *x)
{
 if(sq->top==0)
 {
  printf("stack out\n");
  return 0;
 }
 else
 {
  *x=sq->data[sq->top];
  sq->top--;
  return 1;
 }
}
/*is the stack empty?*/
int emptystack(sqstacktp sq)
{
 if(sq.top==0)
 {
  return 1;
 }
 else
 {
  return 0;
 }
}
/*is it the top of the stack?*/
int gettop(sqstacktp *sq,int *x)
{
 if(sq->top==0)
 {
  return 0;
 }
 else
 {
  *x=sq->data[sq->top];
  return 1;
 }
}
/*is it the init queue*/
int initqueue(queuetp *lq)
{
 lqueuetp *p;
 p=(lqueuetp *)malloc(sizeof(lqueuetp));
 lq->front=p;
 lq->rear=p;
 (lq->front)->next=NULL;
 return 1;
}
/*in queue*/
void enqueue(queuetp *lq, int x)
{
 lqueuetp *p;
 p=(lqueuetp *)malloc(sizeof(lqueuetp));
 p->data=x;
 p->next=NULL;
 (lq->rear)->next=p;
 lq->rear=p;
}
/*out queue*/
int outqueue(queuetp *lq,int *x)
{
 lqueuetp *s;
 if(lq->front==lq->rear)
 {
  printf("queue out\n");
  return 0;
 }
 else
 {
  s=(lq->front)->next;
  *x=s->data;
  (lq->front)->next=s->next;
  if(s->next==NULL)
  {
   lq->rear=lq->front;
  }
  free(s);
  return 1;
 }
}
/*is the queue empty?*/
int emptyqueue(queuetp lq)
{
 if(lq.rear==lq.front)
 {
  return 1;
 }
 else
 {
  return 0;
 }
}
/*get the head of the queue*/
int gethead(queuetp lq,int *x)
{
 lqueuetp *p;
 if(lq.rear==lq.front)
 {
  return 0;
 }
 else
 {
  p=lq.front->next;
  *x=p->data;
  return 1;
 }
}
/*the main function*/
int main()
{
 sqstacktp ps,ts;
 queuetp lq;
 int out,number,temp=0;
 char ch;
 
 initstack(&ps);
 initstack(&ts);
 initqueue(&lq);
 /*init the stack and the queue*/
 out=0;
 /*get a char,if the char is q,or Q then out,if not then get a number*/
 scanf("%c",&ch);
 if(ch=='Q'||ch=='q')
  return 0;
 scanf("%d",&number);
 
 /*loop from here,if the number max than 0 then loop */
 while(number>0)
 {
  switch(ch)
  {
   /*if the char is a or A then do it:*/
   case 'A':
   case 'a':
    if(ps.top==sqstack_maxsize-1)
    {
     enqueue(&lq,number);
     printf("it full in %d\n", number);
    }
    else
    {
     push(&ps,number);
     printf("the num %d in \n", number);
    }
    break;
    
    /*if the char is Dor d then do it:*/
   case 'D':
   case 'd':
    while(!emptystack(ps))
    {
     pop(&ps,&temp);
     if(temp!=number)
     {
      push(&ts,temp);
     }
     else
     {
      printf("the num %d out\n",number);
      out=1;
      break;
     }
     
    }
    if(out==0)
    {
     printf("the num %d is not in\n",number);
    }
    while(!emptystack(ts))
    {
     pop(&ts,&temp);
     push(&ps,temp);
    }
    if(out&&!emptyqueue(lq))
    {
     outqueue(&lq,&temp);
     printf("the num %d is car stop room\n",temp);
     push(&ps,temp);
    }
   out=0;
   break;
  }
  /*get the char and the number again*/
    scanf("%c",&ch);
    if(ch=='Q'||ch=='q')
     return 0;
    scanf("%d",&number);
    
  
 }
return 0;
}

阅读(1668) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~