473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SImple list implementation

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Could anybody tell me if this code is right?

Thanks.
struct lista_alumnos
{

~ struct alumno datos;

~ struct lista_alumnos *siguiente;

};
void InsertaAlumno (struct lista_alumnos &alumnos,
~ const struct alumno &nuevo_alumno)
{

~ lista_alumnos *nuevo_nodo, *iterador;

~ nuevo_nodo = new struct lista_alumnos;

~ nuevo_nodo->datos = nuevo_alumno;
~ for( iterador = alumnos;
~ iterador != NULL;
~ iterador = iterador->siguiente );

~ iterador = nuevo_nodo;

}

- --
F. J. Yáñez (ckroom)

http://nazaries.net/~ckroom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBL0WBDhQpyJGbSuMRAtD7AJ9MHJdAjcfbKLvpGQ/I2A5/mUWWRACfX5iV
aFMwQvYvBO4ibq3kvEwAOxk=
=B7hu
-----END PGP SIGNATURE-----
Jul 22 '05 #1
2 1913
ckroom wrote:
Could anybody tell me if this code is right?

Thanks.
Even after you remove superfluous 'struct' occurrences, define 'alumno',
it won't even compile. It contains logical errors, too.


struct lista_alumnos
{

~ struct alumno datos;

~ struct lista_alumnos *siguiente;

};
void InsertaAlumno (struct lista_alumnos &alumnos,
~ const struct alumno &nuevo_alumno)
{

~ lista_alumnos *nuevo_nodo, *iterador;

~ nuevo_nodo = new struct lista_alumnos;
nuevo_nodo = new lista_alumnos(); // parentheses force the members to 0

~ nuevo_nodo->datos = nuevo_alumno;
~ for( iterador = alumnos;
You cannot assign an object to a pointer. To make it compile you need to
do
for (iterador = &alumnos;
~ iterador != NULL;
~ iterador = iterador->siguiente );

~ iterador = nuevo_nodo;
In these two last statements you're scanning for the end of the list,
but you never remember to update the list itself after the loop has
found the end.

alumnos:
/ datos \ .---> / datos \ .---> ... / datos \
\ siguiente /---' \ siguiente /---' \ NULL / -->X

That's the desired structure. Now, imagine that you do have it correctly
laid out in memory. What happens when you search for the last element?
You iterate the list with the only variable 'iterador'. It gets the value
'NULL' at the last update (iterador = iterador->siguiente), and then what?
You make the _local_ variable (iterador) to point to the dynamic object
you just created. But the list (the last element of that list) has not
been updated.

Instead of having 'iterador' remember the 'siguiente' it found, make it
store the _previous_ element. How? Instead of testing the value of the
'iterador' pointer, test 'iterador->siguiente':

for (iterador = alumnos; iterador->siguiente != NULL;
iterador=iterador->siguiente);

Now, when the loop finishes, 'iterador' will point to the last element of
the list (the tail), and you do

iterador->siguiente = nuevo_nodo;

}


Victor
Jul 22 '05 #2

"ckroom" <ck****@hotmail.com> wrote in message
news:2p************@uni-berlin.de...
Could anybody tell me if this code is right?

Thanks.
struct lista_alumnos
{

~ struct alumno datos;
What are the ~ symbols? (Tab characters?)

You don't need the word struct there (or anywhere else, except where you
actually define the struct's).

~ struct lista_alumnos *siguiente;
Since you have dynamically-allocated data in this struct, you're probably
going to need three functions: a constructor (which initializes the
pointer), a destructor (which manages deleting the list pointed to by that
pointer), and an assignment (=) operator (which copies the contents from one
lista_alumnos to another, including allocating and copying the list
contents).
};
void InsertaAlumno (struct lista_alumnos &alumnos,
~ const struct alumno &nuevo_alumno)
{

~ lista_alumnos *nuevo_nodo, *iterador;

~ nuevo_nodo = new struct lista_alumnos;

~ nuevo_nodo->datos = nuevo_alumno;
Ok, you've made a new list structure (node), and copied the given alumno
object to it's alumno member. But you don't show the alumno structure
anywhere. If it has any dynamically-allocated members (pointers), then
you'll need to provide an assignment operator for it. If not, the default
one the member-wise copy that you'll get by default should work ok.


~ for( iterador = alumnos;
~ iterador != NULL;
~ iterador = iterador->siguiente );
Here, you went from the beginning of the given list to the end. Now what?
When this loop exists, your iterator (iterador) is NULL. That's the exit
condition of the loop. I'm guessing what you really want is to exit when
iterador->siguiente is NULL. That way, iterador will point to the last node
in the list. Then, you can assign that node's next (siguente) pointer to
point to the new node you just created, instead of doing whatever it is you
thought the line below was going to do.

(One problem with my suggestion that you have to work out: what if the list
is empty to begin with? You need to handle that case without going into the
loop in the first place. I'll leave that to you to handle.)

~ iterador = nuevo_nodo;
This is just useless. For one thing, terador is a local variable. Setting
it to point to the new node accomplishes nothing at all, because it will go
out of scoe at the end of the function. (See the comments above for what I
think you meant to do.)

}


-Howard
Jul 22 '05 #3

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

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
10
by: JustSomeGuy | last post by:
I have a few classes... class a : std::list<baseclass> { int keya; }; class b : std::list<a> { int keyb;
6
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. ===...
0
by: coosa | last post by:
Dear all; My code is is a bit long but is modular at least. I'm attempting to implement the depth first search for an application that is supposed to: 1- Fetch based on an ID from the database a...
4
by: theronnightstar | last post by:
I am having a problem with a small bit of code. I can't figure out why it won't work. Here: //code: #include <iostream> #include <fstream> #include <ctype.h> #include <string>
8
by: obrianpatrick | last post by:
Hi, I am relatively new to object oriented programming and design. I am developing an application in VS 2005. I am having the following design problem: I have two interfaces X and Y. Y is...
14
by: Wildemar Wildenburger | last post by:
Hello folks :) This has got to be an FAQ, but somehow I can't seem to find an answer that I understand ... I thought: I'll just write a decorator that lets me react to method calls easily (the...
17
by: marks542004 | last post by:
Hi all , I am an old programmer now hobbyist who has used cobol, basic, and RPG . I am new to c++ but have Microsoft Visual Studio 6.0. I have a program in Basic that reads a file , looks for...
4
by: Thomas | last post by:
Hello, I am a CS student and I want to write simple lisp interpreter. The code should be entierly in C. I don't want to use any compiler generators like Bison or Yak, since wrinting this in...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.