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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
#include <iostream>
using namespace std;
class node{
public:
int value;
node *next;
void insert_front(node *&, int);
void delete_front(node *&);
void insert_back(node *&, int);
void delete_back(node *&);
void insert_by_value(node *&, int);
void delete_by_value(node *&, int);
void sort_by_value(node *&);
bool search_by_value(node *&, int);
void display(node *&);
};
void insert_front(node *&head, int x) //iterative
{
node *temp = new node;
temp->value = x;
temp->next = head;
head = temp;
}
void delete_front(node *&head) //iterative
{
node *temp;
temp = new node;
temp = head;
head = temp->next;
delete temp;
}
void insert_back(node *&head, int x) //recursive
{
if(!head){
head = new node;
head->value = x;
head->next = NULL;
}
else
return insert_back(head->next , x);
}
void delete_back(node *&head) //recursive
{
if(head->next == NULL)
head = NULL;
else
return delete_back(head->next);
}
void insert_by_value(node *&head, int x) //recursive
{
node *temp = new node;
temp->value = x;
if(head == NULL || head->value > x){
temp->next = head;
head = temp;
}
else
insert_by_value(head->next, x);
}
void delete_by_value(node *&head, int x) //recursive
{
if(head->value == x)
head = head->next;
else
delete_by_value(head->next, x);
}
void sort_by_value(node *&head) //iterative
{
//bubble sort
node *temp1 = new node;
node *temp2 = new node;
for(temp1=head; temp1->next!=NULL; temp1=temp1->next)
for(node *temp2=temp1->next; temp2!=NULL; temp2=temp2->next)
if(temp1->value>temp2->value)
swap(temp1->value , temp2->value);
}
bool search_by_value(node *&head, int x) //recursive
{
if(head == NULL)
return false;
else{
if(head!=NULL && head->value==x)
return true;
else
search_by_value(head->next, x);
}
}
void display_list(node *&head) //recursive
{
if(head){
cout<<head->value<<" -> ";
display_list(head->next);
}
}
int main(){
node *head = NULL;
int n, option;
cout<<"\t\t\t\t\t\bMenu\n";
cout<<"\t--------------------------------------------------------------\n";
cout<<"\t\tOption no.\t\t\t Operation\n";
cout<<"\t\t 1\t\t\t\tInsert front\n\t\t 2\t\t\t\tDelete front\n";
cout<<"\t\t 3\t\t\t\tInsert back\n\t\t 4\t\t\t\tDelete back\n";
cout<<"\t\t 5\t\t\t\t\b\bInsert by value\n\t\t 6\t\t\t\t\b\bDelete by value\n";
cout<<"\t\t 7\t\t\t\t\bSort by value\n\t\t 8\t\t\t\t Search\n";
cout<<"\t\t 9\t\t\t\t Display\n\t\t 10\t\t\t\t Exit\n";
do{
cout<<"\nInsert your option: ";
cin>>option;
switch(option){
case 1:
cout<<"Enter value: ";
cin>>n;
insert_front(head, n);
break;
case 2:
if(!head)
cout<<"Nothing to be deleted!\nChoose another option.\n";
else
delete_front(head);
break;
case 3:
cout<<"Enetr value:\n";
cin>>n;
insert_back(head, n);
break;
case 4:
if(!head)
cout<<"Nothing to be deleted!\nChoose another option.\n";
else
delete_back(head);
break;
case 5:
cout<<"Enter value:\n";
cin>>n;
insert_by_value(head, n);
break;
case 6:
if(!head)
cout<<"Nothing to be deleted!\nChoose another option.\n";
else{
cout<<"What inserted value do you want to delete?\n";
cin>>n;
delete_by_value(head, n);
}
break;
case 7:
if(!head)
cout<<"Current list is empty.\n";
else{
sort_by_value(head);
cout<<"Sorted list is:\n";
cout<<"HEAD -> ";
display_list(head);
cout<<"NULL\n";
}
break;
case 8:
cout<<"Which inserted value are you searching?\n";
cin>>n;
if(search_by_value(head, n))
cout<<n<<" exists in the list\n";
else
cout<<n<<" doesn't exist in the list\n";
break;
case 9:
if(!head)
cout<<"Current list is empty.\n";
else{
cout<<"Current list is:\n";
cout<<"HEAD -> ";
display_list(head);
cout<<"NULL\n";
}
break;
case 10:
cout<<"No operation!\n";
break;
default:
cout<<"Invalid input. Choose another option.";
break;
}
}while(option != 10);
cout<<endl;
system ("Pause");
return 0;
}
|