-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCPP-16-Linked-list.txt
More file actions
72 lines (62 loc) · 1.42 KB
/
Copy pathCPP-16-Linked-list.txt
File metadata and controls
72 lines (62 loc) · 1.42 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
/*
1. Define headers
2. Define class linkedlist with private variables and public functions
3. Define a strucure in CPP to have node details
4. Define append() function to add new nodes
5. Define display() to display the linked list items
6. Define main() function to invoke all class functions
*/
#include<iostream>
#include<iomanip>
using namespace std;
class linkedlist{
private:
struct node {
int info;
node *link;
}*start;
public:
linkedlist(){
start = NULL;
}
void append(int);
void display();
};
void linkedlist::append(int item){
node *x = new node;
x->info = item;
x->link = NULL;
if(start == NULL) {
start = x;
}
else {
node *temp = start;
while(temp->link != NULL){
temp = temp->link;
}
temp->link = x;
}
cout<<item<<" is inserted"<<endl;
}
void linkedlist::display(){
node *trav = start;
cout<<"\n Linked list is";
if(start == NULL) {
cout<<"Linked list is empty";
return;
}
while(trav != NULL){
cout<<trav->info<<endl;
trav = trav->link;
}
}
int main(){
linkedlist *x = new linkedlist;
x->display();
x->append(100);
x->display();
x->append(200);
x->display();
x->append(300);
x->display();
}