next pointer in linked list

This is a discussion on next pointer in linked list within the C++ Programming forums, part of the General Programming Boards category; Here is my code for a linked list. LinkedList.h Code: #ifndef LINKEDLIST_H #define LINKEDLIST_H class LinkedList { public: LinkedList(); ~LinkedList(); ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    118

    next pointer in linked list

    Here is my code for a linked list.

    LinkedList.h
    Code:
    #ifndef LINKEDLIST_H
    #define LINKEDLIST_H
    
    class LinkedList
    {
        public:
            LinkedList();
            ~LinkedList();
            void createNode(char name[]);//creates a node at end of list
            void dump();//prints contents of all nodes
    
        private:
            unsigned int length;
            struct Node
            {
                char name[20];
                Node *next;
            };
            Node *startPointer;
    
    };
    
    #endif // LINKEDLIST_H
    LinkedList.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    #include "..\include\LinkedList.h"
    
    LinkedList::LinkedList() : length(0), startPointer(NULL)
    {
    
    }
    
    
    LinkedList::~LinkedList()
    {
    
    }
    
    void LinkedList::createNode(char name[])
    {
        if(startPointer == NULL)
        {
            startPointer = new Node;//Node is a struct
            (*startPointer).next = NULL;
            strcpy((*startPointer).name, name);
          
        }
        else
        {
            Node *temp = startPointer;
            while((*temp).next != NULL)
                temp = (*temp).next;
            (*temp).next = new Node;
            temp = (*temp).next;
            strcpy((*temp).name, name);
            (*temp).next = NULL;
        }
    }
    
    void LinkedList::dump()
    {
        Node *temp = startPointer;
        std::cout << (*temp).name << std::endl;
        while((*temp).next != NULL)
        {
            temp = (*temp).next;
            std::cout << (*temp).name << std::endl;
        }
    }
    In a previous thread it was mentioned that I could set the next pointer to null using the constructor of the struct. I found an example from this website

    Code:
    struct node
    {
        node(int data_p) { data=data_p; next=0; }
        int data;
        node *next;
    };
    In my header file why can't I do this?
    Code:
    struct Node
            {
                char name[20];
                Node *next = null;
            };
    What's so special about initializing the variables the first way?

    What's the best way of doing this?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    You need to do it in a constructor.

    Code:
    struct Node
    {
        Node()
        {
             next = NULL;
        }
        char name[20];
        Node *next = null;
    };
    Also do you hate the -> operator or just unaware of its existence?
    Code:
    Node *temp = startPointer;
    while(temp->next != NULL)
    Woop?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    118
    I see. I was confused because I didn't realize that was a constructor and I thought struct was calling itself before it was fully defined.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    118
    I'm not using the -> operator because I'm still getting use to pointers. Using (*a).b reminds me what's actually happening. It also makes me appreciate how much easier it is to use ->

    Another thing that's bothering me is that I don't need to include libraries for making the c-string or using strcpy. According to www.cplusplus.com/reference/clibrary/cstring/ #include <string.h> is needed. According to C Strings - C Tutorial - Cprogramming.com #include <cstring> is needed.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    <string.h> is the C header for c style string functions

    <cstring> is the C++ header for c style string functions.

    Not sure why you would want to use c-strings besides learning purposes.
    Woop?

  6. #6
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    118
    Quote Originally Posted by prog-bman View Post
    Not sure why you would want to use c-strings besides learning purposes.
    I am doing this just to learn. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Linked list probs
    By mouse163 in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 04:41 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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