Simple linked list in C

0 votes

I am about to create a linked that can insert and display until now:

struct Node {
    int x;
    Node *next;
};

This is my initialization function which only will be called for the first Node:

void initNode(struct Node *head, int n){
    head->x = n;
    head->next = NULL;
}

To add the Node, and I think the reason why my linked list isn't working correct is in this function:

void addNode(struct Node *head, int n){
    struct Node *NewNode = new Node;
    NewNode-> x = n;
    NewNode -> next = head;
    head = NewNode;
}
Jun 2, 2022 in C++ by Nicholas
• 7,760 points
611 views

1 answer to this question.

0 votes

This is the most basic example I can think of in this situation, and it has not been tested. 

Please keep in mind that this violates some C++ best practises and deviates from the norm (initialize lists, separation of declaration and definition, and so on). 

But those aren't topics I can discuss here.

#include <iostream>
using namespace std;

class LinkedList{
    // Struct inside the class LinkedList
    // This is one node which is not needed by the caller. It is just
    // for internal work.
    struct Node {
        int x;
        Node *next;
    };

// public member
public:
    // constructor
    LinkedList(){
        head = NULL; // set head to NULL
    }

    // destructor
    ~LinkedList(){
        Node *next = head;
        
        while(next) {              // iterate over all elements
            Node *deleteMe = next;
            next = next->next;     // save pointer to the next element
            delete deleteMe;       // delete the current entry
        }
    }
    
    // This prepends a new value at the beginning of the list
    void addValue(int val){
        Node *n = new Node();   // create new Node
        n->x = val;             // set value
        n->next = head;         // make the node point to the next node.
                                //  If the list is empty, this is NULL, so the end of the list --> OK
        head = n;               // last but not least, make the head point at the new node.
    }

    // returns the first element in the list and deletes the Node.
    // caution, no error-checking here!
    int popValue(){
        Node *n = head;
        int ret = n->x;

        head = head->next;
        delete n;
        return ret;
    }

// private member
private:
    Node *head; // this is the private member variable. It is just a pointer to the first Node
};

int main() {
    LinkedList list;

    list.addValue(5);
    list.addValue(10);
    list.addValue(20);

    cout << list.popValue() << endl;
    cout << list.popValue() << endl;
    cout << list.popValue() << endl;
    // because there is no error checking in popValue(), the following
    // is undefined behavior. Probably the program will crash, because
    // there are no more values in the list.
    // cout << list.popValue() << endl;
    return 0;
}
I strongly advise you to learn more about C++ and object-oriented programming.
answered Jun 2, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Simple example of threading in C++

Could someone maybe provide a basic C++ ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
319 views
0 votes
0 answers

Simple dictionary in C++

Changing some Python code to C++. BASEPAIRS = { "T": "A", "A": "T", ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
411 views
0 votes
0 answers

What is the difference between std::list<std::pair> and std::map in C++ STL?

What distinguishes std::list<std::pair> from std::map? Does the ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
280 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
613 views
0 votes
1 answer

C++ "Object" class

No, there is no generic base class&nb ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
316 views
0 votes
1 answer

Create a reverse LinkedList in C++ from a given LinkedList

A simpler solution is to just let the current node point to the previous node while going through your linked list, saving the previous and next nodes as you go. void LinkedList::reversedLinkedList() { if(head == ...READ MORE

answered Aug 5, 2022 in C++ by Damon
• 4,960 points
527 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,090 points
491 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,090 points
2,135 views
0 votes
1 answer

insert method for doubly linked list C++

I attempted to repair all of your methods, and I believe I succeeded; at least, the current test example prints the proper answer: #include <iostream> #include <vector> using namespace std; struct Node { ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
637 views
0 votes
1 answer

Declare abstract class in c++

An abstract class is one that is intended to be used as a base class .  At least one pure virtual function exists in an abstract class.  A pure virtual function is declared in the class declaration by using a pure specifier (= 0) in the declaration of a virtual member function. Here is an example of an abstract class: class AB { public: virtual void f() ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
383 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP