The function InsertAtnthPosition()
is not executing properly at the program crashes. I have yet to add deallocation statements. What is the error in that function? Anyways i am posting the code for the whole program which i am trying to build.
The function mentioned after executing it's last statement shows this : Process Returned -1073741819 (0xc0000005)
I am using code blocks as i dont have my laptop right now,usually i use eclipse or VSCode.
Is it running out memory? Or is it something else?
#include<iostream>
#include<stdlib.h>
usingnamespace std;
void InsertAtnThPosition();
void InsertAtBeginning();
void Print();
void Exit();
struct Node
{
int data;
Node *link;
};
Node *head,*temp2;
int main()
{
head = NULL; // List Is Empty
int choice;
char ans = 'y';
do
{
cout << "\n1.Insert At Beginning\n2.Insert at nTH Position\n3.Delete An Element\n4.Print The Linked List\n5.Exit\n";
cin >> choice;
if (choice == 1)
{
InsertAtBeginning();
}
elseif (choice == 2)
{
InsertAtnThPosition();
}
elseif (choice == 4)
{
Print();
}
elseif (choice == 5)
{
Exit();
}
}
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;
}
elseif (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
}
else
temp2 = head;
for (int i = 0 ; i < pos -2 ; ++pos)
{
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 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);
}
for (int i = 0; i < pos - 2; ++pos)
{
temp2 = temp2->link; // temp 2 is already pointing at first node
}
temp2 is bad here. I discovered this by running the program under a debugger. Learn to use the debugger.
You've written an infinite loop here. This for loop will go on and on until pos overflows and wraps back around to a negative number, or until temp2 is bad it crashes.
Ah,I quite didn't get you here,sorry,What do you mean by saying that temp2 is bad? and how is it an infinite loop? It will break once i >= pos- 2. Sorry if i m being dumb,i am coding in c++ after long time,am refreshing my data structures concepts.
for (int i = 0 ; i < pos -2 ; ++i)
{
temp2 = temp2 -> link; // temp 2 is already pointing at first node
}
I corrected the loop and now i am facing another problem,if i input pos = 1 it removes the element at the first position and replaces instead of pushing it down! While my logic is aimed at the latter. ??
@repeater every thing is working except for pos = 1;
In general you likely want to use zero-based indexing to not confuse everyone.
example with string data for clarity:
Data: blah -> foo -> bar -> test -> hooray
Pos: 0 1 2 3 4
Typically an insert means you insert before a specified position. So if user entered "0", the new data would be inserted at the start, before "blah".
The spacing throughout the program could also be improved to reduce confusion. At first glance "pos -2" makes one think something is happening with a negative two.
Similarly, temp2->link is more clear at a glance than temp2 -> link -- the first way better shows an association.
Actually I've been always indexing linked lists from 1 since i started but I will look into your suggestion and yes i know i about that spacing,i don't do it anymore now as many others also pointed out thank you very much.