-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinkedlist5.cpp
71 lines (69 loc) · 1.32 KB
/
Linkedlist5.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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 rev(){
Node *current,*prev,*next;
current=head;
prev=NULL;
while(current!=NULL)
{
next=current->next;
current->next=prev;
prev=current;
current=next;
}
head=prev;
return;
}
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();
}
std::cout<<"The linked list in reverse order is:-\n";
rev();
print();
}