473,799 Members | 3,052 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_beginnin g 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 4045
This is what I got:

void searchindex(nod e*& 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(nod e*& 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(nod e*& 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.programmin g 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(nod e*& 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(nod e*& 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(nod e*& 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
4205
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 ---------------------------------------------------------- #include <stdio.h>
4
1941
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 the Textboxes elsewhere once they have been created. The TextBoxes are being named based on a private global counter (i.e. TextBox tb = new TextBox(); tb.Name = "tb" + (counter++).ToString();) They are being created in a private method called...
0
2997
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 implement those buttons, but I'm not sure what the code should look like. I tried what I thought was obvious code: myCollection.AddNew() and
8
10299
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
2470
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 itm as listviewitem For each itm in lvw.Items 'if certain condition is met then insert new row in the listview here
6
1881
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: It is five past seven. It is eleven to ten. It is half past nine. It is a quarter past eight. It is a quarter to ten.
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 better than O(n). IOW, the PEP should somehow specify which operations are efficient, and which ones aren't. Regards,
60
2726
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 of these values and then average them in another column.
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.