-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
89 lines (86 loc) · 1.9 KB
/
linked_list.cpp
File metadata and controls
89 lines (86 loc) · 1.9 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
#include<iostream>
#include<string>
#include<list>
using namespace std;
struct node{
int data;
node *next; // เก็บ address ของ node ถัดไป
};
class myList{
public:
node* head;
node* tail;
int dataSize;
myList();
void push_front(int data);
void printList();
void insert(int ith, int data);
~myList();
};
myList::myList(){
head = new node;
tail = head;
head->next = nullptr;
dataSize =0;
}
void myList::push_front(int data){
node *newNode = new node;
newNode->data = data;
newNode->next = head->next;
head->next = newNode;
if (newNode->next == nullptr){
tail = newNode;
}
dataSize++;
}
void myList::printList(){
node *temp;
temp = head;
for(int i = 0;temp!= nullptr;i++){
cout<<temp<<"|"<<temp->data<<"|"<<temp->next<<endl;
temp = temp->next;
}
}
void myList::insert(int ith, int data){
node *newNode;
node *temp;
newNode = new node;
temp = head;
newNode->data = data;
for (int i= 1;i<ith;i++){
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
dataSize++;
}
myList::~myList(){
node *temp;
node *dtemp;
temp = head;
cout<<"now deleting all"<<endl;
while(temp!=nullptr){
dtemp = temp;
temp = temp->next;
cout<<"delete = "<<dtemp<<endl;
delete dtemp;
}
}
int main(){
myList l1;
// cout<<l1.head<<endl;
// cout<<l1.head->next<<endl;
// l1.push_front(7);
// l1.push_front(6);
// l1.printList();
for(int i = 1;i<=100;i++){
l1.push_front(i);
}
l1.insert(10, 120);
l1.printList();
cout<<"tail is "<<l1.tail<<endl;
cout<<"data size = "<<l1.dataSize<<endl;
// cout<<l1.head<<"|"<<l1.head->data<<"|"<<l1.head->next<<endl;
// cout<<l1.head->next<<"|"<<l1.head->next->data<<"|"<<l1.head->next->next<<endl;
return 0;
}