-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedlist.cpp
More file actions
153 lines (129 loc) · 3.87 KB
/
Linkedlist.cpp
File metadata and controls
153 lines (129 loc) · 3.87 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
#include <iostream>
using namespace std;
// Structure to define a node in the linked list
struct Node
{
int data;
Node *next;
};
// Pointer to the front (head) of the linked list
Node *front = NULL;
// Pointer to the end (tail) of the linked list
Node *rear = NULL;
// Function to insert an element into the linked list (queue)
void insert(int value)
{
// Allocate a new node in the memory
Node *newNode = new Node;
// Store the value in the new node
newNode->data = value;
// Set the next pointer of the new node to NULL
newNode->next = NULL;
// If the linked list is empty, set the front and rear pointers to the new node
if (front == NULL && rear == NULL)
{
front = rear = newNode;
return;
}
// Add the new node to the end (rear) of the linked list
rear->next = newNode;
rear = newNode;
}
// Function to delete an element from the linked list (queue)
void deletion(int position)
{
// If the linked list is empty, return
if (front == NULL)
{
cout << "The linked list is empty" << endl;
return;
}
// Store the pointer to the front node in a temporary variable
Node *temp = front;
// If the user wants to delete the first node, set the front pointer to the next node
if (position == 1)
{
front = temp->next;
delete temp;
return;
}
// Traverse the linked list to find the node at the specified position
for (int i = 0; temp != NULL && i < position - 2; i++)
{
temp = temp->next;
}
// If the specified position is greater than the number of nodes in the linked list, return
if (temp == NULL || temp->next == NULL)
{
cout << "The specified position is not available in the linked list" << endl;
return;
}
// Store the pointer to the node to be deleted in a temporary variable
Node *next = temp->next->next;
// Free the memory occupied by the node to be deleted
delete temp->next;
// Update the next pointer of the previous node to point to the next node
temp->next = next;
}
// Function to display the elements in the linked list (queue)
void display()
{
// If the linked list is empty, return
if (front == NULL)
{
cout << "The queue is empty" << endl;
return;
}
// Create a pointer to traverse the linked list
Node *ptr = front;
// Traverse the linked list and print the elements
while (ptr != NULL)
{
cout << ptr->data << " ";
ptr = ptr->next;
}
cout << endl;
}
int main()
{
// Declare a variable to store the user's choice
int choice;
// Run an infinite loop until the user chooses to exit
while (true)
{
// Print the menu options
cout << "1. Insert an element into the queue" << endl;
cout << "2. Delete an element from the queue" << endl;
cout << "3. Display the elements in the queue" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
// Read the user's choice
cin >> choice;
// Perform the corresponding operation based on the user's choice
switch (choice)
{
case 1: // Insert an element into the queue
int value;
cout << "Enter the value to be inserted: ";
cin >> value;
insert(value);
break;
case 2: // Delete an element from the queue
int position;
cout << "Enter the position of the element to be deleted: ";
cin >> position;
deletion(position);
break;
case 3: // Display the elements in the queue
display();
break;
case 4: // Exit
exit(0);
break;
default: // Invalid choice
cout << "Invalid choice, try again" << endl;
break;
}
}
return 0;
}