Thread: Need general advice on implementing C program (linked list) into C++ class

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    36

    Need general advice on implementing C program (linked list) into C++ class

    In another thread on the C board I posted this abbreviated program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct nodeStruct {
    	int data;
    	struct nodeStruct* next;
    	struct nodeStruct* previous;
    } node;
    
    node* initList(int listLength) {}
    		
    int getLength(node* head) {}
    
    node* sortList(node* head) {}
    
    void printList(node* head) {}
    
    int main() {}
    I am now being asked to turn it into a C++ file, with one .cpp for class definition and one for testing it. However, I'm stumped as to whether my class should define a "node" or a "list."

    If I only make a class for the list, then I am still stuck with the "typedef struct" block for the node, and (a) I'm not sure where to put that block and (b) I am not sure if typedef belongs in a C++ code since class seems better.

    If I only make a class for the node, then I'm performing "list methods" (print list, sort list, etc) on a node object, which doesn't seem to make sense logically.

    If I make both classes, it gets pretty complex (for me, first ever C++ program), and I'm ending up with more files than asked for in the requirement.

    Any advice is appreciated. I've spent a couple hours already trying to code it in multiple different ways, none of which have worked. At the very least, I need to decide WHICH path to go down and even then I will still have a lot of problems to sort out, but at least I'll be on the right track.

    Thanks.

  2. #2
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    (a) I'm not sure where to put that block
    How about nested in the list class?
    Code:
    class list
    {
        struct node
        {
            int data;
    	node* next;
    	node* previous;
        };
    
        ...
    };
    (b) I am not sure if typedef belongs in a C++ code since class seems better.
    C++ typedefs class and struct names for you.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    36
    Quote Originally Posted by Inanna View Post
    How about nested in the list class?
    Code:
    class list
    {
        struct node
        {
            int data;
    	node* next;
    	node* previous;
        };
    
        ...
    };
    C++ typedefs class and struct names for you.
    Thanks for the tip, that worked! I got thrown into C++ way too quickly and didn't even know that was an option. I've done nested classes in Java before though. C++ is weird though, since you have classes AND structs (which seem like a "weak" version of a class to me). Nothing in Java I can think of that compares to a struct.

  4. #4
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    C++ is weird though, since you have classes AND structs (which seem like a "weak" version of a class to me).
    If it helps, structs and classes are the same except for default access. Structs are public by default and classes are private, but anything you can do with classes you can also do with structs.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    36
    Quote Originally Posted by Inanna View Post
    If it helps, structs and classes are the same except for default access. Structs are public by default and classes are private, but anything you can do with classes you can also do with structs.
    Yes that does help a bit, thanks. When I have more time I'll go through a comprehensive introduction to C/C++ from a good book or something. I was just in a hard place being thrown this assignment with a short deadline and no real introduction to the language. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-15-2011, 10:36 AM
  2. General Advice
    By ph5 in forum C Programming
    Replies: 3
    Last Post: 02-23-2011, 04:16 AM
  3. Implementing a linked list, some problems
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2003, 09:07 AM
  4. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM
  5. implementing graph using linked list
    By Tim in forum C Programming
    Replies: 1
    Last Post: 11-20-2001, 02:00 AM