I'm very much a noob and I'm struggling to correctly implement the insertion of a new node in a singly-linked list. I've tried some of the more easy-to-understand solutions from both here and other sites and the problem is definitely in my brain, but I just can't manage to get this right.
So what I have is this linked list made up of n nodes (where n is given as an input by the user) where I'm trying to insert randomised numbers from 0 to 100 in increasing order and I'm then printing the content of the list.
I think my code is not quite right at all though because the output I get is just the same number over and over, but besides that if I change the code to allow the user to input the numbers instead of generating them randomly, the program crashes if I input two different numbers (it works ok if I input the same number over and over). EDIT: Furthermore, unless srand(time(NULL)); is written inside a loop, the program will compile but crash once I input the amount of elements in my list.
I really can't understand what I'm doing wrong though.
The code looks like this:
/*The program inserts n elements generated randomly in a linked list sorted increasingly, and prints the result.*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct node {
int num;
node *next;
};
node *top=NULL,*nodenew;
void sortedinsert();
void printlist();
int main() {
int n;
do {
cout<<"Insert the amount of elements in your list: ";
cin>>n;
if (n<2) {
cout<<"The list needs to contain at least 2 nodes."<<endl;
}
}
while (n<2);
for (int i=0;i<n;i++) {
srand(time(NULL));
sortedinsert();
}
printlist();
}
void sortedinsert() {
int gen=rand()%101;
nodenew=new node;
nodenew->num=gen;
nodenew->next=NULL;
if (top==NULL or top->num>=gen) {
nodenew->next=top;
top=nodenew;
return;
}
else if (top->next!=NULL and top->next->num>=gen){
node *temp=top->next;
nodenew->next=temp;
top->next=nodenew;
return;
}
else {
node *left;
node *right=top;
while (right!=NULL and right->next->num<=gen) {
left=right;
right=right->next;
}
left->next=nodenew;
nodenew->next=right;
}
}
void printlist() {
cout<<"The sorted list is shown below: "<<endl;
for (nodenew=top;nodenew!=NULL;nodenew=nodenew->next) {
cout<<nodenew->num<<endl;
}
}
i've commented the parts i changed :)
int main() {
int n; // as mentioned in top srand initialized at the begining
srand(time(NULL));
do {
cout << "Insert the amount of elements in your list: ";
cin >> n;
if (n < 2) {
cout << "The list needs to contain at least 2 nodes." << endl;
}
} while (n < 2);
for (int i = 0;i < n;i++) {
sortedinsert();
}
printlist();
}
void sortedinsert() {
int gen = rand() % 101;
nodenew = new node;
nodenew->num = gen;
nodenew->next = NULL;
// split the top part
if (top == NULL) {
top = nodenew;
return;
}
if( top->num >= gen) {
nodenew->next = top;
top = nodenew;
return;
}
else if (top->next != NULL and top->next->num >= gen) {
node *temp = top->next;
nodenew->next = temp;
top->next = nodenew;
return;
}
else {
// left was uninitialized so if it doesn't go into the loop you are going to call left->next Undefined behavior
//right->next->num<=gen you don't test this until you test right->next is not null otherwise Undefined behavior as well
node *left=top;
node *right = top->next;
while (right != NULL and right->num <= gen) {
left = right;
right = right->next;
}
left->next = nodenew;
nodenew->next = right;
}
}
In fact srand(time(NULL)) you must declare it before for loop because like that it gives the same number . And you have a problem when inserting a newnode .
And here i have corrected your code and it works well :
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct node {
int num;
node *next;
};
node *top = NULL, *nodenew;
void sortedinsert();
void printlist();
int main() {
int n;
do {
cout << "Insert the amount of elements in your list: ";
cin >> n;
if (n<2) {
cout << "The list needs to contain at least 2 nodes." << endl;
}
} while (n<2);
srand(time(NULL));
for (int i = 0; i<n; i++) {
sortedinsert();
}
printlist();
system("pause");
}
void sortedinsert() {
int gen = rand() % 101;
cout << gen << endl;
nodenew = new node;
nodenew->num = gen;
nodenew->next = NULL;
if (top == NULL || top->num >= gen) {
nodenew->next = top;
top = nodenew;
}
else
{
node *A = top;
node *B = top->next;
while (B != NULL)
{
if (B->num > gen)
{
nodenew->next = B;
A->next = nodenew;
return;
}
else
{
A = B;
B = B->next;
}
}
A->next = nodenew;
nodenew->next = NULL;
return;
}
}
void printlist() {
cout << "The sorted list is shown below: " << endl;
nodenew = top;
for (nodenew = top; nodenew != NULL; nodenew = nodenew->next) {
cout << nodenew->num << endl;
}
}
You can use the Python code as shown below . The benefits you will have with Python is :
--> It is now used in many industries , and will help you when you explore the regions of Data Science and Machine Learning.
--> It is as easy as implementing pseudocode .
I have shown you the python method for inserting a node into a sorted Doubly linked list , try to get Dry-Run the code and obtain the logic , then use the same to derive code for Singly Linked list .
def sortedInsert(head, data):
node = DoublyLinkedListNode(data)
status = 0
if not data>head.data:
node.prev=head.prev
head.prev=node
node.next=head
head=node
else:
dup = head
while(data>dup.data):
if not dup.next:
status = 1
break
else:
dup = dup.next
if status:
node.prev = dup
node.next = dup.next
dup.next = node
else:
node.prev = dup.prev
dup.prev.next = node
node.next = dup
dup.prev = node
return head
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.