-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.cpp
More file actions
176 lines (171 loc) · 4.16 KB
/
Copy pathList.cpp
File metadata and controls
176 lines (171 loc) · 4.16 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*-------------------------------------------------------------------------------------------------------------------------------------------
List.cpp
An object-oriented program in cpp to implement an ordered list.
181713
13-02-19
-------------------------------------------------------------------------------------------------------------------------------------------*/
#include<iostream>
using namespace std;
typedef struct ListType{
int Num;
struct ListType *Next;
}LISTNODE;
class List
{
private:
LISTNODE *Head;
public:
List();
LISTNODE *MakeListNode(int Num);
void InsertListNode(int Num);
int DeleteFirstNode();
void DeleteListNode(int Num);
void Display();
};
main()
{
List L;
int n,ele,ch;
cout<<"Program to perform List operations"<<endl;
while(1){
cout<<"\nMenu\n";
cout<<"1.Insert list element\n";
cout<<"2.Delete first element from list\n";
cout<<"3.Delete a specified item from list\n";
cout<<"4.Display \n";
cout<<"5.Exit\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch){
case 1: cout<<"Enter the element: ";
cin>>ele;
L.InsertListNode(ele);
break;
case 2: ele=L.DeleteFirstNode();
if(ele!=-1)
cout<<"\nThe deleted element is: "<<ele;
break;
case 3: cout<<"Enter the element to be deleted: ";
cin>>ele;
L.DeleteListNode(ele);
break;
case 4: L.Display();
break;
case 5:exit(0);
default:cout<<"\nSorry! You have entered a wrong choice!!";
}
}
}
//-------------------------------------------------------List()---------------------------------------------------------------------------
List::List()
{
Head=NULL;
}
//-------------------------------------------------------MakeListNode()---------------------------------------------------------------------------
LISTNODE *List::MakeListNode(int Num)
{
LISTNODE *Node;
Node=(LISTNODE *)malloc(sizeof(LISTNODE));
Node->Num=Num;
Node->Next=NULL;
return Node;
}
//-------------------------------------------------------InsertListNode()---------------------------------------------------------------------------
void List::InsertListNode(int Ele)
{
LISTNODE *Curr,*p,*Prev;
p=MakeListNode(Ele);
Curr=Head;
//Case 1: When there is no list
if(Head==NULL){
Head=p;
return;
}
//Case 2: When incoming node has data less than that of the head of the list
if(p->Num<Head->Num){
p->Next=Head;
Head=p;
return;
}
//Case 3: Insert data at the middle or at the end of the list
while(Curr && p->Num>Curr->Num){
Prev=Curr;
Curr=Curr->Next;
}
Prev->Next=p;
p->Next=Curr;
//cout<<"\nThe element is inserted\n\n";
return;
}
//-------------------------------------------------------Display()---------------------------------------------------------------------------
void List::Display()
{
LISTNODE *Curr;
Curr=Head;
if(Head==NULL){
cout<<"Sorry! The list is empty";
return;
}
cout<<"\nThe elements in the list are:\n";
while(Curr->Next!=NULL){
cout<<Curr->Num<<"\t";
Curr=Curr->Next;
}
cout<<Curr->Num<<"\t";
return;
}
//-------------------------------------------------------DeleteFirstNode()---------------------------------------------------------------------------
int List::DeleteFirstNode(){
LISTNODE *Curr;
Curr=Head;
int ele;
if(Head==NULL){
cout<<"Sorry! The list is empty";
return -1;
}
if(Curr->Next==NULL){
Head=NULL;
ele=Curr->Num;
free(Curr);
return ele;
}
ele=Curr->Num;
Head=Curr->Next;
free(Curr);
return(ele);
}
//-------------------------------------------------------DeleteListNode()---------------------------------------------------------------------------
void List::DeleteListNode(int Num){
LISTNODE *Curr,*Prev;
Prev=Curr=Head;
if(Head==NULL){
cout<<"Sorry! The list is empty";
return;
}
else if(Num==Head->Num){
Head=Curr->Next;
free(Curr);
cout<<"Element is deleted!";
return;
}
else{
while(Curr->Next!=NULL){
Prev=Curr;
Curr=Curr->Next;
if(Curr->Num==Num){
Prev->Next=Curr->Next;
free(Curr);
cout<<"The number is deleted";
return;
}
}
if(Curr->Num==Num)
{
Prev->Next=Curr->Next;
free(Curr);
cout<<"The number is deleted";
return;
}
cout<<"Sorry! The entered element is not found in the list!!";
}
}