-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp14SCLL.cpp
More file actions
150 lines (130 loc) · 3.29 KB
/
p14SCLL.cpp
File metadata and controls
150 lines (130 loc) · 3.29 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
/*
14) Design, Develop and Implement a Program to perform insertion, deletion and traversing in a sorted Singly Circular Linked List (SCLL) with header nodes.
*/
#include <iostream>
using namespace std;
// Structure for Node
struct Node
{
int data;
Node *next;
};
// Class for Sorted Singly Circular Linked List
class SCLL
{
private:
Node *header;
public:
// Constructor to initialize the list
SCLL()
{
header = new Node();
header->next = header; // Circular linked list with one node
}
// Function to insert a new node in sorted order
void insert(int value)
{
Node *newNode = new Node();
newNode->data = value;
Node *temp = header;
// Traverse the list to find the correct position
while (temp->next != header && temp->next->data < value)
{
temp = temp->next;
}
// Insert the new node after temp
newNode->next = temp->next;
temp->next = newNode;
}
// Function to delete a node with a specific value
void deleteNode(int value)
{
Node *temp = header;
Node *toDelete = nullptr;
// Traverse to find the node
while (temp->next != header && temp->next->data != value)
{
temp = temp->next;
}
// If the node is found
if (temp->next != header)
{
toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
cout << "Deleted " << value << endl;
}
else
{
cout << "Node with value " << value << " not found!" << endl;
}
}
// Function to traverse and print the list
void traverse()
{
Node *temp = header->next;
if (temp == header)
{
cout << "List is empty!" << endl;
return;
}
cout << "List contents: ";
while (temp != header)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Destructor to free allocated memory
~SCLL()
{
Node *temp = header->next;
while (temp != header)
{
Node *toDelete = temp;
temp = temp->next;
delete toDelete;
}
delete header;
}
};
int main()
{
cout << "ABHISHEK SINGH 231527282435222";
SCLL list;
int choice, value;
while (true)
{
// Menu
cout << "\n--- Sorted Singly Circular Linked List Menu ---\n";
cout << "1. Insert an element\n";
cout << "2. Delete an element\n";
cout << "3. Traverse the list\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the value to insert: ";
cin >> value;
list.insert(value);
break;
case 2:
cout << "Enter the value to delete: ";
cin >> value;
list.deleteNode(value);
break;
case 3:
list.traverse();
break;
case 4:
cout << "Exiting program.\n";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}