-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgLLsorting.cpp
More file actions
55 lines (54 loc) · 824 Bytes
/
sgLLsorting.cpp
File metadata and controls
55 lines (54 loc) · 824 Bytes
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
#include<iostream>
using namespace std;
struct node
{
int data;
node*next;
};
node*first,*temp,*ttemp,*p;
void addnodesortedly()
{
ttemp=new node;
cin>>ttemp->data;
ttemp->next=nullptr;
if(!first)
{
first=ttemp;
return;
}
if(ttemp->data>first->data)
{
ttemp->next=first;
first=ttemp;
return;
}
temp=first;
while(temp->next && temp->next->data>ttemp->data)
{
temp=temp->next;
}
ttemp->next=temp->next;
temp->next=ttemp;
}
void disp()
{
temp=first;
while(temp!=nullptr)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int main()
{
first=temp=ttemp=nullptr;
int n;
cout<<"enter number of elements in LL: ";
cin>>n;
for(int i=0;i<n;i++)
{
addnodesortedly();
}
cout<<"sorted LL\n";
disp();
}