-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLink_List.cpp
122 lines (118 loc) · 3.27 KB
/
Link_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Name: Kathan Sanghavi
// ID: 201901053
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
template<typename T> // made using template so it can be used for different node structures.
class LinkList
{
private:
T* head;
T* tail;
public:
LinkList(){head = NULL; tail = NULL;}; // Constructor
void linkInsert(T *n)
{
long int ID1 = n->ID;
T* ptr = head;
if(ptr == NULL)
{
n->next = NULL;
head = n;
tail = n;
}
else
{
if(ptr->ID>ID1)
{
n->next = ptr;
head = n;
}
else
{
while(ptr->next!=NULL && ptr->next->ID<ID1)
{
ptr = ptr->next;
}
n->next = ptr->next;
ptr->next = n;
if(n->next == NULL)
tail = n;
}
}
}
T* linkSearch(long int ID1)
{
T* ptr = head;
while(ptr!=NULL && ptr->ID<ID1)
{
ptr = ptr->next;
}
if(ptr==NULL||ptr->ID!=ID1)
{
return NULL;
}
else
{
return ptr;
}
}
char* linkDelete(long int ID1) // delete by ID and return the name. Name can be further used if needed.
{
T* ptr = head;
T* prev = NULL;
while(ptr!=NULL && ptr->ID<ID1)
{
prev = ptr;
ptr = ptr->next;
}
if(ptr==NULL||ptr->ID!=ID1)
{
char *r = new char[10];
strcpy(r,"NOT FOUND");
return r;
}
else if(prev==NULL)
{
head = ptr->next;
if(head==NULL)
tail = NULL;
return ptr->Name;
}
else if(ptr->next==NULL)
{
tail = prev;
prev->next = NULL;
return ptr->Name;
}
else
{
prev->next = ptr->next;
return ptr->Name;
}
}
float Compute_Bill() // specifically designed for computing bill from Billing object
{
float bill = 0;
T* ptr = head;
while(ptr!=NULL)
{
bill+=ptr->amount;
ptr = ptr->next;
}
return bill;
}
void Print_List() // specifically designed for printing bill from Billing object
{
T* ptr = head;
while(ptr!=NULL)
{
cout<<ptr->ID<<" "<<ptr->Name<<" "<<ptr->Rate<<" "<<ptr->Quantity<<" "<<ptr->amount<<"\n";
ptr = ptr->next;
}
}
};