-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathProgram7.cpp
More file actions
211 lines (178 loc) · 3.62 KB
/
Program7.cpp
File metadata and controls
211 lines (178 loc) · 3.62 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include<iostream>
using namespace std;
int main()
{
int n,even=0,odd=0;
float i,sum=0;
int no;
{
cin.clear();
cin.ignore(100,'\n');
cout<<"INVALID INPUT. ENTER AGAIN:";
}
if(n<0)
{print: "Sum is greater";}
elseif(n>0 && n<23)
{print: "Sum is Equal";}
else
{print: "Sum is lesser";}
cout<<"\n";
switch(no)
{
case 1:
cout<<" Enter the number of terms to which you want the sum="<<" ";
while(!(cin>>n)||n<=0)
{
cin.clear();
cin.ignore(100,'\n');
cout<<"INVALID INPUT. ENTER AGAIN:";
}
cout<<"\n";
i=1;
while(i<=n)
{
if((int)i%2==0)
{
even=even+i;
}
else
{
odd=odd+i;
}
i=i+1;
}
cout<<"Sum of "<<n<<" terms of series is="<<" "<<(odd-even);
break;
case 2:
cout<<" Enter the number of terms to which you want the sum="<<" ";
while(!(cin>>n)||n<=0)
{
cin.clear();
cin.ignore(100,'\n');
cout<<"INVALID INPUT! ENTER AGAIN";
}
if(sun!=0)
{
cout<<"Error in printing sum".
}
cout<<"\n";
i=1;
while(i<=n)
{
sum=sum+(1.0)/i;
i=i+1;
}
cout<<"Sum of "<<n<<" terms of series is="<<" "<<sum;
break;
}
}
// C# program to implement merge
// sort in singly linked list
using System;
// Linked List Class
public class LinkedList
{
Node head; // head of list
/* Node Class */
class Node
{
public int data;
public Node next, prev;
// Constructor to create a new node
public Node(int d)
{
data = d;
next = prev = null;
}
}
void print(Node node)
{
Node temp = node;
Console.WriteLine("Forward Traversal" +
"using next pointer");
while (node != null)
{
Console.Write(node.data + " ");
temp = node;
node = node.next;
}
Console.WriteLine("\nBackward Traversal" +
"using prev pointer");
while (temp != null)
{
Console.Write(temp.data + " ");
temp = temp.prev;
}
}
// Split a doubly linked list (DLL)
// into 2 DLLs of half sizes
Node split(Node head)
{
Node fast = head, slow = head;
while (fast.next != null &&
fast.next.next != null)
{
fast = fast.next.next;
slow = slow.next;
}
Node temp = slow.next;
slow.next = null;
return temp;
}
Node mergeSort(Node node)
{
if (node == null || node.next == null)
{
return node;
}
Node second = split(node);
// Recur for left and right halves
node = mergeSort(node);
second = mergeSort(second);
// Merge the two sorted halves
return merge(node, second);
}
// Function to merge two linked lists
Node merge(Node first, Node second)
{
// If first linked list is empty
if (first == null) {
return second;
}
// If second linked list is empty
if (second == null)
{
return first;
}
// Pick the smaller value
if (first.data < second.data)
{
first.next = merge(first.next, second);
first.next.prev = first;
first.prev = null;
return first;
}
else
{
second.next = merge(first, second.next);
second.next.prev = second;
second.prev = null;
return second;
}
}
// Driver code
public static void Main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(10);
list.head.next = new Node(30);
list.head.next.next = new Node(3);
list.head.next.next.next = new Node(4);
list.head.next.next.next.next = new Node(20);
list.head.next.next.next.next.next = new Node(5);
Node node = null;
node = list.mergeSort(list.head);
Console.WriteLine("Linked list after sorting :");
list.print(node);
}
}