Inserting element at the end of a linked list.

Jul 24, 2018 at 4:49pm
Well,The function InsertAtTheEnd(); is not working as expected. My logic is sound and implementation also pretty accurate,I know i must've done some silly mistake,Would Appreciate if anyone could help.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<iostream>
#include<stdlib.h>
using namespace std;
void InsertAtnThPosition();
void InsertAtTheend();
void InsertAtBeginning();
void Delete();
void Print();
void Exit();
struct Node
{
    int data;
    Node *link;
};
Node *head,*temp2,*temp3,*temp4,*temp5,*temp6;
int main()
{
    head = NULL; // List Is Empty
    int choice;
    char ans = 'y';
    {
        do
    {
        cout << "\n1.Insert At Beginning\n2.Insert at nTH Position\n3.Insert At The End Of The List\n4.Delete An Element\n5.Print The Linked List\n6.Exit\n";
        cin >> choice;
        switch(choice)
        {
            case 1: InsertAtBeginning();
            break;
            case 2: InsertAtnThPosition();
            break;
            case 3: InsertAtTheend();
            break;
            case 4: Delete();
            break;
            case 5: Print();
            break;
            case 6: Exit();
            break;
            default: cout << "\nWRONG CHOICE ENTER AGAIN or PRESS 5 To Exit";
            break;
        }

    }
    while (ans == 'y' || ans == 'Y');
}
    return 0;
}
void InsertAtBeginning()
{
    int x;
    cout << "\nEnter The Data:";
    cin >> x;
    Node *temp = new Node;
    temp -> data = x;
    temp -> link = NULL;
        if (head == NULL)
        {
            head = temp;
        }
        else
            if (head!= NULL)
        {
            temp -> link = head ;
            head =temp;
        }
}
void InsertAtnThPosition()
{
    int num,pos;
    Node *ptr = new Node;
    cout << "\nEnter The Data:";
    cin >> num;
    cout <<"\nEnter The Position where the Value is to be inserted:";
    cin >> pos;
    ptr -> data = num;
    ptr -> link = NULL;
        if (pos == 1)
           {
            ptr -> link = head; // Set the link of the new node to the node where head points
            head = ptr; // store the address of new node to be inserted at 1th position in the head node
            return ;
           }
        else
            temp2 = head;
        for (int i = 0 ; i < pos -2 ; ++i)
        {
            temp2 = temp2 -> link; // temp 2 is already pointing at first node
        }
        ptr -> link = temp2 ->link; /*sets the link of new node the next node */
        temp2 ->link = ptr; /* sets the link previous node to the new node */
        return ;
}
void InsertAtTheend()
{
   temp5 = head;
   int num;
   cout << "\nEnter The Data:";
   cin >> num;
   temp6 = new Node;
   temp6->data = num;
   temp6->link = NULL;
   while(temp5!=NULL)
   {
       temp5=temp5->link;
   }
   temp5->link=temp6;
   return ;
}
void Delete()
{
    int del_pos;
    cout << "\nEnter The Position Of the node you want to delete (Knowing The List Index here,starts from 1) :";
    cin >> del_pos;
    temp3 = head;
    for (int x = 0 ; x < del_pos -2 ; ++x)
    {
        temp3 = temp3 -> link;  // Reaches the n-1th node
    }
    temp4 = temp3 -> link;
    temp3 ->link = temp4 -> link;
    delete temp4 ;
}
void Print()
{
 Node *ptr;
 ptr = head;
 cout << "\nNow The Linked List Is:\n";
    while(ptr!= NULL)
    {
        cout << endl << ptr -> data;
        ptr = ptr -> link;
    }
}

void Exit()
{
    exit(1);
}

}
Last edited on Jul 24, 2018 at 5:05pm
Jul 24, 2018 at 5:10pm
Lines 103-107: the while loop doesn't end until temp5 is exactly null, so at line 107 temp5 is definitely null, making the access illegal.
You need to loop until temp5->link is null.

Also, get rid of line 16. Declare the variables locally in the functions that use them and give them more descriptive names.
Jul 24, 2018 at 5:14pm
1
2
3
4
5
 while(temp5->link!=NULL)
   {
       temp5=temp5->link;
   }
   temp5 -> link = temp6;


This Worked,Thanks Man! And Yeah,You are right,going to rename all the variables right now. Thank You So much.
Jul 24, 2018 at 5:48pm
Remember to check that temp5 is not null before entering the loop!
Last edited on Jul 24, 2018 at 5:48pm
Topic archived. No new replies allowed.