-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_list.cpp
80 lines (76 loc) · 1.38 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
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *Head = NULL;
// Insert Node
void insertNode(int value)
{
node *new_node, *last;
new_node = new node;
new_node->data = value;
if(Head == NULL)
{
Head = new_node;
new_node->next = NULL;
}
else{
last = Head;
while(last->next != NULL)
{
last = last->next;
}
last->next = new_node;
new_node->next = NULL;
}
}
void DeleteNode(int value)
{
node *current, *previous;
current = Head;
previous = Head;
if(current->data == value)
{
Head = current->next;
free(current);
return;
}
while (current->data != value)
{
previous = current;
current = current->next;
}
previous->next = current->next;
free(current);
}
void Display()
{
node *current;
if(Head == NULL)
{
cout<<"Linked List Is Empty\n";
}
else
{
current = Head;
while (current != NULL)
{
cout<<current->data<<" ";
current = current->next;
}
}
cout<<endl;
}
int main()
{
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
DeleteNode(20);
Display();
return 0;
}