-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkedlist3.cpp
52 lines (51 loc) · 1008 Bytes
/
linkedlist3.cpp
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
#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head;
void insert(int data,int pos)
{ Node* temp1=new Node();
temp1->data=data;
temp1->next=NULL;
if(pos==1)
{
temp1->next=head;
head=temp1;
return;
}
Node* temp2=head;
for(int i=0;i<pos-2;i++)
{
temp2=temp2->next;
}
temp1->next=temp2->next;
temp2->next=temp1;
}
void print(){
Node* temp=head;
std::cout<<"list is:";
while(temp!=NULL)
{
std::cout<<temp->data<<" ";
temp=temp->next;
}
std::cout<<"\n";
}
int main()
{
head=NULL;
int n;
int data,pos;
std::cout<<"How many no you want to insert in this linked list\n";
std::cin>>n;
for(int i=0;i<n;i++)
{
std::cout<<"Enter the no to insert\n";
std::cin>>data;
std::cout<<"Enter the position to insert\n";
std::cin>>pos;;
insert(data,pos);
print();
}}