/
LinkedList.cpp
71 lines (65 loc) · 1.05 KB
/
LinkedList.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
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *link;
};
class LinkedList
{
private:
Node *start, *cur, *temp;
public:
//This is the Constructor
LinkedList()
{
start = NULL;
}
//Method to add the data into the list. . .
add(int NewNumber)
{
if(start == NULL)
{
start = new Node;
start->data = NewNumber;
start->link = NULL;
}
else
{
cur=start;
while(cur->link!=NULL)
{
cur=cur->link;
}
//Creating a new node and adding data into it.
temp = new Node;
temp->data = NewNumber;
temp->link = NULL;
cur->link = temp;
}
}
//Method to print the data of nodes. .
Print_LinkedList_Data()
{
cout<<"Data in linked list"<<endl;
cur=start;
while(cur->link!=NULL)
{
cout<<cur->data<<endl;
cur = cur->link;
}
cout<<cur->data<<endl;
}
};
int main()
{
LinkedList obj;
int val,p;
cout<<"Enter Six Values for LinkedList"<<endl;
for(int i = 1; i<=6; i++)
{
cin>>val;
obj.add(val);
}
obj.Print_LinkedList_Data();
}