-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.cpp
122 lines (114 loc) · 2.45 KB
/
linked_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
#include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node {
int x;
Node * next;
} *head,*save,*last,*node;
Node* createNode(int n)
{
node = new Node;
node->x=n;
node->next=NULL;
return node;
}
void insertNode(Node* n)
{
if (head==NULL){
head =n;
head->next=NULL;
last=head;
}
else
{
last->next=n;
last=n;
}
}
Node* deleteNode(int m){
if (head==NULL)
cout<<"\nList empty.\n";
else {
cout<<"tutuutut";
node = head;
if (node->x==m){
if (node->next==NULL){
head=NULL;
}
else {
head =node->next;
}
return node;
}
while (node->next!=NULL)
{
cout<<"yoyooy";
if (node->next->x==m){
save=node->next;
if (node->next->next!=NULL)
node->next=node->next->next;
else
node->next=NULL;
cout<<node->x;
return save;
}
node=node->next;
}
return NULL;
}
}
void displayList(){
node= head;
cout<<"\n The List is : ";
while (node!=NULL)
{
cout<<node->x<<" ";
node=node->next;
}
cout<<endl;
}
int main()
{
int x;
int n;
while (1){
cout<<"\nOPTIONS \n\t1. Insert\n\t2. Delete\n\t3.Display\n\t4. Exit\n";
cin>>n;
switch(n){
case 1 :
cout<<"Enter the value of node : ";
cin>>x;
node=createNode(x);
if (node==NULL){
cout<<"\nError ..Aborting\n";
break;
}
else {
insertNode(node);
cout<<"\nNode inserted.\n";
}
break;
case 2 :
int ch;
cout<<"\n Enter the node to be deleted ?(Y/N) ";
cin>>ch;
node=deleteNode(ch);
if (node == NULL){
cout<<"\n Not found.\n";
}
else
cout<<"\n Value "<<node->x<<" deleted.\n";
delete node;
break;
case 3 :
displayList();
break;
case 4 :
return -4;
default :
cout<<"\n Incorrect Option selected.\n";
break;
}
}
}