linked list how to input values from the user

0 ragmar thomas · September 8, 2014
#include <iostream>

using namespace std;
void insert(int data, int positionOf_n);
void print();

struct node
{
int data;
struct node *next;
};

struct node *head;

int main()
{
head = NULL; //this is the line of code which i want to
insert(2, 1); //replace
insert(3, 2); //with the
insert(4, 1); //two for loops
insert(5, 2); //in the below other code
print();
}

//insert
void insert(int data, int positionOf_n){
node *temp1 = new node;
temp1 -> data = data;
temp1 -> next = NULL;

if(positionOf_n == 1){
temp1 -> next = head;
head = temp1;
return;
}
node *temp2 = head;

for(int i = 0; i < positionOf_n-2; i++)
temp2 = temp2 -> next;

temp1 -> next = temp2 -> next;
temp2 -> next = temp1;
}

//print
void print(){
node *temp = head;

while(temp != NULL){
cout << temp -> data << " ";
temp = temp -> next;
}
cout << endl;
}

this is the original code which use the insert function call in the main as predefined that doesn't allow the user to input the value that they want
#include <iostream>

using namespace std;

void insert(int data, int positionOf_n);
void print();

struct node
{
int data;
struct node *next;
};

struct node *head;

int main()
{
head = NULL; //empty list

cout << "how many values you want to enter: ";
int n, i, j;
cin >> n;

cout << "insert the value and the position " << endl;
int value, position;

for(i = 1; i <= n; i++){ //here in the
cout << " the value: "; //modified
cin >> value; //code
} //that replace
cout << endl; //the above insert()
for(j = 1; j <= n; j++){ //with numbers in the function call
cout << " the position: "; //
cin >> position; //
}
insert(value, position);
print();
}

//insert
void insert(int data, int positionOf_n){
node *temp1 = new node;
temp1 -> data = data;
temp1 -> next = NULL;

if(positionOf_n == 1){
temp1 -> next = head;
head = temp1;
return;
}
node *temp2 = head;

for(int i = 0; i < positionOf_n-2; i++)
temp2 = temp2 -> next;

temp1 -> next = temp2 -> next;
temp2 -> next = temp1;
}

//print
void print(){
node *temp = head;

while(temp != NULL){
cout << temp -> data << " ";
temp = temp -> next;
}
cout << endl;
}

this is the code i tried to replace the above code by giving the user a way of inserting its own value by using the for loops in the main function  but it keeps crashing, is there a way of replacing the insert function call in the main function by the for loops or other way just for accepting values from the user.

Post a Reply

Replies

Oldest  Newest  Rating
0 ragmar thomas · September 9, 2014
thanks for mentioning the loop part i have found the problem
int main(){

cout << "enter the list amount you want to enter: ";
cin >> n;

int value, position, i, j;

for(i = 0; i <= n; i++){
cout << " the value: ";
cin >> value;

for(j = i; j <= i; j++){
cout << " the position: ";
cin >> position;

insert(value, position);
cout << endl;
print();}
}
}
0 ragmar thomas · September 8, 2014
it is how much list that the user want to input.
if the user wants n values to be inserted in the list the loop goes until n accepting all the value upto n from the user.
.
  • 1

C++

106,994 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator