Inserting node at the beginning in linked list

Hye, everyone! Is this the right way to add a node in the beginning? The code is running fine! Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include <iostream>
using namespace std;
struct abc
{
	int data;
	abc *next;
};
abc *head = NULL;
void insertbeg(int d)
{
	abc *ptr = new abc;
	ptr->data = d;
	if (head == NULL) //  no nood initially ! // pehli dafa to yai loop chalay gi lazmi! cuz HEAD MEIN PEHLI DAFA TO THA HI NULL! 
	{
		head = ptr; // naye node ka address head mein save!
	}
	else
	{
	ptr->next = head; // ptr pointing to the initial node
	head = ptr;// head pointing to ptr
	}
}
void showdata()
{
	abc *temp = head;
	while (temp != NULL)
	{
	  cout << temp->data;
	  temp = temp->next;
	}
	
}
void makingnodes()
{
	abc *node = new abc; //aik dynamic structure banaya hai
	cout << "Enter data" << endl;
	cin >> node->data;
	node->next = NULL;
	if (head == NULL) //  no nood initially ! // pehli dafa to yai loop chalay gi lazmi! cuz HEAD MEIN PEHLI DAFA TO THA HI NULL! 
	{
		head = node; // naye node ka address head mein save!
	}
	else
	{
		abc*temp = head; // temp pointer ko head ka address day dia shuro mein dono 
		while (temp->next != NULL) // temp ko chalao last tak..agay aur koi node nahi
		{
			temp = temp->next;
		}
		temp->next = node; // ab temp next yani...last node k address walay slot par node ka address copy..LINKED!
						   // nabeel did temp->next = &node; cuz object ka address pointer ko!
	}
}
int main()
{ 
	int y;
	cout << "How many nodes you want to enter" << endl;
	cin >> y;
	for (int i = 0; i < y;i++)
	{
		makingnodes();
	}
    int x;
    cout <<"Enter the data of the new node:" <<endl;
	cin >> x;
	insertbeg(x);
	cout <<"Final nodes data are : " <<endl;
	showdata() ;
	cout <<endl;
	system("pause");

}
Last edited on
Your code works fine, but it forces you to use a global variable and to remember to set every new ‘node’ instances to default values.
I'd add at least a basic constructor and I'd try to avoid the global variables passing my singly linked list to the functions by reference.
I’d also tried to be more consistent; I mean, if the function ‘makingnodes()' is in charge of asking the values to the user, why the ‘insertbeg()' one is not?

Basic example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>


struct abc
{
    int data;
    abc *next;

    abc();
};


abc::abc()
    : data {}
    , next { nullptr }
{
}


abc* makingnodes(abc*& head);    // push_back()?
abc* insertbeg(abc*& head);      // push_front()?
void showdata(const abc* const head);


int main()
{ 
    std::cout << "How many nodes you want to enter? ";
    int val;
    std::cin >> val;

    abc *head { nullptr };

    for (int i {}; i < val; ++i)
    {
        abc* node { makingnodes(head) };
        if (nullptr == node) {
            std::cout << "Something went amiss...\nExiting now.\n";
            return -1;
        }
    }

    abc* node { insertbeg(head) };
    if (nullptr == node) {
        std::cout << "Something went amiss...\nExiting now.\n";
        return -1;
    }

    std::cout << "Final nodes data are:\n";
    showdata(head);
    std::cout << '\n';

    // Clean-up missing!!!
}


// Add nodes at back
abc* makingnodes(abc*& head)
{
    std::cout << "Enter data (single integer): ";
    abc* node = new abc;
    std::cin >> node->data;

    if (head == nullptr)
    {
        head = node;
    }
    else
    {
        abc* temp { head };
        while( temp->next != nullptr )
        {
            temp = temp->next;
        }
        temp->next = node;
    }
    return node;
}


// Add nodes at front
abc* insertbeg(abc*& head)
{
    std::cout << "Enter the data of the new node: ";
    abc *ptr = new abc;
    std::cin >> ptr->data;

    if (head == nullptr)
    {
        head = ptr;
    }
    else
    {
        ptr->next = head;
        head = ptr;
    }

    return ptr;
}


void showdata(const abc* const head)
{
    for (const abc *temp = head; temp != nullptr; temp = temp->next)
    {
        std::cout << temp->data;
    }
}

Is this the right way to add a node in the beginning?
No. When head==NULL, you leave node->next uninitialized.

When inserting at the beginning, the value of head doesn't matter:
1
2
3
4
5
6
7
8
9
10
// Add nodes at front
abc* insertbeg(abc*& head)
{
    std::cout << "Enter the data of the new node: ";
    abc *ptr = new abc;
    std::cin >> ptr->data;
    ptr->next = head;
    head = ptr;
    return ptr;
}


By the way, you should never insert at the end of a linked list unless you keep track of a tail pointer. It's just too inefficient.
Topic archived. No new replies allowed.