-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLinked_list.cpp
111 lines (72 loc) · 1.45 KB
/
Linked_list.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
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next = NULL;
};
void display(Node *head)
{
std::cout <<endl<< "printing the data: " << std::endl;
while (head != NULL)
{
std::cout << head->data << std::endl;
head = head->next;
}
}
void addAtEnd(Node **head)
{
Node *New;
int d;
New = new (Node);
Node *last = *head;
std::cout << std::endl<< "Enter the data of new Node: ";
std::cin >> d;
while (last->next != NULL)
last = last->next;
New->data = d;
last->next = New;
New->next = NULL;
cout<<"Successfully added!"<<endl<<endl;
}
void addAtStart(Node** head){
int d;
Node* New;
New = new(Node);
Node *start = *head;
std::cout << std::endl<< "Enter the data of new Node: ";
std::cin >> d;
New->data = d;
New->next = (*head);
(*head) = New;
cout<<"Successfully added!"<<endl<<endl;
}
int main()
{
Node *first;
first = new (Node);
std::cout << "Enter the data of first node: ";
std::cin >> first->data;
int c;
while (c != 4)
{
std::cout << endl<<"What do you want to do?\n1.Add at the end\n2.Add at begining\n3.Display all elements\n4.Exit \nEnter: ";
std::cin >> c;
switch (c)
{
case 1:
addAtEnd(&first);
break;
case 2:
addAtStart(&first);
break;
case 3:
display(first);
break;
default:
break;
}
}
return 0;
}