zoukankan      html  css  js  c++  java
  • 杭电acm1908

    http://acm.hdu.edu.cn/showproblem.php?pid=1908

    题目大意是做一个操作系统,可执行4个系统操作,输入0(退出),1(输入P和K,并且以p排序后存储),2(输出p最小的那个k),3(输出p最大的那个k)注意每次输出后都要删掉那些输出的数据。。。。。我用一条双向链表写的,并设定了头结点和尾结点,,,这之中,头结点的next域存储了表头,尾结点的prior域存储了表尾,因为第一次写,,,,所以折磨了很久,每次输入P和K后都按p排序,然后一起存储

    View Code
      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 typedef struct node{
      4                     int k;
      5                     int p;
      6                     struct node *next;
      7                     struct node *prior;
      8                     }S;
      9 S *head,*last;
     10 S *creat()
     11 {
     12    S *s;
     13    s=(S *)malloc(sizeof(S));
     14    s->next=s->prior=NULL;
     15    return s;
     16 }
     17 S *insert(int k,int p)
     18 {
     19    S *q=head->prior,*n=head->next,*r;
     20        r=(S *)malloc(sizeof(S));
     21        r->k=k;
     22        r->p=p;
     23    if(n==last)
     24    {
     25        head->next=r;
     26        r->next=last;
     27        r->prior=head;
     28        last->prior=r;
     29    }
     30    else {
     31          while(n->next)
     32          {
     33             if(p<n->p&&n==head->next)
     34                {
     35                   head->next=r;
     36                   r->next=n;
     37                   r->prior=head;
     38                   n->prior=r;
     39                   break;
     40                }
     41             if(p<n->p&&p>n->prior->p)
     42                 {
     43                     n->prior->next=r;
     44                     r->prior=n->prior;
     45                     r->next=n;
     46                     n->prior=r;
     47                     break;
     48                 }
     49             if(p>n->p&&n==last->prior)
     50               {   
     51                   n->next=r;
     52                   r->prior=last->prior;
     53                   r->next=last;
     54                   last->prior=r;
     55                   break;
     56               }
     57               n=n->next;
     58          }
     59          }
     60 }
     61 void printmax()
     62 {
     63      S *s=head->next;
     64      if(head->next==last)
     65         printf("0\n");
     66      else 
     67         {
     68            printf("%d\n",s->k);
     69            s->next->prior=head;
     70            head->next=s->next;
     71            free(s);
     72         }
     73 }
     74 S * printmin()
     75 {
     76      S *s=last->prior;
     77      if(last->prior==head)
     78      printf("0\n");
     79      else 
     80      {
     81          printf("%d\n",s->k);
     82          s->prior->next=last;
     83          last->prior=s->prior;
     84          free(s);
     85      }
     86 }
     87 int main()
     88 {
     89     int n,k,p;
     90     head=creat();
     91     last=creat();
     92     head->next=last;
     93     last->prior=head;
     94     while(scanf("%d",&n)&&n)
     95     {
     96         if(n==1)
     97         {
     98             scanf("%d%d",&k,&p);
     99             insert(k,p);
    100         }
    101         else if(n==2)
    102           printmin();
    103         else printmax();
    104     }
    105     return 0;    
    106 }
  • 相关阅读:
    【转】常见经济类名词解释
    Linux parted命令详解
    【转】Linux下从TCP状态机,三次握手判断DDOS攻击
    【转】Java学习---HashMap和HashSet的内部工作机制
    【转】Redis学习---阿里云Redis多线程性能增强版详解
    改变自己,改变世界
    对话任正非两万字实录:最重要的是要沉着
    qt手写输入法资料
    Qt框架及模块认识
    哲学必读10本经典著作
  • 原文地址:https://www.cnblogs.com/huzhenbo113/p/3072562.html
Copyright © 2011-2022 走看看