Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,391 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,149 people online right now. Registration is fast and FREE... Join Now!




Add nodes to front of a Link list

 
Reply to this topicStart new topic

Add nodes to front of a Link list

eudos
4 Sep, 2006 - 01:25 AM
Post #1

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
Hi..

I am tryin to add nodes to a FRONT of a link list (struct) which contains a number in each..I need to it recursively..

Problem with the code is first pointer is a NULL pointer.. so after creating a new node i am able to add the value to the first input..But when the second input is entered and if i do EOF program crash..
CODE

List addFront(List list, int value) {

     List element, temp;


    if (list == NULL) {
       element = newElement();
      
       setElement(element, value);

       element->next = temp;
    } else {

        list->next = addEndIterative(list->next, value);
    }

    temp = list;
    return(element);
}



How can i save the first pointer when the function is called..coz i think i need to equal "element->next = temp;"

Can anyone plz help me with this..Ask if u can't understand the above.. smile.gif
User is offlineProfile CardPM
+Quote Post

eudos
RE: Add Nodes To Front Of A Link List
4 Sep, 2006 - 02:37 AM
Post #2

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
Sorry for a bad mistake in the above code.. mistakes means an obvious one..but the real problem is still there..Since the function is recursive plz replace the code
CODE

list->next = addEndIterative(list->next, value);



with this line...
CODE


list->next = addFront(list->next, value);


User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Add Nodes To Front Of A Link List
4 Sep, 2006 - 04:42 AM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
What I don't understand is why you need a recursive function to add a node.
All you have to do is this:
  • Create a new node.
  • node->value = the_value_you_want_to_set;
  • node->next = element;
  • element = node;
Here, I've assumed the element pointer to point to the first node in the Link List.
For Adding a node in front of a link list, we create a new node and set its next pointer to the current first node (which is element in this case).
Once that is done, you set the content of element pointer to node so that it continues to point to the first node in the Link List.

Why use recursion?
User is offlineProfile CardPM
+Quote Post

eudos
RE: Add Nodes To Front Of A Link List
4 Sep, 2006 - 05:10 AM
Post #4

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
I am actually writing code for my tutorial class. thats what tutor has asked..hehe..when comes to recursion i get confuse a little..

I am able to add new nodes to END of the list recursively..
CODE

Sequence addEnd(Sequence sequence, Item item) {
    if (sequence == NULL) { // Empty sequence
        sequence = item;
    } else {
        sequence->next = addEnd(sequence->next, item);
    }

    return(sequence);
}


This works fine..When it comes to add to FRONT using recursion program crash.. wihout recursion it works.. smile.gif

Hope u understood the reason of using recursion..
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:33AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month