-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.cpp
More file actions
125 lines (109 loc) · 1.47 KB
/
lab1.cpp
File metadata and controls
125 lines (109 loc) · 1.47 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
using namespace std;
struct node
{
int data;
node *link;
}*head;
void dispAll()
{
node *ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
ptr=ptr->link;
}
cout<<endl;
}
void insSort(int val)
{
node * ptr=head;
node *temp =new node;
node *p=NULL;
if(head==NULL)
{
temp->data=val;
temp->link=NULL;
head=temp;
//return;
}
else if(head->data>val)
{
temp->data=val;
temp->link=head;
head=temp;
}
else
{
while(ptr!=NULL&&ptr->data<val)
{
p=ptr;
ptr=ptr->link;
}
if(ptr=NULL)
{
temp->data=val;
temp->link=NULL;
p->link=temp;
//return;
}
//
else
{
temp->data=val;
temp->link=p->link;
p->link=temp;
}
}
dispAll();
}
void dispn(int n)
{
node *ptr=head;
int s=1,x=1;
while(ptr!=NULL)
{
ptr=ptr->link;
s++;
}
x=s-n-1;
ptr=head;
for(int i=1;i<=x;i++)
{
ptr=ptr->link;
}
cout<<"Element is "<<ptr->data<<endl;
}
int main()
{
int x;
while(1)
{
cout<<"\tEnter a choice:\n1. Enter new Number\n2. Display List.\n3. Element at nth position from lasr \n4. Exit\n\n Enter: ";
cin>>x;
// while(x!=20)
// {
// cin>>x;
// insSort(x);
// }
// //dispAll();
switch(x)
{
case 1: int n;
cout<<"Enter the Number :";
cin>>n;
insSort(n);
break;
case 2: dispAll();
break;
case 3: cout<<"Enter Index :" ;
cin>>n;
dispn(n);
break;
case 4: return 0;
default: cout<<"Invalid option"<<endl;
break;
}
}
return 0;
}