-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenter in asc oreder LL.cpp
61 lines (56 loc) · 1.11 KB
/
enter in asc oreder LL.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node {
int data;
struct node * link;
};
void insert ( struct node **, int ) ;
void display ( struct node * ) ;
int main() {
struct node *p ;
p=NULL;
int n;
char ch[10];
do {
printf("Enter the value\n");
scanf("%d",&n);
insert(&p,n);
printf("Do you want to add another node? Type Yes/No\n");
scanf("%s",ch);
} while(!strcmp(ch,"Yes"));
printf("The elements in the linked list are");
display(p);
printf("\n");
return 0;
}
void insert ( struct node **q, int num ) {
struct node *temp=*q;
struct node *nn=(struct node *)malloc(sizeof(struct node));
nn->data=num;
nn->link=NULL;
if((*q)==NULL || (*q)->data >= nn->data)
{
nn->link=(*q);
(*q)=nn;
return;
}
else
{
temp=*q;
while(temp->link!=NULL && temp->link->data < nn->data)
{
temp=temp->link;
}
nn->link=temp->link;
temp->link=nn;
}
}
void display ( struct node *q ) {
struct node *temp=q;
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->link;
}
}