链式存储的队列问题:
数据结构实验之队列一:排队买饭
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
中午买饭的人特多,食堂真是太拥挤了,买个饭费劲,理工大的小孩还是很聪明的,直接奔政通超市,哈哈,确实,政通超市里面也卖饭,有好几种菜,做的比食堂好吃多了,价格也不比食堂贵,并且买菜就送豆浆,吸引了不少童鞋。所以有时吧,人还是很多的,排队是免不了的,悲剧的是超市只有两个收银窗口。
问题是这样的:开始有两队人在排队,现在咱们只研究第一队,现在我们给每个人一个编号,保证编号各不相同,排在前面的人买完饭就走了,有些人挑完饭就排在后面等待付款,还有一些人比较聪明,看到另一个队人比较少,直接离开这个队到另一个队去了。我要问的是队的总人数和某个位置上人的编号。
Input
首先输入一个整数m(m<10000),代表当前有m个人,第二行输入m个数,代表每个人的编号,第三行输入一个整数n(n<10000),代表队列变动和询问一共n次,以后n行,JOIN X表示编号为X(保证与以前的编号不同)的人加入;LEAVE Y表示第Y(Y小于当前队列长度)个位置 上的人离队 ;ASK Z(Z小于当前队列长度)表示询问第Z个位置上的人的编号;FINISH D表示有D个人买完饭离开了;LENGTH表示询问队列的长度 。保证所有数据在int 范围内.
Output
对每个询问输出相应的答案,每个答案占一行。
Example Input
31
2 3
6
JOIN 4
ASK 2
LEAVE 2
LENGTH
FINISH 2
LENGTH
Example Output
2
3
1
Hint
代码:
001 | #include <stdio.h> |
002 | #include <stdlib.h> |
003 | #include <string.h> |
004 | typedef int QelemType; |
005 | typedef struct Qnode |
006 | { QelemType data; |
007 | struct Qnode *next; |
008 | }Qnode,*QueuePtr; |
009 | struct |
010 | { |
011 | QueuePtr front; |
012 | QueuePtr rear; |
013 | }LinkQueue; |
014 | int InitQuene(LinkQueue *S) |
015 | { |
016 | S->front = S->rear = (Qnode *) malloc ( sizeof (Qnode)); |
017 | if (!S->front) return 0; |
018 | S->rear->next=NULL; |
019 | return 1; |
020 | } |
021 |
022 | int Creat(LinkQueue *S, int e) |
023 | { |
024 | QueuePtr p; |
025 | while (e--) |
026 | { |
027 | p = (QueuePtr) malloc ( sizeof (Qnode)); |
028 | if (!p) return 0; |
029 | scanf ( "%d" ,&p->data); |
030 | S->rear->next = p; |
031 | p->next = NULL; |
032 | S->rear = p; |
033 | } |
034 | return 1; |
035 | } |
036 | int EnQueue(LinkQueue *S,QelemType m) |
037 | { |
038 | QueuePtr p; |
039 | p = (QueuePtr) malloc ( sizeof (Qnode)); |
040 | if (!p) return 0; |
041 | p->data=m; |
042 | S->rear-> next = p; |
043 | p->next = NULL; |
044 | S->rear = p; |
045 | return 1; |
046 | } |
047 | void DeQueue1(LinkQueue *S, int c) |
048 | { |
049 | if (S->front==S->rear) return ; |
050 | QueuePtr p,q; |
051 | p = S->front; |
052 | int i; |
053 | for (i = 1;i<=c-1;i++) |
054 | { |
055 | p = p->next; |
056 | } |
057 | q = p->next; |
058 | if (S->front==p) S->rear = S->front; |
059 | else if (S->rear==q) S->rear = p; |
060 | p->next = q->next; |
061 | free (q); |
062 |
063 | } |
064 | void DeQueue2(LinkQueue *S, int c) |
065 | { |
066 | while (c--) |
067 | { |
068 | if (S->front==S->rear) return ; |
069 | QueuePtr p; |
070 | p = S->front->next; |
071 | if (S->front==p) S->rear = S->front; |
072 | S->front->next = p->next; |
073 | free (p); |
074 | } |
075 |
076 |
077 | } |
078 |
079 | void Find(LinkQueue *S, int c) |
080 | { |
081 | QueuePtr p; |
082 | p = S->front; |
083 | while (c--) |
084 | p = p->next; |
085 | printf ( "%d\n" ,p->data); |
086 | } |
087 |
088 | int main() |
089 | { |
090 | int m,n; |
091 | scanf ( "%d" ,&m); |
092 | LinkQueue S; |
093 | InitQuene(&S); |
094 | Creat(&S,m); |
095 | scanf ( "%d" ,&n); |
096 | char s[10]; |
097 | while (n--) |
098 | { |
099 | int c; |
100 | scanf ( "%s" ,s); |
101 | if ( strcmp ( "JOIN" ,s)==0) |
102 | { |
103 | scanf ( "%d" ,&c); |
104 | EnQueue(&S, c); |
105 | m++; |
106 | } |
107 | if ( strcmp ( "LEAVE" ,s)==0) |
108 | { |
109 | scanf ( "%d" ,&c); |
110 | DeQueue1(&S,c); |
111 | m--; |
112 | } |
113 | if ( strcmp ( "ASK" ,s)==0) |
114 | { |
115 | scanf ( "%d" ,&c); |
116 | Find(&S,c); |
117 | } |
118 | if ( strcmp ( "LENGTH" ,s)==0) |
119 | printf ( "%d\n" ,m); |
120 | if ( strcmp ( "FINISH" ,s)==0) |
121 | { |
122 | scanf ( "%d" ,&c); |
123 | DeQueue2(&S,c); |
124 | m=m-c; |
125 |
126 | } |
127 | } |
128 | return 0; |
129 | } |
130 |