AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   "Linked list" (https://forums.alliedmods.net/showthread.php?t=232675)

KissLick 01-03-2014 06:27

"Linked list"
 
Ciao,

what do you think about "linked list" made via dynamic array of datapacks (every cell of array contains handle to datapack)?

Is that a good idea? I know about array of enums, but that is not dynamic..

And if I make some stocks for that solution it would be an awesome thing I guess.

xf117 01-03-2014 07:20

Re: "Linked list"
 
Probably, there is a better way to do what you want to do.
But you can use adt array for that. Or even adt stack or global databack whichever fits you better.

KissLick 01-03-2014 09:29

Re: "Linked list"
 
I meant somthing like this
PHP Code:

new Handle:gh_LinkedList INVALID_HANDLE// global variable

// Create dyn. array
gh_LinkedList CreateArray();

.
.
.

// Create and push datapacks
PushListValuesgh_LinkedList"ciao"147.35 ); // index 0
PushListValuesgh_LinkedList"hallo"3584.4 ); // index 1

.
.
.

// Get values
new String:name64 ];
new 
age;
new 
Float:height;
GetListValuesgh_LinkedList0nameageheight ); // get values from datapack on index 0
GetListValuesgh_LinkedList1nameageheight ); // get values from datapack on index 1 

and stocks
PHP Code:

// Stock for pushing datapacks
stock PushListValuesHandle:list, const String:arg164 ], arg2Float:arg3 )
{
    new 
Handle:pack CreateDataPack();
    
WritePackStringpackarg1 );
    
WritePackCellpackarg2 );
    
WritePackFloatpackarg3 );
    
    return 
PushArrayCell( list, _:pack );
}

// Stock for getting datapack values
stock GetListValuesHandle:list, index, const String:value164 ], value2Float:value3 )
{
    new 
Handle:pack Handle:GetArrayCell( list, index );
    
    
ResetPackpack );
    
ReadPackStringpackvalue164 );
    
value2 ReadPackCellpack );
    
value3 ReadPackFloatpack );



friagram 01-03-2014 18:19

Re: "Linked list"
 
Linklists are cancer
Iono why they even teach them in programming classes.
Just like data structures theory.

Unless it is for a very specific application that requires re-use of the handles for convenient storage and lookup, it's a complete waste, which almost always is the case.
They are also really hard to iterate through.

Mathias. 01-04-2014 05:14

Re: "Linked list"
 
Have you ever though about array of enum of adt array :3

A similar solution (that i've just posted for someone else) may interest you:

https://forums.alliedmods.net/showpo...7&postcount=17
https://forums.alliedmods.net/showpo...9&postcount=20

Zephyrus 01-04-2014 06:37

Re: "Linked list"
 
Quote:

Originally Posted by friagram (Post 2080724)
Linklists are cancer
Iono why they even teach them in programming classes.
Just like data structures theory.

Unless it is for a very specific application that requires re-use of the handles for convenient storage and lookup, it's a complete waste, which almost always is the case.
They are also really hard to iterate through.

what the.... linked lists are wildly used everywhere. in windows kernel too. its the best way to store dynamically allocated variables... linked lists are far superior than reallocating a huge array just to add a single element. also, iterating through a linked list is jsut a few lines:

Code:

LinkedList * element = my_linked_list;
while(element)
{
  ...
  element= element->next;
}

also, what do you mean by convenient lookups? o.O linked lists are not key-value stores... i think you are talking about trees... but then again, tries and binary search trees are efficient, fast and used by every single database server

napalm00 01-04-2014 18:00

Re: "Linked list"
 
In addition to what Zephyrus said, depending on the implementation linked lists can be iterated in both directions (right->left or left->right).

friagram 01-04-2014 19:05

Re: "Linked list"
 
Perhaps sl, but linked lists in c are much different than sourcepawn implementations...

Powerlord 01-04-2014 20:32

Re: "Linked list"
 
Quote:

Originally Posted by napalm00 (Post 2081182)
In addition to what Zephyrus said, depending on the implementation linked lists can be iterated in both directions (right->left or left->right).

That would be the difference between a Linked List and a Doubly Linked List.

Linked lists have their uses, but ease of lookups isn't one of them... from any position in a linked list, you only know what the next element is (and previous element for doubly linked lists). In other words, you're not likely to run into too many cases where they're actually useful for SourcePawn.

KissLick 01-05-2014 17:20

Re: "Linked list"
 
Thx guys for your opinions! You helped me ;-)


All times are GMT -4. The time now is 16:40.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.