-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDemo.cpp
141 lines (131 loc) · 3.18 KB
/
Demo.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
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
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
#include<iostream>
#include "node.h"
#include "single.h"
using namespace std;
int main()
{
SingleLinkedList list;
list.createList();
int choice,data,x,position;
while(1)
{
cout<< "\n";
cout << "1.Display list\n";
cout << "2.Count the number of nodes\n";
cout << "3.Search for an element\n";
cout << "4.Insert in empty list/Insert in beginning of the list\n";
cout << "5.Insert a node at the end of the list\n";
cout << "6.Insert a node after a specified node\n";
cout << "7.Insert a node before a specified node\n";
cout << "8.Insert a node at a given position\n";
cout << "9.Delete first node\n";
cout << "10.Delete last node\n";
cout << "11.Delete any node\n";
cout << "12.Reverse the list\n";
cout << "13.Insert Cycle\n";
cout << "14.Detect Cycle\n";
cout << "15.Remove cycle\n";
cout << "16.Bubble sort by exchanging data\n";
cout << "17.Bubble sort by exchanging links\n";
cout << "18.MergeSort\n";
cout << "19.Quit\n";
cout << "Enter your choice : ";
cin >> choice;
if( choice == 19 )
break;
switch( choice )
{
case 1:
list.displayList();
break;
case 2:
cout<<"Number of nodes is " << list.countNodes() << "\n";
break;
case 3:
cout << "Enter the element to be searched : ";
cin >> data;
position = list.find(data);
if( position == 0 )
cout << data << " not in list\n";
else
cout << data << " present at position " << position << "\n";
break;
case 4:
cout << "Enter the element to be inserted : ";
cin >> data;
list.insertAtBeginning(data);
break;
case 5:
cout << "Enter the element to be inserted : ";
cin >> data;
list.insertAtEnd(data);
break;
case 6:
cout << "Enter the element to be inserted : ";
cin >> data;
cout << "Enter the element after which to insert : ";
cin >> x;
list.insertAfter(data,x);
break;
case 7:
cout << "Enter the element to be inserted : ";
cin >> data;
cout << "Enter the element before which to insert : ";
cin >> x;
list.insertBefore(data,x);
break;
case 8:
cout << "Enter the element to be inserted : ";
cin >> data;
cout << "Enter the position at which to insert : ";
cin >> position;
list.insertAtPosition(data,position);
break;
case 9:
list.deleteFirstNode();
break;
case 10:
list.deleteLastNode();
break;
case 11:
cout << "Enter the element to be deleted : ";
cin >> data;
list.deleteNode(data);
break;
case 12:
list.reverseList();
break;
case 13:
cout << "Enter the element at which the cycle has to be inserted : ";
cin >> data;
list.insertCycle(data);
break;
case 14:
if( list.hasCycle() )
cout << "List has a cycle\n";
else
cout << "List does not have a cycle\n";
break;
case 15:
list.removeCycle();
break;
case 16:
list.bubbleSortExData();
break;
case 17:
list.bubbleSortExLinks();
break;
case 18:
list.mergeSort();
break;
default:
cout << "Wrong Choice\n";
break;
}
}
cout << "Exiting \n";
}