0% found this document useful (0 votes)
2 views4 pages

Queue Homework

The document is a C# program that implements various operations on a queue of integers, including insertion, sorting, counting occurrences, and separating even and odd numbers. It defines multiple methods to manipulate the queue and perform tasks like finding the smallest common element between two queues. The main function demonstrates the usage of these methods by inserting integers into a queue and printing the results before and after sorting.

Uploaded by

golan.perry2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Queue Homework

The document is a C# program that implements various operations on a queue of integers, including insertion, sorting, counting occurrences, and separating even and odd numbers. It defines multiple methods to manipulate the queue and perform tasks like finding the smallest common element between two queues. The main function demonstrates the usage of these methods by inserting integers into a queue and printing the results before and after sorting.

Uploaded by

golan.perry2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

...11th_grade_stuff\11th_grade_stuff\queue_homework.

cs 1
1 using System;
2 using [Link];
3 using [Link];
4 using [Link];
5 using [Link];
6 using [Link];
7 using [Link];
8
9
10 namespace _11th_grade_stuff
11 {
12 internal class queue_homework
13 {
14 public static void Main(string[] args)
15 {
16 Queue<int> q1 = new Queue<int>();
17 [Link](5);
18 [Link](4);
19 [Link](6);
20 [Link](3);
21 [Link](7);
22 PrintQueue(q1);
23 SortQueue(q1);
24 PrintQueue(q1);
25 }
26 public static void PrintQueue(Queue<int> q1)
27 {
28 Queue<int> tmp = new Queue<int>();
29
30 while (![Link]())
31 {
32 int x = [Link]();
33 [Link](x + " ");
34 [Link](x);
35 }
36
37 while (![Link]())
38 {
39 [Link]([Link]());
40 }
41
42 [Link]();
43 }
44 public static int QueueLength(Queue<int> q1)
45 {
46 Queue<int> tmp = new Queue<int>();
47 int count = 0;
48
49 while (![Link]())
50 {
51 int x = [Link]();
52 count = count + 1;
53 [Link](x);
...11th_grade_stuff\11th_grade_stuff\queue_homework.cs 2
54 }
55
56 while (![Link]())
57 {
58 [Link]([Link]());
59 }
60
61 return count;
62 }
63
64 public static void SortQueue(Queue<int> q1)
65 {
66 int[] arr = new int[QueueLength(q1)];
67 int len = QueueLength(q1);
68 for (int i = 0; i < len; i++)
69 {
70 arr[i] = [Link]();
71 }
72 for (int i = 0; i < len; i++)
73 {
74 for (int j = i; j < len; j++)
75 {
76 if (arr[i] > arr[j])
77 {
78 int temp = arr[i];
79 arr[i] = arr[j];
80 arr[j] = temp;
81 }
82 }
83 }
84 for (int i = 0; i < len; i++)
85 {
86 [Link](arr[i]);
87 }
88 }
89 public static int distQueue(Queue<int> q1, int x, int y)
90 {
91 int count = 0;
92 Queue<int> q2 = new Queue<int>();
93 while (![Link]())
94 {
95 int tempNum = [Link]();
96 if (tempNum >= y && tempNum <= x)
97 {
98 count++;
99 }
100 [Link](tempNum);
101 }
102
103 while (![Link]())
104 {
105 [Link]([Link]());
106 }
...11th_grade_stuff\11th_grade_stuff\queue_homework.cs 3
107
108 return count;
109 }
110 public static int SmallestCommon(Queue<int> q1, Queue<int> q2)
111 {
112 Queue<int> a = new Queue<int>();
113 Queue<int> b = new Queue<int>();
114 int ans = -1;
115
116 while (![Link]() && ![Link]())
117 {
118 int x = [Link]();
119 int y = [Link]();
120
121 [Link](x);
122 [Link](y);
123
124 if (x == y && ans == -1)
125 {
126 ans = x;
127 }
128 else if (x < y)
129 {
130 [Link](y);
131 }
132 else
133 {
134 [Link](x);
135 }
136 }
137
138 while (![Link]()) [Link]([Link]());
139 while (![Link]()) [Link]([Link]());
140 while (![Link]()) [Link]([Link]());
141 while (![Link]()) [Link]([Link]());
142
143 return ans;
144 }
145
146 public static Queue<int> CountAppear(Queue<int> q1)
147 {
148 Queue<int> q2 = new Queue<int>();
149 Queue<int> q3 = new Queue<int>();
150 while (![Link]())
151 {
152 [Link]([Link]());
153 }
154 while (![Link]())
155 {
156 int num = [Link]();
157
158 int count = 1;
159 Queue<int> temp = new Queue<int>();
...11th_grade_stuff\11th_grade_stuff\queue_homework.cs 4
160
161 while (![Link]())
162 {
163 int t = [Link]();
164 if (t == num)
165 {
166 count++;
167 }
168 [Link](t);
169 }
170
171 while (![Link]())
172 {
173 [Link]([Link]());
174 }
175
176 [Link](num);
177 [Link](count);
178 [Link](num);
179 }
180
181 return q3;
182 }
183 public static void EvensFirst(Queue<int> q)
184 {
185 Queue<int> ev = new Queue<int>();
186 Queue<int> od = new Queue<int>();
187
188 while (![Link]())
189 {
190 int x = [Link]();
191 if (x % 2 == 0) [Link](x);
192 else [Link](x);
193 }
194
195 while (![Link]()) [Link]([Link]());
196 while (![Link]()) [Link]([Link]());
197 }
198
199
200 }
201 }
202

You might also like