-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.cpp
85 lines (67 loc) · 1.3 KB
/
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
#include"list.h"
List::List(){
HEAD = NULL;
TAIL = NULL;
}
List::~List(){}
bool List::isEmpty(){
return HEAD == NULL;
}
void List::addToHead(int data)
{
Node* new_node=new Node(); //create new node
new_node->info=data;
new_node->next=HEAD;
HEAD=new_node;
if(TAIL=NULL){
TAIL=HEAD;}
}
void List::addToTail( int data){
Node* new_node=new Node();
new_node->info=data;
new_node->next=NULL;
TAIL->next=new_node;
Tail=new_node;
}
void List::add(int data,Node* predecessor)
{
Node* new_node=new Node();
new_node->info=data;
new_node->next=predecessor->next;
predecessor->next=new_node;
}
void List::removeFromHead()
{
Node* nodeToDelete=HEAD;
HEAD=nodeToDelete->next;
delete nodeToDelete;
}
/*void List::remove(int data){
if(isEmpty()){
std::cout<<"The List is Empty"<<endl;
}
if(HEAD->info==data){
removeFromHead();
if(HEAD==NULL){TAIL==NULL;}
else{
temp=HEAD->next;
prev=HEAD;
}
}
}*/
void List::traverse()
{
Node* temp=HEAD;
while(temp!=NULL){
std::cout<<temp->info;
temp=temp->next;
}
}
int main(){
List l;
std::cout<<l.isEmpty();
l.addToHead(6);
l.addToHead(12);
l.addToHead(15);
l.traverse();
}