-
Notifications
You must be signed in to change notification settings - Fork 0
/
labtestdsa.cpp
85 lines (84 loc) · 1.41 KB
/
labtestdsa.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
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *ptr;
};
void creation( node **head, int d)
{
node *temp;
node *newnode=new node;
newnode->data=d;
newnode->ptr=NULL;
if(*head==NULL)
{
*head=newnode;
temp=newnode;
}
else
{
temp->ptr=newnode;
temp=temp->ptr;
}
}
void last_insert(node **head,int d)
{
node *newnode= new node;
newnode->data=d;
newnode->ptr=NULL;
node *temp=*head;
while(temp->ptr!=NULL)
temp=temp->ptr;
temp->ptr=newnode;
}
void first_insertion(node **head, int d)
{
node *newnode= new node;
newnode->data=d;
newnode->ptr=*head;
*head=newnode;
}
void print(node **head)
{
node *temp=*head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->ptr;
}
}
void sortlinked(node **head)
{
node *i,*j;
int sw;
for(i=*head;i->ptr!=NULL;i=i->ptr)
{
for(j=i->ptr;j!=NULL;j=j->ptr)
{
if(i->data > j->data)
{
sw=i->data;
i->data=j->data;
i->data=sw;
}
}
}
}
int main()
{
int n;
cin>>n;
node *head=NULL;
for(int i=0;i<n;i++)
{
int d;
cin>>d;
creation(&head,d);
}
// last_insert(&head,10);
first_insertion(&head,69);
sortlinked(&head);
print(&head);
}