-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
98 lines (85 loc) · 1.33 KB
/
linkedlist.cpp
File metadata and controls
98 lines (85 loc) · 1.33 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
#include <iostream>
using namespace std;
class linkedlist{
public:
struct Node{
int data;
Node* next;
};
linkedlist(){
head = NULL;
}
void insert(int val){
Node *n= new Node();
n->data = val;
n->next = head;
head = n;
}
void addToEnd(int val){
Node *n = new Node();
Node *temp;
temp=head;
n->data = val;
n->next = NULL;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next = n;
}
void display(){
Node *temp;
if(head==NULL){
cout<<"empty";
}
temp = head;
while(temp->next!=NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<temp->data<<" ";
}
void deleteval(int val){
Node *temp;
Node *prev;
if(head==NULL){
cout<<"empty";
}
if(head->data= val){
head=head->next;
}
temp = head->next;
prev = head;
while(temp->next!=NULL){
if(temp->data==val){
prev->next= temp->next;
break;
}
prev = prev->next;
temp=temp->next;
}
if(temp->data==val){
prev->next= NULL;
}
}
void pop(){
head=head->next;
}
private:
Node* head;
};
int main(){
linkedlist list;
list.insert(5);
list.insert(10);
list.insert(15);
list.insert(20);
list.insert(14);
list.display();
cout<<endl;
//list.deleteval(14);
list.addToEnd(200);
//list.pop();
list.insert(22);
list.display();
return 0;
}