Saturday, May 9, 2009

How do I delete the last node of a linked list in C programming language?

Also, how do I print a linked list in reverse order?

How do I delete the last node of a linked list in C programming language?
Starting from the first node or head node, go through the list until you get to the last node. You will know its the last node, as it will be pointing to null, or the first node, or the head node, depending on how its set up.


Whilst looping to get to the last node, you will have to keep a record of the node before the last (the node before the one you are testing).


Then all you have to do is change the reference / pointer of this second last node to what the last node pointer to, and delete the last node.





To print in reverse will require recursion, as you want to print the last first, and the first last.


You will once again need to loop until you get to the last node.


For every loop that is not the last node, recall this function with the next node.


Once the last node is found, do not call the function again, but instead print out the value of this node.


Then allow the function to end / return.


As this function now prints out the value of the node, allow the program to do so after calling the function.





Eg,


void function(node)


if node is not last


function(next node) // call the function again with the next node


end if


print value


end function
Reply:The first option you have is to implement a doubly linked list, which is a linked list that you can access from either side. This requires a bit more thinking and a bot more code.





If you just want to use the linked list you have, then use recursion.


recurse through the nodes until you reach a null pointer


when you have a null pointer, you know u are at the last node, so return with a different return value. The previous node will see this value, what it needs to do is release the memory from the other node, and then turn its pointer to null, to mark itself as the last node.





Sry I can't have precise C code, it's been a while since i used it.


What does it mean on the council housing waiting list band 2 c?

hi. i have gone on to the council housing waiting list and they have said i have been placed into band 2 c what does this mean. does anyone know how long i am likely to wait. my council uses a bidding system so does it all depend on who is bidding for the property at that time





UK ANSWERS ONLY PLEASE

What does it mean on the council housing waiting list band 2 c?
To be honest all that bidding takes forever and you can be bidding a long time with no success and end up taking any old dive out of desperation. Try a housing association, mines accent and i was waiting a year for my house which is in a fairly decent area, it's not on an estate otherwise i would have got one even quicker! Band c sounds like the lowest band as i expect band a would be the highest but i'm not sure?


How do you make a randomly generated list in C++?

I am trying to make a program that gets words from three list and makes a phrase with them. Such as a noun list an adjetive list and an adverb list, to randomly generate object ideas for a game i am writing. I dont want it to be flashy, i just want something i can go back into and edit the lists whenever i want to add things to it.

How do you make a randomly generated list in C++?
http://members.cox.net/srice1/random/cra...





shows the c++ rand function.
Reply:You can use the rand() function to generate a random number. Note that this generates a double between [0, 1). To turn this into an integer, I normally define a macro:





#define randint(min, max) (((int(rand() * 10e9)) % ((max) - (min) + 1)) + (min))





The first part turns the random fractional double into an integer from [0, 10e9]. The second part turns that integer into a number from [0, max - min]. The third part makes it [min, max].


Anyone know where I can find a list of all C & D Counties in the US?

I'm trying to find a list online of all the smaller C %26amp; D counties across the US and am having some problems. Anyone know where I might have some luck in finding them by chance?

Anyone know where I can find a list of all C %26amp; D Counties in the US?
The census bureau's website-I used it for my regional development class- if you go to their site you can obtain statistical data from census 2000 for every county in the United States- you'll have to select each state first then review the data. I don't have the website memorized any longer, but google census bureau data 2000. Good luck

long stem roses

How much is a 1902 $5 National Bank Note from Sunbury PA worth? What does Don C Kelly list it as?

I have a 1902 $5 National Bank Note from Sunbury Pennsylvania, charter number 1237. I would like to know more about it.

How much is a 1902 $5 National Bank Note from Sunbury PA worth? What does Don C Kelly list it as?
I'm not sure, but I did recently sell a crisp $10 Numidia PA National Bank Note. Can't remember exactly what I got, but I think it was less than $100.
Reply:I kno a dealer who does good evaluation.. Mail me if u are interested...


When do you think is the best time to post a help wanted ad on C.List so that the maximum amount of people see

do you know what day and time most people go on cl and look at jobs? weekends good?

When do you think is the best time to post a help wanted ad on C.List so that the maximum amount of people see
Sunday @ 12:00 a.m. If your ad will be seen by more users if you are the first ad listed.
Reply:IMHO, post on Mondays or Tuesdays. Typically people already working but frustrated with their jobs will start looking. On the other hand, Monday is a busy day so some qualified candidates wont look at it.


How do you initialize a simple linked list in C?

My teacher taught us everything about linked list except how the code looks like. Could someone show me how to initialize a linked list in C?

How do you initialize a simple linked list in C?
first, include list.h





Then create a list with a line like





list%26lt;int%26gt; myList;





where int is the data type and myList is whatever you want to call it.





Then you can use myList to access all the list commands. To go through a list, you need to create an iterator.





list%26lt;int%26gt;::iterator iterator_name;





see the source for more info...
Reply:hello, linked list program is very complicate to write with this. i will give some notes about it and you try in your class. actually the initialization of the linked list means, the first node of the list should be null.


when make enter a new node then the FIRST pointer should point the first node and address of the first node should be null. this is the actual meaning.


to initialize a linked list, make the FIRST pointer as NULL. when you want to add the first node, then create an object for Node structure and assain the new value to the newly created node.


then make the address of the new node as NULL so that it cant see another next node. this means that the first node is you are created. also there is no more nodes in the list.


this is the logic. you try with you lab to solve the problem what your teacher asked. if you want to increase the level of your programming, then you should find the logic. its enough to program well. the program for the logic you shold do it in your own. dont expect to someone to give the exact code to help you. all are ready to give some logic to you to program.


you should be ready to program for the logic. this will help you to start program at the time you see the problem. OK?


__Vasantha kumar
Reply:A linked list is simply a chain of structures which contain a pointer to the next element.


Using malloc function you need to initialize the linked list node


sample


struct node* head = NULL;


head= malloc(sizeof(struct node));





the following link will help you to find more info.


http://cslibrary.stanford.edu/103/