Priority Queue in c linked list [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have created this program for priority queue and I am having a problem. I am getting the wrong output.
Here is the input:
Insert 10000 2
Insert 10000 2
Insert 10000 3
Insert 19444 9
Pop
Insert 10331 3
Pop
Pop
Pop
Pop
Pop
Here's what the output should be:
19444
10000
10331
10000
10000
-1
Here's the output which I get:
19444
10000
10000
10000
10331
-1
SOLVED !

I believe your priority checking logic is incorrect:
while (queue->next != NULL && queue->next->prior >= /* not <= */ priorty)
or better yet
while (queue->next != NULL && priorty <= queue->next->prior)
Not sure how you intend to handle the case where two elements have the same priority, but since your insert uses "greater than" to replace the head of the queue you probably want to keep the same logic.

You loop for inserting the node is incorrect, it should read:
while (queue->next != NULL && queue->next->prior >= priorty)
queue = queue->next;

Related

Sum value of col 3 based on col 1 and col 2 value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to sum value of col3 based on col1 and col2
here is the link of dummy data sheet in which I have shown sample table according to which I want sum of col 3 please guys check the sheet and suggest me some solution for it.
https://docs.google.com/spreadsheets/d/1_MeiySJHI8OD84BPDOj_z57My-TXbs2ey4AOkQP3zug/edit?usp=sharing
use QUERY:
=QUERY(A:C; "select A,B,sum(C) where A is not null group by A,B label sum(C)''")
update:
={"result"; INDEX(IF(COUNTIFS(A2:A&" "&B2:B, A2:A&" "&B2:B, ROW(A2:A), "<="&ROW(A2:A))=1,
IFNA(VLOOKUP(A2:A&" "&B2:B,
QUERY({A2:A&" "&B2:B, C2:C},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''"), 2, 0)), ))}

Splitting Strings then Creating Segments out of those. T-SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How do I split a csv string into this format in SQL Server?
Initial String value (A, B, C, D) into :
A-B
B-C
C-D
You can try using string_split in conjunction with lead()
select value + '-' + lead(value) over (order by value) new_value
from string_split('A,B,C,D',',')
SQL FIDDLE:
http://sqlfiddle.com/#!18/0a28f/2607
Grab a copy of NGrams8K then you could simply do this.
DECLARE #string VARCHAR(100) = 'A, B, C, D';
SELECT TheString = CONCAT(ng.Token,'-',ng.Nxt)
FROM
(
SELECT ng.Token, Nxt = LEAD(ng.Token,1) OVER (ORDER BY ng.Position)
FROM dbo.ngrams8k(#string,1) AS ng
WHERE ng.Token LIKE '%[a-z]%'
) AS ng
WHERE ng.Nxt IS NOT NULL;
Returns:
TheString
---------------------
A-B
B-C
C-D
Order is guaranteed without a sort in the execution plan.

SQL row count differently than array count which is defined at 10000. Please suggest. Expecting SQL row count should be same as 10000 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
if [ "${ARRAY_SIZE}" = "" ];then
ARRAY_SIZE=10000
fi
declare
type t_reqseqArray is table of TEMP_TABLE_BKP.REQ_SEQ%type;
v_reqseqArray t_reqseqArray;
v_array_limit number(5) := ${ARRAY_SIZE};
v_commit_rate number(5) := ${COMMIT_RATE};
v_fetch_count number(5) := 0;
cursor c_reqseq is select req_seq from TEMP_TABLE_bkp;
v_TEMP_TABLE_bkp number(16) := 0;
v_array number(16) := 0;
begin
open c_reqseq;
loop
v_fetch_count := v_fetch_count + 1;
fetch c_reqseq bulk collect into v_reqseqArray limit v_array_limit;
v_array := v_reqseqArray.count;
dbms_output.put_line('Array count, '||to_char(v_array,'9999999')||' Request Sequences got');
forall idx in 1..v_reqseqArray.count
update TEMP_TABLE_bkp set req_seq=req_seq+1 where req_seq = v_reqseqArray(idx);
v_TEMP_TABLE_bkp := sql%rowcount;
exit when c_reqseq%notfound;
if (v_fetch_count >= v_commit_rate) then
dbms_output.put_line('Commit point reached, '||to_char(v_TEMP_TABLE_bkp,'9999999')||' Request Sequences got updated.');
${COMMIT};
v_fetch_count := 0;
end if;
end loop;
close c_reqseq;
end;
/
And result shows, Array count as 10000 where as SQL row count showed 12907.
Array count, 10000 Request Sequences got
12907
Commit point reached, 12907 Request Sequences got updated.
Array count, 10000 Request Sequences got
8660
Commit point reached, 8660 Request Sequences got updated.
Array count, 4001 Request Sequences got
2434
sql%rowcount is the number of rows updated by the whole forall .. update statement, not the number of times FORALL executed DML. Single update can affect many rows.
You can check also SQL%BULK_ROWCOUNT pseudo collection, it contains the number of rows affected for each DML statement executed by FORALL and you will see, that for some values v_reqseqArray(idx) it updated many rows.
Also bear in mind that your transaction sees your updates: cursor contains old values, the table contains updated, and UPDATE can see updates done in the same transaction.
BTW this is classic fetch across commit example.

ISNULL Not working when I am calculating the sum of two columns in the expression part of the function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to use ISNULL (), but in the expression part of the function, instead of having a column name as the argument,I have the sum of four columns, and it should return a 0 when all of the value in the column is NULL, else the sum. for e.g., if three columns have values and corresponding value in the other column is Null, it should still return the sum of the three values. This is how I have written my query:
Select ISNULL([FY18 P1]+[FY18 P2]+[FY18 P3]+[FY18 P4],0) as [Previous YTD]
from TableA
This calculated column inside the ISNULL function is not working. Can anybody help me rewrite this expression so that it will work. What i mean when it is not working is that, it is returning a NULL when only one column is NULL but the rest of the columns have a value. Basically it should return the sum and not NULL in this case.
If any column in your concatenation IS NULL then the result will be NULL.
You need to wrap each column in IS NULL to make this column value 0 so that your addition doesn't return NULL.
SELECT ISNULL([FY18 P1],0) +ISNULL([FY18 P2],0) + ISNULL([FY18 P3],0) + ISNULL([FY18 P4],0)
This is because anything + NULL returns NULL
select 1 + 2 + 3 + NULL --returns `NULL`

Indexing Computed Column of 2 largest integers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
According to Sql Server, Any computed column can be indexed only when it is deterministic.
ie., consider columns a,b, and c are of INT datatype and c = a+b. Now column C can be indexed but **when column a or b holds the largest Integer value it will throw the Arithmetic Error, is there any solution ?
Thanks.
create table TA (
ID int not null primary key,
a int not null,
b int not null,
c as a+b
)
go
create index IX_TA_c on TA (c)
go
insert into TA(ID,a,b) values (1,1,2147483647)
The insert gets:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
If you want the calculation to always work, change the calculation so that at least one of the columns is forced to be a bigint (and so the maths and the resultant type of c are all bigint also):
c as CAST(a as bigint)+b

Resources