-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletionInLinkedList.cpp
76 lines (67 loc) · 1.49 KB
/
deletionInLinkedList.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
#include<iostream>
using namespace std ;
class Node{
public:
int data;
class Node * next ;
};
void traversalOfElements(class Node * ptr)
{
while(ptr != NULL)
{
cout<<ptr ->data <<"|";
ptr = ptr ->next;
}
}
void deletethisElement(class Node * ptr , int position)
{ class Node* q;
int counter = 1;
while ( ptr != NULL)
{
ptr = ptr ->next ;
counter++;
if(counter == position)
{
q = ptr ->next ;
ptr ->next = q ->next ;
free(q);
}
}
}
int main()
{
int position =2;
class Node * head ;
class Node * second ;
class Node * third ;
class Node * forth;
//class Node * NewNode;
//class Node * head2 ;
//head2 = new Node ;
head = new Node ;
second = new Node ;
third = new Node ;
forth = new Node ;
//NewNode = new Node ;
head->data = 7 ;
head->next = second;
//head2 = head;
second->data = 8 ;
second->next = third;
third->data = 9 ;
third->next = forth;
forth->data = 10 ;
forth->next = NULL;
//NewNode->data = 78;
//traversalOfElements(head);
//cout<<endl;
//insertionInBegining(NewNode,head);
//head = NewNode ;
//traversalOfElements(head);
//insertionInEnd(NewNode,head);
//traversalOfElements(head);
//insertionInBetween(NewNode,head,position);
deletethisElement(head , position);
traversalOfElements(head);
return 0 ;
}