Like Tree6Likes

singly linked list

This is a discussion on singly linked list within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> struct node { int x; struct node *next; }; int main(void ) { struct node ...

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    singly linked list

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
    int x;
    struct node *next;
    };
    
    int main(void ) {
    struct node *root;
    struct node *conductor;
    
    root = malloc( sizeof(struct node) );
    root->next = NULL;
    root->x=10;
    
    conductor = root;
    
    if(conductor != NULL) { /*At this point conductor does not = NULL*/
    while(conductor->next != NULL) { /*This line confuses me. conductor->next does = NULL. Doesn't it? I didnt initialize the pointer inside of the global struct to NULL but did set a dummy node to equal NULL with the struct conductor right? Or am I just confused?*/
    ........conductor = conductor->next; /*What exactly does this do? I want to say it traverses down the list. But am not sure.*/
    ........}
    }
    
    conductor->next = malloc( sizeof(struct node) );
    conductor = conductor->next;
    
    if(conductor = NULL) {
    fprintf(stderr, "Out of memory.\n");
    
    else{
    conductor->next = NULL;
    conductor->x = 20;
    }
    
    }
    Anything else you feel like adding....

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It moves through the linked list. Let's say you have already set up 3 nodes.
    Code:
    root->next = node1, node1->next = node2;
    So let's see what it does:
    Code:
    conductor = root;
    if( conductor != NULL ) /* it's not... */
        while( conductor->next != NULL ) /* it's not, conductor->next = node1, because root->next = node1, and conductor = root */
            conductor = conductor->next; /* in makes conductor point node1 (the first time through) */
    That loops until conductor points at node2, because at that point node2->next is NULL. So this loop finds the last node in the list.
    Code:
    if(conductor = NULL) {
    That's wrong though. = assigns, == compares.


    Quzah.
    beatbox32 likes this.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    That's what I thought; It cycles thru the list until it reaches the last node. And I have to set that last node or the dummy node to null so it stops. Otherwise it might point to a random memory address. I just wasn't sure because I only had one node setup. But I guess the way that worked was if it did reach the last node it added one on the end. As for the equals operator....I program on my iPhone and it's so easy to miss something that small! But thank you Quzah

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by Annonymous View Post
    I program on my iPhone
    Why do that to yourself?
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Why do that to myself, you ask. Well, I don't have a computer or a laptop. So I ported a C compiler to my iPhone 4. The small screen is a pain in the ass but it's better then nothing!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Annonymous View Post
    Why do that to myself, you ask. Well, I don't have a computer or a laptop. So I ported a C compiler to my iPhone 4. The small screen is a pain in the ass but it's better then nothing!
    Good lord man... for 4 months fee for that stupid phone you could buy a brand new laptop!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Good lord man... for 4 months fee for that stupid phone you could buy a brand new laptop!
    Yeah but that would be 4 uncool months!


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Yeah but that would be 4 uncool months!
    Quzah.
    Never owned a cellphone... never even talked on one... Won't allow them in my house.
    When people come over, the phone gets turned off and sits in a basket near the front door.
    anduril462 likes this.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    Never owned a cellphone... never even talked on one... Won't allow them in my house.
    When people come over, the phone gets turned off and sits in a basket near the front door.
    Do you also insulate your house with tinfoil to make sure the aliens can't "read your thoughts"?
    manasij7479 likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Do you also insulate your house with tinfoil to make sure the aliens can't "read your thoughts"?
    No of course not... our name is not "Hunter"... (Gees, some people!)

    We just took a decision amonst ourselves that we were sick and tired of people coming over to visit, eating my smarter half's excellent food, while spending most of their time with the cursed phone glued to their ears. If they can't be without it for a couple of hours to actually be present when visiting, they aren't the kind of people we want in our home.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    No of course not... our name is not "Hunter"... (Gees, some people!)
    Lol....touche sir.

    Quote Originally Posted by CommonTater View Post
    We just took a decision amonst ourselves that we were sick and tired of people coming over to visit, eating my smarter half's excellent food, while spending most of their time with the cursed phone glued to their ears. If they can't be without it for a couple of hours to actually be present when visiting, they aren't the kind of people we want in our home.
    I do agree, with cell phones came an apparent lack of etiquette that continues to amaze me.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Lol....touche sir.
    I do agree, with cell phones came an apparent lack of etiquette that continues to amaze me.
    Especially when you spend 70 bucks putting on a real nice dinner and half your guests are physically present but not really there. We decided to label it "Cyberfreeloading" and put an end to it. The sole exception being an EMS worker who is sometimes on call and he only gets a pass when he's *actually* on call.

    We also turn off the ringers on our landlines when guests come over...

    Odd thing... without the phones, texts and tweets constantly interrupting us, we've actually managed to have some pretty good conversations!

    Next on the list... Board games!
    Last edited by CommonTater; 09-09-2011 at 10:12 AM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    Do you also insulate your house with tinfoil to make sure the aliens can't "read your thoughts"?
    I would totally build a faraday cage into the walls of my house if I was building a new one. Not even joking.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Quote Originally Posted by quzah View Post
    I would totally build a faraday cage into the walls of my house if I was building a new one. Not even joking.
    I'm with you! When my parents re-roofed recently, they had a radiant barrier put on the sheathing to help insulate against heat. Basically it's a layer of foil on the bottom of the roof. Then the TV antenna, which used to hide in the attic, stopped receiving any signal, and had to be remounted on top of the roof. Faraday cage and thermal insulation all in one! I wonder if you can get that stuff for your walls. But what about the doors and windows?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    I'm with you! When my parents re-roofed recently, they had a radiant barrier put on the sheathing to help insulate against heat. Basically it's a layer of foil on the bottom of the roof. Then the TV antenna, which used to hide in the attic, stopped receiving any signal, and had to be remounted on top of the roof. Faraday cage and thermal insulation all in one! I wonder if you can get that stuff for your walls. But what about the doors and windows?
    Same insulation as the attic under the siding, Metal doors (harder to break in), wire woven copper impregnated glass (also shatter proof), ground straps everywhere all connected to copper grounding rods every 8 feet and driven 10 feet into the ground... (For a practical example, watch "Enemy of the State".)

    "Golllllleee Martha, I feel so much better since those Martian mind rays stopped getting in here...."

    ROFL... It's a paranoid fool's wet dream!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked list
    By aextine in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 02:20 PM
  2. singly linked list
    By right2001 in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 10:21 AM
  3. Singly Linked List
    By devarishi in forum C Programming
    Replies: 4
    Last Post: 12-23-2008, 11:00 PM
  4. need help with singly linked-list
    By vearns in forum C++ Programming
    Replies: 20
    Last Post: 04-09-2008, 09:48 AM
  5. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21