-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfourth.cpp
77 lines (77 loc) · 1.44 KB
/
fourth.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
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* head;
node* getNewNode(int x)
{
node* temp = new node();
temp->data = x;
temp->next = temp;
return temp;
}
void insertAttheEnd(int x) {
node* temp1 = getNewNode(x);
node* temp = head;
while(temp->next != head)
{
temp = temp->next;
}
temp->next = temp1;
temp1->next = head;
}
void display() {
node* temp = head;
do
{
std::cout << temp->data << '\n';
temp = temp->next;
}while (temp != head);
}
void insertAttheStart(int x)
{
node* temp1 = getNewNode(x);
if (head == NULL) {
head = temp1;
temp1->next = head;
return;
}
node* temp = head;
temp1->next = head;
while (temp->next != head) {
temp =temp->next;
}
temp->next = temp1;
head = temp1;
}
void removeAtTheEnd() {
node* temp = head;
node* temp1;
while (temp->next != head) {
temp1 = temp;
temp = temp->next;
}
temp1->next = head;
delete temp;
}
int main(int argc, char const *argv[]) {
head = NULL;
insertAttheStart(3);
std::cout << '\n';
display();
insertAttheStart(2);
std::cout << '\n';
display();
insertAttheEnd(5);
std::cout << '\n';
display();
insertAttheEnd(6);
std::cout << '\n';
display();
std::cout << '\n';
removeAtTheEnd();
display();
return 0;
}