LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   hexadecimal linked list (https://www.linuxquestions.org/questions/programming-9/hexadecimal-linked-list-4175413090/)

mscoder 06-24-2012 03:21 AM

hexadecimal linked list
 
i came across a problem of adding 2 hexadecimal linked lists but i can't understand how to implement list where both alphabets and integer characters are stored..
help me wid the code of the same pls..

pixellany 06-24-2012 03:29 AM

Please post a sample of what the data looks like, and what you want the output to look like.

When you say "adding two lists", do you mean that you want to add pairs of hex values, or maybe just combine the lists?

pan64 06-24-2012 04:20 AM

do you have a preferred language (perl, awk, c++ or ?)

mscoder 06-24-2012 07:24 AM

Quote:

Originally Posted by pixellany (Post 4710456)
Please post a sample of what the data looks like, and what you want the output to look like.

When you say "adding two lists", do you mean that you want to add pairs of hex values, or maybe just combine the lists?

sorry for not clearing it in the last post..but by adding i mean adding the two hexadecimal numbers stored in the two lists...the final list shoud contin the addition of two numbers in hexadecimal format.

dwhitney67 06-24-2012 07:39 AM

Quote:

Originally Posted by mscoder (Post 4710567)
sorry for not clearing it in the last post..but by adding i mean adding the two hexadecimal numbers stored in the two lists...the final list shoud contin the addition of two numbers in hexadecimal format.

Still not clear is what programming language you are using.

However, as far as the storage of the hex data, store it as a string of character(s). All symbols are characters, unless you opt to treat them otherwise. So "8F" is composed of the characters '8' and 'F' (and the terminating NULL character).

To perform addition of the hex data, you will need to convert the string to a number; for example, for "8F", you would convert the '8' to the number 8 and multiply by 16; for the 'F', convert this to 15 and add to the previous multiplication result. Repeat for the other value, then perform the addition of the two values. When done, reconvert to the result to a hex string representation.

In summary (pseudocode):
Code:

hexStrResult = ConvertToHexString( ConvertToNumber(hexStr1) + ConvertToNumber(hexStr2) )

P.S. Conversely, you can store all data as numbers, and only display the data in hex when requested to print the values of the linked list. This might be the easier approach.

graemef 06-24-2012 07:49 AM

So if I understand correctly the problem boils down to adding two numbers which are held as a string and in hexadecimal representation.

So you may need a loop (to step through each character in the string, starting with from the right, the least significant digit) {If the point of the list is to hold a single character then this is not required}
Take each character and any carry digit and add the three together
Convert each digit to a number
If the three values are greater than 16 the subtract 16 and add a carry digit
Convert the total back to a character

The functions that will convert back and forth from number to character can be simple switch statement

There is the basic design for you. Now implement in your selected language, test and find any errors or omissions. ;)


All times are GMT -5. The time now is 01:40 AM.