This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 581
/
Insertion_sort_using_linked_list.cpp
97 lines (84 loc) · 1.92 KB
/
Insertion_sort_using_linked_list.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
using namespace std;
struct Node // strcuture node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
void insert(int x)
{
// struct Node link = (struct Node)malloc(sizeof(struct Node));
struct Node *link = (struct Node *)malloc(sizeof(struct Node));
link->data = x;
link->next = NULL;
if(head==NULL)
{
link->next = head;
head = link;
}
else
{
struct Node *current, *temp = NULL;
current=head;
bool ans=true;
while(current!=NULL)
{
if(link->data<=current->data)
{
if(temp==NULL)
{
link->next=current;
head=link;
ans=false;
break;
}
else
{
temp->next=link;
link->next=current;
ans=false;
break;
}
if(!ans)
break;
}
temp=current;
current=current->next;
}
if(ans)
{
temp->next=link;
link->next=NULL;
}
}
}
void disp()
{
struct Node *prt = head;
printf("List is : \n");
while (prt != NULL)
{
printf("%d\t", prt->data);
prt = prt->next;
}
printf("\n");
}
int main()
{
int x;
for(int i=0;i<5;i++)
{
cout<<"Give Data of Node "<<i+1<<":";
cin>>x;
insert(x);
}
disp();
return 0;
}