-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete_Element_From_LinkedList.cpp
99 lines (78 loc) · 1.62 KB
/
Delete_Element_From_LinkedList.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
// DeleteElement.cpp : Defines the entry point for the console application.
// Example to delete data from linked list
#include "stdafx.h"
#include <iostream>
using namespace std;
// there is another program to insert element in the linked list
struct Node{
Node* _link;
int _data;
};
Node* _head;
Node* _createNode(int data)
{
Node* newNode = new Node;
newNode->_data = data;
newNode->_link = NULL;
return newNode;
}
void _insertNode(int data )
{
Node* newNode = _createNode(data);
// check the node inthe linked list
if (_head==NULL) // for the very first nmode head will be NULL
{
_head = newNode;
return ;
}
newNode->_link = _head;
_head = newNode;
}
void _printData()
{
Node* tempNode = _head;
while (tempNode)
{
std::cout<<tempNode->_data<<"\t";
tempNode = tempNode->_link;
}
std::cout<<"\n";
}
// delete node from the nth position
void _deleteDataFrom_nth_Position(int pos)
{
if (_head==NULL)
{
std::cout<<"There is no element in the linked list"<<std::endl;
}
// if its a first element
Node* tempNode = _head;
if (pos==1)
{
_head = tempNode->_link;
return;
}
for (int i = 0; i < pos - 2 ; i++)
{
tempNode = tempNode->_link;
}
Node* temp = tempNode->_link;
tempNode->_link = temp->_link;
}
int _tmain(int argc, _TCHAR* argv[])
{
// initialize head;
_head = NULL;
_insertNode(95);
_insertNode(5);
_insertNode(15);
_insertNode(52);
_insertNode(25);
_insertNode(35);
_insertNode(45);
_insertNode(55);
_printData();
_deleteDataFrom_nth_Position(4);
_printData();
return 0;
}