473,503 Members | 12,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding index counter to linked list

I want to implement an index/counter to my linked list.
Why? I wrote a search function which searches the list for elements
based on their values (say I add an element and I assign the value 54
and the next element 789 and so on...) now I'd like to write a search
function which would search the list for elements based on their index.

Say first element is 54 and index 1, second element is 789 and index 2
so when I want to search, I'll do Search index: 2 and it will return
789.

How should I do that? I was thinking of making a global int counter = 0
and then in the add_to_beginning function I would do counter++ and the
same with other functions but that would only increment the counter, it
wouldn't index my elements.

Please, no pre-made libraries or OOP.I'm doing this with structures,
then I'll move to OOP.

Apr 13 '06 #1
9 4032
This is what I got:

void searchindex(node*& p_beginning, int index)
{
int counter;
node* p_temp = p_beginning;

while (p_temp != 0)
{
if (p_temp->element == index)
{
cout << counter; or return counter; (int instead od void)
}
else
{
p_temp = p_temp->p_next;
counter++;
}
}
}

When I search it returns nothing and gets back to the menu.

Apr 13 '06 #2
Daniel Vukadinovic wrote:
This is what I got:

void searchindex(node*& p_beginning, int index)
{
int counter;
Note that 'counter' is uninitialised. At best it contains some valid
value, but not what you think. At worst, the value it contains is
a trap value which can cause your computer to blow up. In any case,
the behaviour of your program is undefined if you use that value.
node* p_temp = p_beginning;

while (p_temp != 0)
{
if (p_temp->element == index)
{
cout << counter; or return counter; (int instead od void)
And here you try to return a value of an uninitialised variable.
}
else
{
p_temp = p_temp->p_next;
counter++;
And here you're incrementing an unintialised variable.
}
}
}

When I search it returns nothing and gets back to the menu.


No surprise.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '06 #3
I did counter = 0; but I still don't get anything.
Am I doing this the right way? I mean, the whole indexing thing.

Apr 13 '06 #4
Your first post:
I want to implement an index/counter to my linked list.
Why? I wrote a search function which searches the list for elements
based on their values (say I add an element and I assign the value 54
and the next element 789 and so on...) now I'd like to write a search
function which would search the list for elements based on their index.

Say first element is 54 and index 1, second element is 789 and index 2
so when I want to search, I'll do Search index: 2 and it will return
789.
Daniel Vukadinovic <da****************@gmail.com> wrote: This is what I got:

void searchindex(node*& p_beginning, int index)
{
int counter;
node* p_temp = p_beginning;

while (p_temp != 0)
{
if (p_temp->element == index)
Based on your first post, why are you looking at p_temp->element? In
your example, if index == 2, then element == 789. These are not equal,
so you continue looping until you hit the end of the list, or until
coincidentally the index and the element are the same.
{
cout << counter; or return counter; (int instead od void)
}
else
{
p_temp = p_temp->p_next;
counter++;
}
}
}

When I search it returns nothing and gets back to the menu.


--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 13 '06 #5
Daniel Vukadinovic wrote:
I want to implement an index/counter to my linked list.
Why? I wrote a search function which searches the list for elements
based on their values (say I add an element and I assign the value 54
and the next element 789 and so on...) now I'd like to write a search
function which would search the list for elements based on their index.


Have you considered implementing this a bit differently?

template<class Value>
struct Node
{
Value value;
int next;
};

then allocate an array of these guys, and use indexes as your links
rather than pointers to nodes.

so your array might look like:

begin = 2
array[0] =: Node<int> { 10000, 3 }
array[1] =: Node<int> { 10001, -1 }
array[2] =: Node<int> { 10002, 0 }
array[3] =: Node<int> { 10003, 1 }

and now you have your O(1) random access along with the O(N) seqeuntial
lookup. this is of course extendible to any number of O(N) indexing
schemes, but may not be useful depending on your insert/delete requirements.

you might also try comp.programming as this is clearly not just a c++
problem as my crappy little psuedo code demonstrates :-\
Apr 13 '06 #6
Daniel Vukadinovic wrote:
This is what I got:

void searchindex(node*& p_beginning, int index)
{
int counter;
node* p_temp = p_beginning;

while (p_temp != 0)
{
if (p_temp->element == index)
{
cout << counter; or return counter; (int instead od void)
}
else
{
p_temp = p_temp->p_next;
counter++;
}
}
}

When I search it returns nothing and gets back to the menu.


I am guessing here but maybe the below is what you want i.e. finding
the index-th element:

void searchindex(node*& p_beginning, int index)
{
int counter = 0;
node* p_temp = p_beginning;

while (p_temp != 0)
{
if (counter == index)
{
cout << p_temp->element; // or return p_temp->element;
}
else
{
p_temp = p_temp->p_next;
counter++;
}
}
}

Apr 13 '06 #7
Markus Schoder wrote:
Daniel Vukadinovic wrote:
I am guessing here but maybe the below is what you want i.e. finding
the index-th element:

void searchindex(node*& p_beginning, int index)
{
int counter = 0;
node* p_temp = p_beginning;

while (p_temp != 0)
{
if (counter == index)
{
cout << p_temp->element; // or return p_temp->element;
}
else
{
p_temp = p_temp->p_next;
counter++;
}
}
}


Need to add a return after the cout to avoid an infinite loop.

Apr 13 '06 #8
pillbug: That's too complicated.
Markus: That's what I did after I modified the code and as a result,
when search, my screen gets flooded with numbers.

Apr 13 '06 #9
Yes, return helps.Thanks!
Thank you all for helping me!

Apr 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4185
by: PRadyut | last post by:
In this code i tried to add the elements in ascending order but the output is only 0 1 2 the rest of the elements are not shown. the code...
4
1926
by: slylos | last post by:
I've got a Windows form that has a TabControl. I add TabPages to this TabControl at Runtime, along with two Textboxes and a button. The problem I'm having is I can't figure out how to reference...
0
2985
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
8
10273
by: Sam | last post by:
Hi, Here is my code : For Each row As DataRow In ds.Tables(0).Rows next How can I get the index of the row being processed ? Thx
2
2460
by: dotnetnewbie | last post by:
Whilst looping through items in a listview I wish to have the option of inserting a new row (in the middle not necessarily at the end of the listview). thus if lvw is the listview name dim...
6
1859
by: mattmao | last post by:
Hi all. There is a challenge question I encountered recently, which says: "In plain English, there are six different ways when you want to tell someone else about the current time: ...
9
175
by: =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= | last post by:
``odict.byindex(index)`` For this API, I think it's important to make some performance guarantees. It seems fairly difficult to make byindex O(1), and simultaneously also make insertion/deletion...
60
2626
by: Bill Cunningham | last post by:
I have a row of values like such, placed in a text file by fprintf. 10.50 10.25 10.00 10.75 11.00 What I want to do to the above colum is add a new column right beside it which is a total...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.