1: #include <iostream>
2: using namespace std;
3:
4: class Node {
5: public:
6: int data;
7: Node* next;
8: };
9:
10:
11: Node* head;
12:
13:
14: void display(Node* n)
15: {
16: while(n!=NULL)
17: {
18: cout<<n->data<<" ->";
19: n=n->next;
20: }
21: cout<<"NULL \n";
22: }
23:
24: void insertatfront1(Node* X,Node** head)
25: /* We need **head because we are completely assigning
another node to it
26: but for X we are only modifying its datamembers data
and next
27: so * is enough.
28: Node* is used for X because X is of Node* type
29: head is also Node* type but we are modifying it so
we use pointer form of Node* i.e Node**
30: We need to call as &head in function calling
31: Eg: insertatfront1(X,&head);display(head); &head
because Node** type in func
32:
33: or we can also use Node* &head as in next function
to modify it
34: This is Call by Reference
35: */
36: { cout<<"After inserting "<<X->data<<" at front:
\n";
37: X->next=*head;
38: (*head)=X;
39: }
40:
41:
42: void insertatfront(int x,Node* &head)
43: /*we can also use Node* &head than Node* *head
44: This is Call by Reference so as to modify head
45: We need to call as head in function calling
46: Eg: insertatfront(X,head); // only head because
Node* & type in func
47: */
48: { cout<<"After inserting "<<x<<" at front: \n";
49: Node* Y;Y=new Node();
50: Y->data=x;
51: Y->next=head;
52: (head)=Y;
53: }
54:
55: void insertafternode(Node* prev,int y)
56: { if (prev == NULL)
57: {
58: cout<<"The given previous node cannot be NULL";
59: return;
60: }
61: cout<<"Inserting "<<y<<" after "<<(prev)->data<<"
: \n";
62: Node *Y; Y=new Node();
63: Y->data=y;
64: Y->next=(prev)->next;
65: (prev)->next=Y;
66: }
67:
68: void insertafterkey(int key,int y,Node* head)
69: { if (key==NULL)
70: {
71: cout<<"The given key cannot be NULL";
72: return;
73: }
74: cout<<"Inserting "<<y<<" after "<<key<<" : \n";
75: Node* prev=head;
76: while(prev->data!=key)
77: {
78: prev=prev->next;
79: }
80: Node *Y; Y=new Node();
81: Y->data=y;
82: Y->next=(prev)->next;
83: (prev)->next=Y;
84: }
85:
86: void insertatpos(int pos,int y,Node** head)
87: {
88: if(pos==1) {
89: cout<<"After inserting "<<y<<" at front: \n";
90: Node *Y;Y=new Node();
91: Y->data=y;
92: Y->next=*head;
93: (*head)=Y;
94: }
95: else{
96: cout<<"Inserting "<<y<<" at position
"<<pos<<" : \n";
97: Node* prev;
98: prev=*head;
99: for(int i=2;i<=pos-1;i++)
100: { prev=prev->next; }
101: Node *Y; Y=new Node();
102: Y->data=y;
103: Y->next=(prev)->next;
104: (prev)->next=Y;
105: }
106: }
107:
108:
109: void insertatend(int z,Node* head)
110: { Node* n;
111: Node* Z;Z=new Node();
112: Z->data=z;
113: Z->next=NULL;
114: n=head;
115: if (head == NULL)
116: {
117: head= Z;
118: return;
119: }
120: while(n->next!=NULL)
121: {
122: n=n->next;
123: }
124: n->next=Z;
125: cout<<"After inserting "<<z<<" at end: \n";
126: }
127:
128: void deleteatfront(Node** head)
129: {
130: Node* n;
131: n=*head;
132: *head=(*head)->next;
133: delete(n);
134: cout<<"After deleting first node: \n";
135:
136: }
137:
138: void deleteatpos(int pos,Node** head)
139: {
140: Node* prevnode;Node* currentnode;
141: if(pos==1)
142: {
143: Node* n;
144: n=*head;
145: *head=(*head)->next;
146: delete(n);
147: cout<<"After deleting first node: \n";
148: }
149: else
150: {
151: currentnode=*head;
152: for(int i=1;i<=pos-1;i++)
153: {
154: prevnode=currentnode;
155: currentnode=currentnode->next;
156: }
157: prevnode->next=currentnode->next;
158: delete(currentnode);
159: cout<<"After deleting at postion
"<<pos<<" : \n";
160: }
161:
162: }
163:
164:
165: void deleteatkey(int key,Node** head)
166: {
167: if((*head)->data==key)
168: {
169: Node* n;
170: n=*head;
171: *head=(*head)->next;
172: delete(n);
173: cout<<"After deleting first node: \n";
174: }
175: else
176: {
177: Node* prev=*head;
178: Node* keynode=prev->next;
179: while(keynode->data!=key)
180: {
181: prev=keynode;
182: keynode=keynode->next;
183: }
184: prev->next=keynode->next;
185: delete(keynode);
186: cout<<"After deleting "<<key<<endl;
187: }
188:
189: }
190:
191: void deleteatend(Node* head)
192: {
193: if(head->next==NULL)
194: {
195: delete(head);
196: }
197: else
198: {
199: Node* prev=head;
200: Node* last=prev->next;
201: while(last->next!=NULL)
202: {
203: prev=last;
204: last=last->next;
205: }
206: prev->next=NULL;
207: delete(last);
208: cout<<"After deleting last node: \n";
209: }
210:
211: }
212:
213:
214: int main()
215: {
216: Node* first;Node* second;
217: Node* third;Node* fourth;
218:
219: first=new Node();
220: second=new Node();
221: third=new Node();
222: fourth=new Node();
223:
224: first->data=4;
225: first->next=second;
226:
227: second->data=9;
228: second->next=third;
229:
230: third->data=16;
231: third->next=fourth;
232:
233: fourth->data=25;
234: fourth->next=NULL;
235:
236: head=first;
237: display(head);
238:
239: Node* X;
240: X=new Node();
241: X->data=10;
242: X->next=NULL;
243:
244: insertatfront1(X,&head);display(head);// &head
because Node** type in func
245: insertatfront(20,head);display(head);
246: insertafternode(second,100);display(head);
247: insertafterkey(16,500,head);display(head);
248: insertafterkey(20,1500,head);display(head);
249: insertatpos(1,1729,&head);display(head);
250: insertatpos(2,200,&head);display(head);
251: insertatend(400,head);display(head);
252: deleteatfront(&head);display(head);
253: deleteatpos(5,&head);display(head);
254: deleteatpos(3,&head);display(head);
255: deleteatkey(200,&head);display(head);
256: deleteatkey(head->data,&head);display(head);
257: deleteatend(head);display(head);
258:
259: int c;
260: do{
261: cout<<(" .....Menu..... \n");
262: cout<<(" [Link] \n [Link] at begin \n
[Link] at position \n [Link] after key \n [Link]
at end \n");
263: cout<<(" [Link] at Begin \n [Link] at Position
\n [Link] at Key \n [Link] at End \n [Link] \n");
264: cout<<("Enter any Choice: ");cin>>(c);
265: switch(c){
266: int x,y;
267: case 1:
268: display(head);
269: break;
270: case 2:
271: cout<<("Enter value to be inserted at
beginning: ");
272: cin>>(x);
273: insertatfront(x,head);
274: display(head);
275: break;
276: case 3:
277: cout<<("Enter value to be inserted :
");
278: cin>>(y);
279: cout<<("Enter position to be inserted
: ");
280: cin>>(x);
281: insertatpos(x,y,&head);
282: display(head);
283: break;
284: case 4:
285: cout<<("Enter value to be inserted :
");
286: cin>>(y);
287: cout<<("Enter key after which to be
inserted : ");
288: cin>>(x);
289: insertafterkey(x,y,head);
290: display(head);
291: break;
292: case 5:
293: cout<<("Enter value to be inserted at
End: ");
294: cin>>(x);
295: insertatend(x,head);
296: display(head);
297: break;
298: case 6:
299: deleteatfront(&head);
300: display(head);
301: break;
302: case 7:
303: cout<<("Enter position to be deleted :
");
304: cin>>(y);
305: deleteatpos(y,&head);
306: display(head);
307: break;
308: case 8:
309: cout<<("Enter value to be deleted : ");
310: cin>>(y);
311: deleteatkey(y,&head);
312: display(head);
313: break;
314: case 9:
315: deleteatend(head);
316: display(head);
317: break;
318: case 0:
319: break;
320: default:
321: cout<<("Invalid Input !! Please Try
again:\n");
322: }
323: }while(c!=0);
324:
325: return 0;
326: }