Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct node{
int data;
struct node* next;
};
struct node* head;
void insert(int data,int n)
{
 node* temp1=new node();
 temp1->data= data;
 temp1->next=NULL;
 if (n==1){
    temp1->next=head;
    head=temp1;
    return;
 }
 node* temp2=head;
 for(int i=0;i<n-2;i++){
    temp2=temp2->next;
}
temp1->next=temp2->next;
temp2->next= temp1;
};
void print(){
node* temp=head;
while(temp!= NULL){
    cout<<temp->data<<"\n";
    temp=temp->next;
}
}
int main()
{
 head=NULL;
 insert(2,1);
 insert(7,2);
 insert(8,3);
 insert(6,4);
 insert(0,4);
 insert(3,4);
 print();
}


What I have tried:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct node{
int data;
struct node* next;
};
struct node* head;
void insert(int data,int n)
{
 node* temp1=new node();
 temp1->data= data;
 temp1->next=NULL;
 if (n==1){
    temp1->next=head;
    head=temp1;
    return;
 }
 node* temp2=head;
 for(int i=0;i<n-2;i++){
    temp2=temp2->next;
}
temp1->next=temp2->next;
temp2->next= temp1;
};
void print(){
node* temp=head;
while(temp!= NULL){
    cout<<temp->data<<"\n";
    temp=temp->next;
}
}
int main()
{
 head=NULL;
 insert(2,1);
 insert(7,2);
 insert(8,3);
 insert(6,4);
 insert(0,4);
 insert(3,4);
 print();
}
Posted
Updated 9-Aug-23 2:51am
Comments
CHill60 21-Jul-20 8:50am    
Your question is not clear. How could a user input a linked list into anything? Please rephrase your question and tidy up your question so that the code is only presented once.
Also - try some research - Google[^]
Critical Fist 21-Jul-20 9:01am    
i want user to input a linked list instead of me inserting it manually

Replace this code:
insert(2,1);
insert(7,2);
insert(8,3);
insert(6,4);
insert(0,4);
insert(3,4);
With a loop which reads the data and insert position from the user using cin and than calls insert passing those values.
Hint: Your insert function will need to do some bounds checking.

But this is your homework, not ours - so writing the code is your task!
 
Share this answer
 
Comments
Critical Fist 21-Jul-20 9:21am    
i did this code and its working but i wanted to know if this is efficient.
count=2;
do
{
cout<<"enter the number or enter -1 if you want to quit";
cin>>n;
if (n!=-1)
{
insert(n,count);
};
count++;
}while(n!=-1);
count 2 because i have also given a manuel item in the first slot
OriginalGriff 21-Jul-20 9:43am    
I'd suggest that instead of calling "insert" all the time with a number, you write a "add" method which traverses the list and adds to the end, then call that instead. It makes the calling code a lot "cleaner" - I'd also provide an "AddToFront" function that effectively does "insert" at the head.
Critical Fist 21-Jul-20 9:53am    
ok thank you
OriginalGriff 21-Jul-20 10:06am    
You're welcome!
# include <iostream>
# include <conio.h>

using namespace std;

struct node // design of node
{
int data;
node* link;
};


node* head; // global variable head


void end(int x) //adds element to the end of linked list
{
node* temp= head;
node* temp1=new node();
temp1->data=x;
temp1->link=NULL;
if(head==NULL)
{
head=temp1;
return;
}
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=temp1;
}


void start(int x) // adds element to the start of linked list
{
node* temp=new node();
temp->data=x;
temp->link=head;
head=temp;
}


void atn(int x, int n) // adds element to nth place of linked list
{
node* temp=head;
node* temp1= new node();
temp1->data=x;
if(n==1)
{
temp1->link=temp;
head=temp1;
return;
}
for(int i=1;i<n-1;i++)
{
="" temp="temp-">link;
}
temp1->link=temp->link;
temp->link=temp1;
}


void print() // prints element in the linked list
{
node* temp= head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->link;
}
}


int main()
{
head=NULL;
int a;
cout<<"Choose operation\n";
cout<<"Press 1 and hit enter for insertion at beginning\n";
cout<<"Press 2 and hit enter for insertion at ending\n";
cout<<"Press 3 and hit enter for insertion at nth position\n";
cin>>a;
if(a==1)
{
int n;
cout<<"Enter number of elements in list\n";
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cout<<"Enter number\n";
cin>>x;
start(x);
print();
cout<<"\n";
}
}
if(a==2)
{
int n;
cout<<"Enter number of elements in list\n";
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cout<<"Enter number\n";
cin>>x;
end(x);
print();
cout<<"\n";
}
}
if(a==3)
{
int n;
cout<<"Enter number of elements in list\n";
cin>>n;
cout<<"Enter the elements of sample linked list\n";
for(int i=1;i<=n;i++)
{
int x;
cout<<"Enter number\n";
cin>>x;
end(x);
print();
cout<<"\n";
}
while(1)
{
int x,y;
cout<<"Enter number and position of number\n";
cin>>x>>y;
atn(x,y);
print();
cout<<"\n";
}
}
}
 
Share this answer
 
Comments
Dave Kreskowiak 22-Jun-21 10:23am    
Doing someone's homework for them is not helping them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900